-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (70 loc) · 1.54 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
package main
import (
"fmt"
"log"
"os"
"github.com/mys1024/imgi/v2/internal"
"github.com/urfave/cli/v2"
"golang.design/x/clipboard"
)
func main() {
app := &cli.App{
Name: "imgi",
Usage: "prints images' information",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Value: "yaml",
Usage: "output format, available value: \"yaml\", \"toml\", \"json\"",
Aliases: []string{"f"},
},
&cli.BoolFlag{
Name: "copy",
Value: false,
Usage: "copy output to clipboard",
Aliases: []string{"c"},
},
},
Action: func(cCtx *cli.Context) error {
// args
dir := "./"
if cCtx.NArg() == 1 {
dir = cCtx.Args().Get(0)
} else if cCtx.NArg() >= 2 {
return fmt.Errorf(fmt.Sprintf("Expected 0-1 arguments, but got %v", cCtx.NArg()))
}
// scan specified dir
scanResult, err := internal.Scan(dir)
if err != nil {
return err
}
// format output
format := cCtx.String("format")
formatter := internal.Yaml
switch format {
case "yaml":
formatter = internal.Yaml
case "toml":
formatter = internal.Toml
case "json":
formatter = internal.Json
default:
return fmt.Errorf(fmt.Sprintf("Wrong flag (format) value: %v", format))
}
output, err := formatter(scanResult)
if err != nil {
return err
}
// copy output to clipboard
if cCtx.Bool("copy") {
clipboard.Write(clipboard.FmtText, []byte(output))
}
// print output
fmt.Println(output)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}