/
main.go
120 lines (104 loc) · 3.15 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
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
"github.com/manifoldco/go-manifold"
"github.com/manifoldco/go-manifold/names"
"github.com/manifoldco/manifold-cli/config"
"github.com/manifoldco/manifold-cli/plugins"
"github.com/manifoldco/manifold-cli/prompts"
)
var cmds []cli.Command
func main() {
cli.VersionPrinter = func(ctx *cli.Context) {
versionLookup(ctx)
}
app := cli.NewApp()
app.Name = "manifold"
app.HelpName = "manifold"
app.Usage = "A tool making it easy to buy, manage, and integrate developer services into an application."
app.Version = config.Version
app.Commands = append(cmds, helpCommand)
app.Flags = append(app.Flags, cli.HelpFlag)
app.EnableBashCompletion = true
app.Action = func(cliCtx *cli.Context) error {
// Show help if no arguments passed
if len(os.Args) < 2 {
cli.ShowAppHelp(cliCtx)
return nil
}
// Execute plugin if installed
cmd := os.Args[1]
success, err := plugins.Run(cmd)
if err != nil {
return cli.NewExitError("Plugin error: "+err.Error(), -1)
}
if success {
return nil
}
// Otherwise display global help
cli.ShowAppHelp(cliCtx)
fmt.Println("\nIf you were attempting to use a plugin, it may not be installed.")
return nil
}
app.Run(os.Args)
}
// copied from urfave/cli so we can set the category
var helpCommand = cli.Command{
Name: "help",
Usage: "Shows a list of commands or help for one command",
Category: "UTILITY",
ArgsUsage: "[command]",
Action: func(c *cli.Context) error {
args := c.Args()
if args.Present() {
return cli.ShowCommandHelp(c, args.First())
}
cli.ShowAppHelp(c)
return nil
},
}
// promptName encapsulates the logic for accepting a name as the first
// positional argument (optionally), and returning the appropriate value
func promptNameForResource(ctx *cli.Context, optionalID *manifold.ID,
product manifold.Label) (string, error) {
// The user may supply a name value as the first positional arg
argName, err := optionalArgName(ctx, 0, "name")
if err != nil {
return "", err
}
shouldAccept := true
if optionalID != nil {
name := names.ForResource(product, *optionalID)
if argName == "" {
argName = string(name)
shouldAccept = false
}
}
// If no name value is supplied, prompt for it
// otherwise validate the given value
shouldAcceptName := shouldAccept && argName != ""
return prompts.Name("resource", argName, shouldAcceptName, true)
}
// promptName encapsulates the logic for accepting a name as the first
// positional argument (optionally), and returning the appropriate value
func promptName(ctx *cli.Context, optionalID *manifold.ID, objectName string, allowEmpty bool) (string, error) {
// The user may supply a name value as the first positional arg
argName, err := optionalArgName(ctx, 0, "name")
if err != nil {
return "", err
}
shouldAccept := true
if optionalID != nil {
_, name := names.New(*optionalID)
if argName == "" {
argName = string(name)
shouldAccept = false
}
}
// If no name value is supplied, prompt for it
// otherwise validate the given value
shouldAcceptName := shouldAccept && argName != ""
return prompts.Name(objectName, argName, shouldAcceptName, allowEmpty)
}