-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
268 lines (252 loc) · 5.23 KB
/
cli.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
package main
import (
"flag"
"fmt"
"io/fs"
"neocitiesCli/api"
"neocitiesCli/ui"
"neocitiesCli/util"
"os"
"path/filepath"
)
type command struct {
function string
args []string
useGitIgnore bool
}
func execute(conn *api.Connection, config api.Config, cmd command) error {
var err error
switch cmd.function {
case "push":
var files []string
err = filepath.WalkDir(cmd.args[0], func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return err
}
if cmd.useGitIgnore {
files, err = util.RemoveGitIgnore(files)
if err != nil {
return err
}
}
util.Upload(conn, files)
case "upload":
var errs []error
var files []string
for _, file := range cmd.args {
_, err := os.Stat(file)
if err != nil {
errs = append(errs, err)
} else {
files = append(files, file)
}
}
if len(errs) > 0 {
errorList := ""
for _, err := range errs {
errorList += err.Error() + "\n"
}
return fmt.Errorf("multiple errors:\n%s", errorList)
}
if cmd.useGitIgnore {
files, err = util.RemoveGitIgnore(cmd.args)
if err != nil {
return err
}
}
util.Upload(conn, files)
case "delete":
files := cmd.args
if cmd.useGitIgnore {
files, err = util.RemoveGitIgnore(cmd.args)
if err != nil {
return err
}
}
err = conn.Delete(files)
if err != nil {
return err
}
fmt.Printf("Deleted %d files\n", len(files))
case "list":
args := ""
if len(cmd.args) > 0 {
args = cmd.args[0]
}
items, err := conn.List(args)
if err != nil {
return err
}
for _, item := range items {
fmt.Println(item.View())
}
case "info":
info, err := conn.Info(cmd.args[0])
if err != nil {
return fmt.Errorf("error: %s\nEnsure URL is valid", err)
}
fmt.Println(ui.InfoView(info))
case "config":
// change current account
// list accounts
// login
// reset (scary)
if len(cmd.args) == 0 {
fmt.Println("no arguments provided")
fmt.Print(CONFIG_HELP_STRING)
return fmt.Errorf("invalid arguments")
}
switch cmd.args[0] {
case "list":
configList, err := util.ReadConfig()
if err != nil {
return err
}
for _, cfg := range configList.Configs {
fmt.Printf("- %s\n", cfg.Domain)
}
return nil
case "set":
if len(cmd.args) < 2 {
fmt.Println("Usage: neocities config set [domain]")
return fmt.Errorf(("invalid arguments"))
}
configList, err := util.ReadConfig()
if err != nil {
return err
}
defaultSet := false
var changed []int = []int{}
for i, cfg := range configList.Configs {
if cfg.IsDefault {
configList.Configs[i].IsDefault = false
changed = append(changed, i)
}
if !defaultSet && cfg.Domain == cmd.args[1] {
configList.Configs[i].IsDefault = true
defaultSet = true
changed = append(changed, i)
}
}
if !defaultSet {
return fmt.Errorf("domain %s not found", cmd.args[1])
}
for _, i := range changed {
err = util.WriteConfig(configList.Configs[i])
if err != nil {
return err
}
}
return nil
case "login":
cfg, err := util.Login()
if err != nil {
return err
}
err = util.WriteConfig(cfg)
return err
case "reset":
if len(cmd.args) == 0 || cmd.args[0] != "--force" {
resetting := util.ResetPrompt()
if !resetting {
return fmt.Errorf("reset aborted")
}
}
path, err := util.GetConfigPath()
if err != nil {
return err
}
err = os.Remove(path)
if err != nil {
return err
}
util.WriteConfig(util.DEFAULT_CONFIG.Configs[0])
default:
fmt.Printf("invalid subcommand: %s\n", cmd.args[0])
fmt.Print(CONFIG_HELP_STRING)
return fmt.Errorf("invalid subcommand")
}
case "tui":
err := ui.Run(*conn, config)
if err != nil {
return err
}
case "version":
fmt.Println(VERSION)
return nil
}
return err
}
func setFlags() (command, error) {
var cmd command
args := os.Args
if len(args) == 1 {
cmd.function = "tui"
return cmd, nil
}
switch args[1] {
case "push":
if len(args) < 3 {
fmt.Println("Usage: neocities push directory")
return cmd, fmt.Errorf("invalid arguments")
}
cmd = command{
function: "push",
args: args[2:],
}
case "upload":
if len(args) < 3 {
fmt.Println("Usage: neocities upload [files]")
return cmd, fmt.Errorf("invalid arguments")
}
cmd = command{
function: "upload",
args: args[2:],
}
case "delete":
if len(args) < 3 {
fmt.Println("Usage: neocities delete [files]")
return cmd, fmt.Errorf("invalid arguments")
}
cmd = command{
function: "delete",
args: args[2:],
}
case "list":
cmd = command{
function: "list",
args: args[2:],
}
case "info":
sitename := ""
if len(args) > 2 {
sitename = args[2]
}
cmd = command{
function: "info",
args: []string{sitename},
}
case "config":
cmd = command{
function: "config",
args: args[2:],
}
case "--version":
cmd = command{
function: "version",
args: nil,
}
default:
fmt.Print(MAIN_HELP_STRING)
return cmd, fmt.Errorf("invalid subcommand")
}
ignore := flag.Bool("no-gitignore", false, "ignore exclusions listed in .gitignore file")
cmd.useGitIgnore = !*ignore
flag.Parse()
return cmd, nil
}