forked from phartz/service-broker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb.go
103 lines (87 loc) · 3.01 KB
/
sb.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
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/phartz/service-broker-cli/sbcli"
)
// Slice which stores the registered commands
var sbcommands []sbcli.SBCommand = make([]sbcli.SBCommand, 0)
// Variables to identiy the build
var (
Version string
Build string
BuildTime string
)
// main function
func main() {
// Check trace
if os.Getenv("SB_TRACE") == "ON" {
fmt.Println("Trace is activated...")
fmt.Println()
}
// register all commands
registerCommands()
if len(os.Args) == 1 {
usage()
return
}
c := sbcli.NewCommandline(os.Args)
for _, command := range sbcommands {
if command.Name == c.Command || command.Shortcut == c.Command {
command.Function(c)
return
}
}
usage()
}
func registerCommands() {
// register commands
addCommand("help", "h", "Show help", Help)
addCommand("", "", "", nil)
addCommand("api", "", "Sets or gets the api", sbcli.Api)
addCommand("login", "l", "Login to the target", sbcli.Login)
addCommand("logout", "lo", "Logout from the target", sbcli.Logout)
addCommand("auth", "", "Authenticate to the target", sbcli.Auth)
addCommand("version", "-v", "Print the version", ShowVersion)
addCommand("", "", "", nil)
addCommand("marketplace", "m", "List available offerings in the marketplace", sbcli.Marketplace)
addCommand("services", "s", "List all service instances in the target space", sbcli.Services)
addCommand("service", "", "Show service instance info", sbcli.Service)
addCommand("find-service", "fs", "Lookup service with a given deployment name", sbcli.FindService)
addCommand("", "", "", nil)
addCommand("create-service", "cs", "Create a service instance", sbcli.CreateService)
addCommand("update-service", "", "Update a service instance", sbcli.UpdateService)
addCommand("delete-service", "ds", "Delete a service instance", sbcli.DeleteService)
addCommand("", "", "", nil)
addCommand("create-service-key", "csk", "Create key for a service instance", sbcli.CreateServiceKey)
addCommand("service-keys", "sk", "List keys for a service instance", sbcli.ServiceKeys)
addCommand("delete-service-key", "dsk", "Delete a service key", sbcli.DeleteServiceKey)
}
// fnuction to add the commands into the command slice
func addCommand(name string, shortcut string, helptext string, function sbcli.SBFunction) {
sbcommands = append(sbcommands, sbcli.SBCommand{Name: name, Shortcut: shortcut, Helptext: helptext, Function: function})
}
func Help(cmd *sbcli.Commandline) {
if len(cmd.Options) == 0 {
usage()
return
}
fmt.Println(sbcli.GetHelpText(cmd.Options[0]))
}
// prints the usage text
func usage() {
fmt.Println(sbcli.UsageText)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintf(w, "command\tshortcut\tdescription\n")
for _, command := range sbcommands {
fmt.Fprintf(w, "%s\t%s\t%s\n", command.Name, command.Shortcut, command.Helptext)
}
w.Flush()
}
func ShowVersion(cmd *sbcli.Commandline) {
fmt.Printf("sb version %s (%s)\n", getVersionString(), BuildTime)
}
func getVersionString() string {
return fmt.Sprintf("%s+%s", Version, Build)
}