-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
334 lines (325 loc) · 8.9 KB
/
main.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/imshuai/alistsdk-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
var (
client *alistsdk.Client
logger *logrus.Logger
conf *Config
)
const (
VERSION = "v0.1.1"
)
type Config struct {
Endpoint string `json:"endpoint" yaml:"endpoint"`
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
SkipVerify bool `json:"skip-verify" yaml:"skip-verify"`
Proxy string `json:"proxy" yaml:"proxy"`
}
func main() {
logger = logrus.New()
logger.SetLevel(logrus.WarnLevel)
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
app := cli.NewApp()
app.Name = "alist-cli"
app.Description = "alist command line interface"
app.Version = VERSION
app.EnableBashCompletion = true
app.Flags = []cli.Flag{}
app.HideVersion = true
app.Commands = []*cli.Command{
{
Name: "version",
Usage: "show alist-cli version",
Action: func(c *cli.Context) error {
fmt.Printf("version: %s\n", VERSION)
return nil
},
},
{ // move
Name: "move",
Aliases: []string{"mv", "rename"},
Usage: "move file from src to dst, cannot cross mount point",
UsageText: fmt.Sprintf(`%s move [-v] src-path dst-folder`, app.Name),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "verbose mode, show more infomation",
Value: false,
Action: func(c *cli.Context, v bool) error {
if v {
logger.SetLevel(logrus.InfoLevel)
logger.Info("verbose mode enabled")
}
return nil
},
},
},
Action: func(c *cli.Context) error {
//TODO: move file from src to dst
err := ReadConfig()
if err != nil {
return fmt.Errorf("read config failed: %v", err)
}
client = alistsdk.NewClient(conf.Endpoint, conf.Username, conf.Password, conf.SkipVerify, 30)
u, err := client.Login()
if err != nil {
return fmt.Errorf("login failed: %v", err)
}
logger.Infof("login success: %v", u.Username)
src := c.Args().Get(0)
dst := c.Args().Get(1)
if !CheckPathIsExist(src) {
return fmt.Errorf("path %s not exist", src)
}
if !CheckPathIsFolder(dst) {
return fmt.Errorf("path %s is not a folder", dst)
}
//split src path to folder and filename
pathParts := strings.Split(src, "/")
folder := "/" + strings.Join(pathParts[:len(pathParts)-1], "/")
filename := pathParts[len(pathParts)-1]
err = client.Move(folder, dst, []string{filename})
if err != nil {
return fmt.Errorf("move %s to %s failed: %v", src, dst, err)
}
logger.Infof("move %s to %s success", src, dst)
return nil
},
},
{ // copy
Name: "copy",
Aliases: []string{"cp"},
Usage: "copy file from src to dst, can cross mount point",
UsageText: fmt.Sprintf(`%s copy [-v] src-path dst-folder`, app.Name),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "verbose mode, show more infomation",
Value: false,
Action: func(c *cli.Context, v bool) error {
if v {
logger.SetLevel(logrus.InfoLevel)
}
return nil
},
},
},
Action: func(c *cli.Context) error {
//TODO: copy file from src to dst
err := ReadConfig()
if err != nil {
return fmt.Errorf("read config failed: %v", err)
}
client = alistsdk.NewClient(conf.Endpoint, conf.Username, conf.Password, conf.SkipVerify, 30)
u, err := client.Login()
if err != nil {
return fmt.Errorf("login failed: %v", err)
}
logger.Infof("login success: %v", u.Username)
src := c.Args().Get(0)
dst := c.Args().Get(1)
dst = strings.TrimSuffix(dst, "/")
if !CheckPathIsExist(src) {
return fmt.Errorf("path %s not exist", src)
}
pathParts := strings.Split(src, "/")
folder := "/" + strings.Join(pathParts[:len(pathParts)-1], "/")
filename := pathParts[len(pathParts)-1]
if CheckPathIsExist(dst + "/" + filename) {
return fmt.Errorf("dst path %s exist", dst+"/"+filename)
}
if !CheckPathIsFolder(dst) {
return fmt.Errorf("path %s is not a folder", dst)
}
err = client.Copy(folder, dst, []string{filename})
if err != nil {
return fmt.Errorf("copy %s to %s failed: %v", src, dst, err)
}
logger.Infof("copy %s to %s mission submit success", src, dst)
return nil
},
},
{ // list
Name: "list",
Aliases: []string{"ls"},
Usage: "list all files in path",
UsageText: fmt.Sprintf(`%s list [-v] path`, app.Name),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "verbose mode, show more infomation",
Value: false,
Action: func(c *cli.Context, v bool) error {
if v {
logger.SetLevel(logrus.InfoLevel)
}
return nil
},
},
},
Action: func(c *cli.Context) error {
//TODO: list all files in path
err := ReadConfig()
if err != nil {
return fmt.Errorf("read config failed: %v", err)
}
client = alistsdk.NewClient(conf.Endpoint, conf.Username, conf.Password, conf.SkipVerify, 30)
u, err := client.Login()
if err != nil {
return fmt.Errorf("login failed: %v", err)
}
logger.Infof("login success: %v", u.Username)
path := c.Args().Get(0)
files, err := client.List(path, "", 0, 0, true)
if err != nil {
return fmt.Errorf("list %s failed: %v", path, err)
}
fmt.Printf("Files in %s:\n", path)
for _, f := range files {
fmt.Printf("%-30.30s\t%6.6s\n", f.Name, func() string {
if f.IsDir {
return "folder"
}
return "file"
}())
}
logger.Infof("list %s success, get %d files", path, len(files))
return nil
},
},
{ // delete
Name: "delete",
Aliases: []string{"rm"},
Usage: "delete file from path",
UsageText: fmt.Sprintf(`%s delete [-v] path`, app.Name),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "verbose mode, show more infomation",
Value: false,
Action: func(c *cli.Context, v bool) error {
if v {
logger.SetLevel(logrus.InfoLevel)
}
return nil
},
},
},
Action: func(c *cli.Context) error {
//TODO: delete file from path
err := ReadConfig()
if err != nil {
return fmt.Errorf("read config failed: %v", err)
}
client = alistsdk.NewClient(conf.Endpoint, conf.Username, conf.Password, conf.SkipVerify, 30)
u, err := client.Login()
if err != nil {
return fmt.Errorf("login failed: %v", err)
}
logger.Infof("login success: %v", u.Username)
path := c.Args().Get(0)
if !CheckPathIsExist(path) {
return fmt.Errorf("path %s not exist", path)
}
pathParts := strings.Split(path, "/")
folder := "/" + strings.Join(pathParts[:len(pathParts)-1], "/")
filename := pathParts[len(pathParts)-1]
err = client.Remove(folder, []string{filename})
if err != nil {
return fmt.Errorf("delete %s failed: %v", path, err)
}
logger.Infof("delete %s success", path)
return nil
},
},
{ // upload
Name: "upload",
Usage: "upload file to path, imcomplete not supported yet",
UsageText: fmt.Sprintf(`%s upload [-v] src-file dst-folder`, app.Name),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "path to upload",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "verbose mode, show more infomation",
Value: false,
Action: func(c *cli.Context, v bool) error {
if v {
logger.SetLevel(logrus.InfoLevel)
}
return nil
},
},
},
Action: func(c *cli.Context) error {
//TODO: upload file to path
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
logger.Error(err)
}
}
func ReadConfig() error {
conf = &Config{}
// read config file from config.json or config.yaml, prefer config.yaml
if _, err := os.Stat("config.yaml"); err == nil {
logger.Info("read config from config.yaml")
byts, err := os.ReadFile("config.yaml")
if err != nil {
return err
}
return yaml.Unmarshal(byts, conf)
} else if os.IsNotExist(err) {
logger.Info("read config from config.json")
byts, err := os.ReadFile("config.json")
if err != nil {
return err
}
return json.Unmarshal(byts, conf)
} else {
return err
}
}
func CheckPathIsFolder(path string) bool {
f, e := client.Get(path, "")
if e != nil {
logger.Errorf("get %s info failed with error: %v", path, e)
return false
}
if !f.IsDir {
logger.Errorf("%s is not a directory", path)
return false
}
return true
}
func CheckPathIsExist(path string) bool {
_, e := client.Get(path, "")
if e != nil {
logger.Errorf("get %s info failed with error: %v", path, e)
return false
}
return true
}