forked from iron-io/functions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
135 lines (115 loc) · 3.42 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
package main
import (
"bytes"
"fmt"
"os"
"strings"
vers "github.com/iron-io/functions/api/version"
"github.com/iron-io/functions/fn/commands"
image_commands "github.com/iron-io/functions/fn/commands/images"
"github.com/iron-io/functions/fn/common"
"github.com/urfave/cli"
)
var aliases = map[string]cli.Command{
"build": image_commands.Build(),
"bump": image_commands.Bump(),
"deploy": image_commands.Deploy(),
"push": image_commands.Push(),
"run": image_commands.Run(),
"call": commands.Call(),
}
func init() {
if os.Getenv("API_URL") != "" {
common.API_URL = os.Getenv("API_URL")
}
common.BASE_PATH = common.GetBasePath(common.API_VERSION)
}
func main() {
app := newFn()
app.Run(os.Args)
}
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 = vers.Version
app.Authors = []cli.Author{{Name: "iron.io"}}
app.Description = "IronFunctions command line tools"
app.UsageText = `Check the manual at https://github.com/iron-io/functions/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 - IronFunctions 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{
commands.InitFn(),
commands.Apps(),
commands.Routes(),
commands.Images(),
commands.Lambda(),
commands.Version(),
}
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
}
}