-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
322 lines (318 loc) · 8.87 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
package main
import (
"fmt"
"log"
"math"
"os"
"runtime"
"sort"
"strings"
gettext "github.com/chai2010/gettext-go"
"github.com/haokunt/filetools/tools"
"github.com/urfave/cli"
)
var version = "MISSING build version [git hash]"
func main() {
localZipBytes := MustAsset("local.zip")
gettext.BindLocale(gettext.New("filetools", "local.zip", localZipBytes))
gettext.SetDomain("filetools")
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
app := cli.NewApp()
app.Name = "filetools"
app.Usage = gettext.Gettext("some tools about file")
app.Version = fmt.Sprintf("Git:[%s] (%s)", strings.ToUpper(version), runtime.Version())
app.Commands = []cli.Command{
{
Name: "compare",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("compare two files or directory"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "src, s",
Usage: gettext.Gettext("source `file`"),
Value: "./",
},
cli.StringFlag{
Name: "dst, d",
Usage: gettext.Gettext("destination `file`"),
Value: "./",
},
cli.BoolFlag{
Name: "content, c",
Usage: gettext.Gettext("whether compare content or not"),
},
cli.BoolFlag{
Name: "verbose, v",
Usage: gettext.Gettext("print verbose output"),
},
},
Action: func(c *cli.Context) error {
Options := tools.CompareOptions{
Src: c.String("src"),
Dst: c.String("dst"),
Content: c.Bool("content"),
Verbose: c.Bool("verbose"),
}
if err := tools.Compare(&Options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Compare: %s", err), -1)
}
return nil
},
},
{
Name: "info",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("Show file information"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: gettext.Gettext("the `filename`"),
Required: true,
},
cli.BoolFlag{
Name: "crypto, c",
Usage: gettext.Gettext("use crypto to calculate the md5, sha1 and, sha256, this flag only works when the file is not directory"),
},
},
Action: func(c *cli.Context) error {
Options := tools.InfoOptions{
FileName: c.String("file"),
Crypto: c.Bool("crypto"),
}
if err := tools.Info(&Options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Info: %s", err), -1)
}
return nil
},
},
{
Name: "copy",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("copy a file or directory"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "src, s",
Usage: gettext.Gettext("source `file`"),
Required: true,
},
cli.StringFlag{
Name: "dst, d",
Usage: gettext.Gettext("destination `file`"),
Required: true,
},
cli.BoolFlag{
Name: "recursive, r",
Usage: gettext.Gettext("Recursive copy"),
},
cli.BoolFlag{
Name: "createDir, c",
Usage: gettext.Gettext("Create dst directory if it doesn't exist"),
},
cli.BoolFlag{
Name: "verbose, v",
Usage: gettext.Gettext("print verbose output"),
},
cli.BoolFlag{
Name: "pbar, pb",
Usage: gettext.Gettext("Whether show progress bar, if true, it will calculate the size of directory, it can only be used with no verbose flag"),
},
},
Action: func(c *cli.Context) error {
Options := tools.CopyOptions{
Src: c.String("src"),
Dst: c.String("dst"),
IsDir: c.Bool("recursive"),
CreateDir: c.Bool("createDir"),
Verbose: c.Bool("verbose"),
ProgressBar: c.Bool("pbar"),
}
if err := tools.Copy(&Options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Copy: %s", err), -1)
}
return nil
},
},
{
Name: "rename",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("Rename a file"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "src, s",
Usage: gettext.Gettext("source `file`"),
Required: true,
},
cli.StringFlag{
Name: "dst, d",
Usage: gettext.Gettext("destination `file`"),
Required: true,
},
cli.BoolFlag{
Name: "keep-original, k",
Usage: gettext.Gettext("keep original file"),
},
},
Action: func(c *cli.Context) error {
Options := tools.RenameOptions{
Src: c.String("src"),
Dst: c.String("dst"),
KeepOriginal: c.Bool("keep-original"),
}
if err := tools.Rename(&Options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Rename: %s", err), -1)
}
return nil
},
},
{
Name: "list",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("list all files in a directory"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "directory, d",
Usage: gettext.Gettext("the directory `path`"),
Required: true,
},
cli.StringSliceFlag{
Name: "sortby, s",
Usage: gettext.Gettext("sort the files by some fields, now supported: name, size, " +
"you can use it like this, --sortby name:desc --sortby size:asc, " +
"desc means descending, asc means ascending, desc and asc can be write in short name 'a' and 'd'." +
"the sort field appears first will be the main field, and so on. " +
"if sort, the command will calculate first, so the performance will be slower."),
},
cli.IntFlag{
Name: "limit, l",
Usage: gettext.Gettext("limit the `number` of files to show"),
Value: math.MaxInt32,
},
},
Action: func(c *cli.Context) error {
Options := tools.ListOptions{
Path: c.String("directory"),
SortBy: c.StringSlice("sortby"),
Limit: c.Int("limit"),
}
if err := tools.List(&Options); err != nil {
return cli.NewExitError(fmt.Errorf("Error List: %s", err), -1)
}
return nil
},
},
{
Name: "delete",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("delete a file or directory, it will move file to the ~/.filetools-trash default"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: gettext.Gettext("the `file` or directory"),
Required: true,
},
cli.BoolFlag{
Name: "hard, hd",
Usage: gettext.Gettext("hard delete, it will delete the directory directly"),
},
},
Action: func(c *cli.Context) error {
Options := tools.DeleteOptions{
Path: c.String("file"),
HardDelete: c.Bool("hard"),
}
if err := tools.Delete(&Options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Delete: %s", err), -1)
}
return nil
},
},
{
Name: "move",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("move a file or directory, it works like `mv`"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "source, s",
Usage: gettext.Gettext("source `file`"),
Required: true,
},
cli.StringFlag{
Name: "destination, d",
Usage: gettext.Gettext("destination `file`"),
Required: true,
},
},
Action: func(c *cli.Context) error {
options := tools.MoveOptions{
Src: c.String("source"),
Dst: c.String("destination"),
}
if err := tools.Move(&options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Move: %s", err), -1)
}
return nil
},
},
{
Name: "server",
Category: gettext.Gettext("General"),
Usage: gettext.Gettext("Using a Simple HTTP Server to download or upload files"),
Flags: []cli.Flag{
cli.StringFlag{
Name: "directory, d",
Usage: gettext.Gettext("the directory to be served"),
Value: pwd,
},
cli.StringFlag{
Name: "host",
Usage: gettext.Gettext("the host you want to use"),
},
cli.IntFlag{
Name: "port, p",
Usage: gettext.Gettext("port"),
Value: 9000,
},
cli.StringFlag{
Name: "user, u",
Usage: gettext.Gettext("username of the server"),
},
cli.StringFlag{
Name: "password, pw",
Usage: gettext.Gettext("password of the server, if you not specify a password and username, The server will not use basic authentication"),
},
cli.StringFlag{
Name: "upload-dir, o",
Usage: gettext.Gettext("upload directory, default is the same as root directory"),
},
},
Action: func(c *cli.Context) error {
var uploadDir = c.String("upload-dir")
if uploadDir == "" {
uploadDir = c.String("directory")
}
options := tools.ServerOptions{
Host: c.String("host"),
Port: c.Int("port"),
RootPath: c.String("directory"),
UploadDir: uploadDir,
User: c.String("user"),
Password: c.String("password"),
}
if err := tools.Server(&options); err != nil {
return cli.NewExitError(fmt.Errorf("Error Server: %s", err), -1)
}
return nil
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}