forked from fnproject/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (122 loc) · 3.28 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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/urfave/cli"
)
var aliases = map[string]cli.Command{
"build": build(),
"bump": bump(),
"deploy": deploy(),
"push": push(),
"run": run(),
"call": call(),
"calls": calls(),
"logs": logs(),
}
func aliasesFn() []cli.Command {
cmds := []cli.Command{}
for alias, cmd := range aliases {
cmd.Name = alias
cmd.Hidden = true
cmds = append(cmds, cmd)
}
return cmds
}
func newFn() *cli.App {
app := cli.NewApp()
app.Name = "fn"
app.Version = Version
app.Authors = []cli.Author{{Name: "Fn Project"}}
app.Description = "Fn command line tool"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose,v", // v is taken for version by default with urfave/cli
Usage: "Use `--verbose` to enable verbose mode for debugging",
},
}
cli.VersionFlag = cli.BoolFlag{
Name: "version",
Usage: "print only the version",
}
cli.AppHelpTemplate = `{{.Name}} {{.Version}}{{if .Description}}
{{.Description}}{{end}}
ENVIRONMENT VARIABLES:
FN_API_URL - Fn server address
FN_REGISTRY - Docker registry to push images to, use username only to push to Docker Hub - [[registry.hub.docker.com/]USERNAME]{{if .VisibleCommands}}
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{end}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}{{end}}
LEARN MORE:
https://github.com/fnproject/fn
`
app.CommandNotFound = func(c *cli.Context, cmd string) {
fmt.Fprintf(os.Stderr, "Command not found: \"%v\" -- see `fn --help` for more information.\n", cmd)
}
app.Commands = []cli.Command{
startCmd(),
updateCmd(),
initFn(),
apps(),
routes(),
images(),
lambda(),
version(),
calls(),
deploy(),
logs(),
testfn(),
buildServer(),
}
app.Commands = append(app.Commands, aliasesFn()...)
prepareCmdArgsValidation(app.Commands)
return app
}
func parseArgs(c *cli.Context) ([]string, []string) {
args := strings.Split(c.Command.ArgsUsage, " ")
var reqArgs []string
var optArgs []string
for _, arg := range args {
if strings.HasPrefix(arg, "[") {
optArgs = append(optArgs, arg)
} else if strings.Trim(arg, " ") != "" {
reqArgs = append(reqArgs, arg)
}
}
return reqArgs, optArgs
}
func prepareCmdArgsValidation(cmds []cli.Command) {
// TODO: refactor fn to use urfave/cli.v2
// v1 doesn't let us validate args before the cmd.Action
for i, cmd := range cmds {
prepareCmdArgsValidation(cmd.Subcommands)
if cmd.Action == nil {
continue
}
action := cmd.Action
cmd.Action = func(c *cli.Context) error {
reqArgs, _ := parseArgs(c)
if c.NArg() < len(reqArgs) {
var help bytes.Buffer
cli.HelpPrinter(&help, cli.CommandHelpTemplate, c.Command)
return fmt.Errorf("Missing required arguments: %s", strings.Join(reqArgs[c.NArg():], " "))
}
return cli.HandleAction(action, c)
}
cmds[i] = cmd
}
}
func main() {
app := newFn()
err := app.Run(os.Args)
if err != nil {
// TODO: this doesn't seem to get called even when an error returns from a command, but maybe urfave is doing a non zero exit anyways? nope: https://github.com/urfave/cli/issues/610
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}