forked from fnproject/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
146 lines (126 loc) · 3.82 KB
/
main.go
File metadata and controls
146 lines (126 loc) · 3.82 KB
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
package main
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"github.com/fnproject/cli/commands"
"github.com/fnproject/cli/config"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
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.EnableBashCompletion = true
app.Before = func(c *cli.Context) error {
err := config.LoadConfiguration(c)
if err != nil {
return err
}
commandArgOverrides(c)
return nil
}
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.StringFlag{
Name: "context",
Usage: "Use --context to select context configuration file",
},
cli.StringFlag{
Name: "registry",
Usage: "Use --registry to select registry",
},
}
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)
fmt.Fprintf(os.Stderr, "Note: the fn CLI command structure has changed, change your command to use the new structure.\n")
}
app.Commands = append(app.Commands, commands.GetCommands(commands.Commands)...)
app.Commands = append(app.Commands, VersionCommand())
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
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 init() {
err := config.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}
func commandArgOverrides(c *cli.Context) {
if registry := c.String(config.EnvFnRegistry); registry != "" {
viper.Set(config.EnvFnRegistry, registry)
}
}
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)
fmt.Fprintf(os.Stderr, "Client version: %s\n", Version)
os.Exit(1)
}
}