forked from rackspace/rack
/
main.go
138 lines (126 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
136
137
138
package main
import (
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"text/template"
"github.com/jrperritt/rack/commands/blockstoragecommands"
"github.com/jrperritt/rack/commands/filescommands"
"github.com/jrperritt/rack/commands/networkscommands"
"github.com/jrperritt/rack/commands/serverscommands"
"github.com/jrperritt/rack/util"
"github.com/codegangsta/cli"
)
func main() {
cli.HelpPrinter = printHelp
cli.CommandHelpTemplate = `NAME: {{.Name}} - {{.Usage}}{{if .Description}}
DESCRIPTION: {{.Description}}{{end}}{{if .Flags}}
OPTIONS:
{{range .Flags}}{{flag .}}
{{end}}{{ end }}
`
app := cli.NewApp()
app.Name = "rack"
app.Usage = "An opinionated CLI for the Rackspace cloud"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "configure",
Usage: "Used to interactively create a config file for Rackspace authentication.",
Action: configure,
},
{
Name: "servers",
Usage: "Used for the Servers service",
Subcommands: serverscommands.Get(),
},
{
Name: "files",
Usage: "Used for the Files service",
Subcommands: filescommands.Get(),
},
{
Name: "networks",
Usage: "Used for the Networks service",
Subcommands: networkscommands.Get(),
},
{
Name: "block-storage",
Usage: "Used for the Block Storage service",
Subcommands: blockstoragecommands.Get(),
},
}
app.Flags = util.GlobalFlags()
app.BashComplete = func(c *cli.Context) {
completeGlobals(globalOptions(app))
}
app.Before = func(c *cli.Context) error {
//fmt.Printf("c.Args: %+v\n", c.Args())
return nil
}
app.CommandNotFound = commandNotFound
app.Run(os.Args)
}
// completeGlobals returns the options for completing global flags.
func completeGlobals(vals []interface{}) {
for _, val := range vals {
switch val.(type) {
case cli.StringFlag:
fmt.Println("--" + val.(cli.StringFlag).Name)
case cli.IntFlag:
fmt.Println("--" + val.(cli.IntFlag).Name)
case cli.BoolFlag:
fmt.Println("--" + val.(cli.BoolFlag).Name)
case cli.Command:
fmt.Println(val.(cli.Command).Name)
default:
continue
}
}
}
// globalOptions returns the options (flags and commands) that can be used after
// `rack` in a command. For example, `rack --json`, `rack servers`, and
// `rack --json servers` are all legitimate command prefixes.
func globalOptions(app *cli.App) []interface{} {
var i []interface{}
globalFlags := util.GlobalFlags()
for _, globalFlag := range globalFlags {
i = append(i, globalFlag)
}
for _, cmd := range app.Commands {
i = append(i, cmd)
}
return i
}
func printHelp(out io.Writer, templ string, data interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
"flag": flag,
}
w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
err := t.Execute(w, data)
if err != nil {
panic(err)
}
w.Flush()
}
func flag(flag cli.Flag) string {
switch flag.(type) {
case cli.StringFlag:
flagType := flag.(cli.StringFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
case cli.IntFlag:
flagType := flag.(cli.IntFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
case cli.BoolFlag:
flagType := flag.(cli.BoolFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
case cli.StringSliceFlag:
flagType := flag.(cli.StringSliceFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
}
return ""
}