-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemojipacks.go
180 lines (148 loc) · 3.84 KB
/
emojipacks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package emojipacks
import (
"flag"
"fmt"
"net/http"
"strings"
loginslack "github.com/Code-Hex/go-emojipacks/internal/slack"
p "github.com/Songmu/prompter"
"github.com/logrusorgru/aurora"
"github.com/mattn/go-colorable"
)
var (
stdout = colorable.NewColorableStdout()
stderr = colorable.NewColorableStderr()
)
func Run(args []string) int {
progName := args[0]
if err := run(args); err != nil {
fmt.Fprintf(stderr, "failed to run %s: %v\n", progName, err)
return 1
}
return 0
}
var _ flag.Value = (sliceFlags)(nil)
type sliceFlags []string
func (s sliceFlags) String() string {
return strings.Join(s, ", ")
}
func (s sliceFlags) Set(v string) error {
s = append(s, v)
return nil
}
type options struct {
subdomain string
email string
password string
token string
yamlfiles sliceFlags
}
func parse(args []string) (*options, error) {
o := &options{}
flg := flag.NewFlagSet(args[0], flag.ContinueOnError)
flg.StringVar(&o.subdomain, "s", "", "subdomain (workspace) of your slack")
flg.StringVar(&o.email, "e", "", "email of your account")
flg.StringVar(&o.password, "p", "", "password of your account")
flg.StringVar(&o.token, "t", "", "xoxs type api token of your account")
flg.Var(&o.yamlfiles, "y", "batch uploading of multiple yaml files")
if err := flg.Parse(args[1:]); err != nil {
return nil, err
}
return o, nil
}
type inputter interface {
Prompt(message, defaultAnswer string) string
Password(message string) string
}
type defaultPrompter struct{}
func (defaultPrompter) Prompt(message, defaultAnswer string) string {
return p.Prompt(message, defaultAnswer)
}
func (defaultPrompter) Password(message string) string {
return p.Password(message)
}
var prompter inputter = defaultPrompter{}
func (o *options) getSubDomain() string {
if o.subdomain != "" {
return o.subdomain
}
return prompter.Prompt("Slack subdomain", "")
}
func (o *options) getEmail() string {
if o.email != "" {
return o.email
}
return prompter.Prompt("Email address login", "")
}
func (o *options) getPassword() string {
if o.password != "" {
return o.password
}
return prompter.Password("Password")
}
func (o *options) getToken() string {
if o.token != "" {
return o.token
}
return prompter.Prompt("xoxs API Token (empty is ok)", "")
}
func (o *options) getYAMLFiles() []string {
if len(o.yamlfiles) > 0 {
return o.yamlfiles
}
return []string{prompter.Prompt("Path or URL of Emoji yaml file", "")}
}
func run(args []string) error {
o, err := parse(args)
if err != nil {
return err
}
subdomain := o.getSubDomain()
accessToken := o.getToken()
if accessToken == "" || !(strings.HasPrefix(accessToken, "xoxs") || strings.HasPrefix(accessToken, "xoxc")) {
fmt.Println("not found valid api token")
email := o.getEmail()
password := o.getPassword()
accessToken, err = loginslack.Login(email, password, subdomain)
if err != nil {
return err
}
}
yamlFiles := o.getYAMLFiles()
packs, err := unmarshalYAMLs(yamlFiles)
if err != nil {
return err
}
u := &uploader{accessToken: accessToken}
return u.runUploadEmojiPacks(packs)
}
type uploader struct {
accessToken string
}
func (u *uploader) runUploadEmojiPacks(packs []*EmojiPacks) error {
for _, pack := range packs {
if err := u.runUploadEmojiPack(pack); err != nil {
return err
}
}
return nil
}
func (u *uploader) runUploadEmojiPack(pack *EmojiPacks) error {
fmt.Fprintln(stdout, aurora.BrightCyan("start upload: "+pack.Title))
for _, emoji := range pack.Emojis {
err := u.pipeDownloadAndUpload(emoji.Src, emoji.Name)
if err != nil {
return err
}
fmt.Fprintln(stdout, aurora.BrightGreen("complete: "+emoji.Name))
}
return nil
}
func (u *uploader) pipeDownloadAndUpload(srcURL, emojiName string) error {
resp, err := http.Get(srcURL)
if err != nil {
return err
}
defer resp.Body.Close()
return u.uploadEmoji(resp.Body, resp.ContentLength, emojiName)
}