-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
141 lines (128 loc) · 3.42 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
package main
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"github.com/creativeprojects/go-selfupdate"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const Version = "0.1.1"
const DefaultPort = 5731
type CLIParseResult struct {
Port int
Entries []string
Verbose bool
PublicIPOnly bool
}
func update(version string) error {
log.Debug("start update")
latest, found, err := selfupdate.DetectLatest(context.Background(), selfupdate.ParseSlug("fpfeng/quickurl"))
if err != nil {
return fmt.Errorf("error occurred while detecting version: %w", err)
}
if !found {
return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
}
if latest.LessOrEqual(version) {
fmt.Printf("current version (%s) is the latest\n", version)
return nil
}
exe, err := os.Executable()
if err != nil {
return errors.New("could not locate executable path")
}
if err := selfupdate.UpdateTo(context.Background(), latest.AssetURL, latest.AssetName, exe); err != nil {
return fmt.Errorf("error occurred while updating binary: %w", err)
}
fmt.Printf("successfully updated to version %s\n", latest.Version())
return nil
}
func StartCLI(cliArgs []string) *CLIParseResult {
cli.AppHelpTemplate = `
NAME:
{{.Name}} - {{.Usage}}
USAGE:
quickurl /path/to/file1 /path/to/folder1
quickurl -s /path/to/file1 -s /path/to/folder1 -p 8080
{{if .Commands}}
OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}`
result := CLIParseResult{Port: 0, Entries: make([]string, 0)}
app := &cli.App{
Name: fmt.Sprintf("QuickURL %v", Version),
Usage: "Sharing file with http instantly",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "p",
Usage: "listening `port number`",
Value: DefaultPort,
Destination: &result.Port,
},
&cli.StringSliceFlag{
Name: "s",
Usage: "serving `path`, -s /path/to/file1 -s /path/to/folder",
TakesFile: true,
},
&cli.BoolFlag{
Name: "v",
Usage: "verbose log",
Destination: &result.Verbose,
Value: false,
},
&cli.BoolFlag{
Name: "public-ip",
Usage: "print out public ip from external API only",
Destination: &result.PublicIPOnly,
Value: false,
},
&cli.BoolFlag{
Name: "update",
Usage: "update to latest release version",
Value: false,
},
},
Action: func(cCtx *cli.Context) error {
if cCtx.Bool("v") {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
log.SetOutput(os.Stdout)
}
if cCtx.Bool("help") {
cli.ShowAppHelp(cCtx)
return nil
}
if cCtx.Bool("update") {
if err := update(Version); err != nil {
log.Fatalf("update failed: %v", err)
}
return nil
}
rawArgs := cCtx.Args().Slice()
log.Debugf("args: %v", rawArgs)
servingArgfiles := cCtx.StringSlice("s")
simpleModeFiles := make([]string, 0)
for _, arg := range rawArgs { // executed as quickurl file1 file2
if _, err := os.Stat(arg); err != nil {
log.Fatal(err)
}
simpleModeFiles = append(simpleModeFiles, arg)
}
if len(simpleModeFiles) > 0 {
result.Entries = append(result.Entries, simpleModeFiles...)
} else if len(servingArgfiles) > 0 {
result.Entries = append(result.Entries, servingArgfiles...)
} else {
cli.ShowAppHelp(cCtx)
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
return &result
}