-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
154 lines (132 loc) · 3.63 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
package main
import (
"bytes"
"fmt"
"net/url"
"os"
"strings"
functions "github.com/funcy/functions_go"
"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: "Oracle Corporation"}}
app.Description = "Fn command line tool"
app.UsageText = `Check the docs at https://github.com/fnproject/fn/blob/master/fn/README.md`
cli.AppHelpTemplate = `{{.Name}} {{.Version}}{{if .Description}}
{{.Description}}{{end}}
USAGE:
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
ENVIRONMENT VARIABLES:
API_URL - Oracle Functions remote API address{{if .VisibleCommands}}
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{end}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
ALIASES:
build (images build)
bump (images bump)
deploy (images deploy)
run (images run)
call (routes call)
push (images push)
GLOBAL OPTIONS:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}{{end}}
`
app.CommandNotFound = func(c *cli.Context, cmd string) {
fmt.Fprintf(os.Stderr, "command not found: %v\n", cmd)
}
app.Commands = []cli.Command{
startCmd(),
updateCmd(),
initFn(),
apps(),
routes(),
images(),
lambda(),
version(),
calls(),
logs(),
testfn(),
}
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("ERROR: Missing required arguments: %s\n\n%s", strings.Join(reqArgs[c.NArg():], " "), help.String())
}
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 occurred: %v, exiting...\n", err)
os.Exit(1)
}
}
func resetBasePath(c *functions.Configuration) error {
apiURL := os.Getenv("API_URL")
if apiURL == "" {
apiURL = "http://localhost:8080"
}
u, err := url.Parse(apiURL)
if err != nil {
return err
}
u.Path = "/v1"
c.BasePath = u.String()
return nil
}