-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcommands.go
319 lines (273 loc) · 6.6 KB
/
commands.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"fmt"
"github.com/howeyc/gopass"
"github.com/writeas/writeas-cli/fileutils"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
func cmdPost(c *cli.Context) error {
_, err := handlePost(readStdIn(), c)
return err
}
func cmdNew(c *cli.Context) error {
fname, p := composeNewPost()
if p == nil {
// Assume composeNewPost already told us what the error was. Abort now.
os.Exit(1)
}
// Ensure we have something to post
if len(*p) == 0 {
// Clean up temporary post
if fname != "" {
os.Remove(fname)
}
InfolnQuit("Empty post. Bye!")
}
_, err := handlePost(*p, c)
if err != nil {
Errorln("Error posting: %s", err)
Errorln(messageRetryCompose(fname))
return cli.NewExitError("", 1)
}
// Clean up temporary post
if fname != "" {
os.Remove(fname)
}
return nil
}
func cmdPublish(c *cli.Context) error {
filename := c.Args().Get(0)
if filename == "" {
return cli.NewExitError("usage: writeas publish <filename>", 1)
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
p, err := handlePost(content, c)
if err != nil {
return err
}
// Save post to posts folder
cfg, err := loadConfig()
if cfg.Posts.Directory != "" {
err = WritePost(cfg.Posts.Directory, p)
if err != nil {
return err
}
}
return nil
}
func cmdDelete(c *cli.Context) error {
friendlyID := c.Args().Get(0)
token := c.Args().Get(1)
if friendlyID == "" {
return cli.NewExitError("usage: writeas delete <postId> [<token>]", 1)
}
u, _ := loadUser()
if token == "" {
// Search for the token locally
token = tokenFromID(friendlyID)
if token == "" && u == nil {
Errorln("Couldn't find an edit token locally. Did you create this post here?")
ErrorlnQuit("If you have an edit token, use: writeas delete %s <token>", friendlyID)
}
}
tor := isTor(c)
if c.Int("tor-port") != 0 {
torPort = c.Int("tor-port")
}
if tor {
Info(c, "Deleting via hidden service...")
} else {
Info(c, "Deleting...")
}
err := DoDelete(c, friendlyID, token, tor)
if err != nil {
return err
}
// Delete local file, if necessary
cfg, err := loadConfig()
if cfg.Posts.Directory != "" {
// TODO: handle deleting blog posts
err = fileutils.DeleteFile(filepath.Join(cfg.Posts.Directory, friendlyID+postFileExt))
if err != nil {
return err
}
}
return nil
}
func cmdUpdate(c *cli.Context) error {
friendlyID := c.Args().Get(0)
token := c.Args().Get(1)
if friendlyID == "" {
return cli.NewExitError("usage: writeas update <postId> [<token>]", 1)
}
u, _ := loadUser()
if token == "" {
// Search for the token locally
token = tokenFromID(friendlyID)
if token == "" && u == nil {
Errorln("Couldn't find an edit token locally. Did you create this post here?")
ErrorlnQuit("If you have an edit token, use: writeas update %s <token>", friendlyID)
}
}
// Read post body
fullPost := readStdIn()
tor := isTor(c)
if c.Int("tor-port") != 0 {
torPort = c.Int("tor-port")
}
if tor {
Info(c, "Updating via hidden service...")
} else {
Info(c, "Updating...")
}
return DoUpdate(c, fullPost, friendlyID, token, c.String("font"), tor, c.Bool("code"))
}
func cmdGet(c *cli.Context) error {
friendlyID := c.Args().Get(0)
if friendlyID == "" {
return cli.NewExitError("usage: writeas get <postId>", 1)
}
tor := isTor(c)
if c.Int("tor-port") != 0 {
torPort = c.Int("tor-port")
}
if tor {
Info(c, "Getting via hidden service...")
} else {
Info(c, "Getting...")
}
return DoFetch(friendlyID, userAgent(c), tor)
}
func cmdAdd(c *cli.Context) error {
friendlyID := c.Args().Get(0)
token := c.Args().Get(1)
if friendlyID == "" || token == "" {
return cli.NewExitError("usage: writeas add <postId> <token>", 1)
}
err := addPost(friendlyID, token)
return err
}
func cmdList(c *cli.Context) error {
urls := c.Bool("url")
ids := c.Bool("id")
var p Post
posts := getPosts()
for i := range *posts {
p = (*posts)[len(*posts)-1-i]
if ids || !urls {
fmt.Printf("%s ", p.ID)
}
if urls {
base := writeasBaseURL
if isDev() {
base = devBaseURL
}
ext := ""
// Output URL in requested format
if c.Bool("md") {
ext = ".md"
}
fmt.Printf("%s/%s%s ", base, p.ID, ext)
}
fmt.Print("\n")
}
return nil
}
func cmdAuth(c *cli.Context) error {
// Check configuration
u, err := loadUser()
if err != nil {
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
}
if u != nil && u.AccessToken != "" {
return cli.NewExitError("You're already authenticated as "+u.User.Username+". Log out with: writeas logout", 1)
}
// Validate arguments and get password
username := c.Args().Get(0)
if username == "" {
return cli.NewExitError("usage: writeas auth <username>", 1)
}
fmt.Print("Password: ")
pass, err := gopass.GetPasswdMasked()
if err != nil {
return cli.NewExitError(fmt.Sprintf("error reading password: %v", err), 1)
}
// Validate password
if len(pass) == 0 {
return cli.NewExitError("Please enter your password.", 1)
}
return DoLogIn(c, username, string(pass))
}
func cmdLogOut(c *cli.Context) error {
return DoLogOut(c)
}
func cmdOptions(c *cli.Context) error {
// Edit config file
if c.Bool("e") {
composeConfig()
// List configs
} else if c.Bool("l") || c.Bool("a") {
uc, err := loadConfig()
if err != nil {
ErrorlnQuit(fmt.Sprintf("Error loading config: %v", err), 1)
}
printConfig(uc, "", c.Bool("a"))
// Check arguments
} else {
nargs := len(c.Args())
// No arguments nor options: display command usage
if nargs == 0 {
cli.ShowSubcommandHelp(c)
return nil
}
name := c.Args().Get(0)
value := c.Args().Get(1)
// Load config file
uc, err := loadConfig()
if err != nil {
ErrorlnQuit(fmt.Sprintf("Error loading config: %v", err), 1)
}
// Get reflection of field
rval, err := getConfigField(uc, name)
if err != nil {
ErrorlnQuit(fmt.Sprintf("%v", err), 1)
}
// Print value
if nargs == 1 {
fmt.Printf("%s=%v\n", name, *rval)
// Set value
} else {
// Cast and set value
switch typ := rval.Kind().String(); typ {
case "bool":
b, err := strconv.ParseBool(value)
if err != nil {
ErrorlnQuit(fmt.Sprintf("error: \"%s\" is not a valid boolean", value), 1)
}
rval.SetBool(b)
case "int":
i, err := strconv.ParseInt(value, 0, 0)
if err != nil {
ErrorlnQuit(fmt.Sprintf("error: \"%s\" is not a valid integer", value), 1)
}
rval.SetInt(i)
case "string":
rval.SetString(value)
}
// Save config to file
err = saveConfig(uc)
if err != nil {
ErrorlnQuit(fmt.Sprintf("Unable to save config: %s", err), 1)
}
fmt.Println("Saved config.")
}
}
return nil
}