Skip to content

Commit ca0bab1

Browse files
fix(client-go): pass arguments when using root command to list
1 parent ea94180 commit ca0bab1

18 files changed

Lines changed: 301 additions & 263 deletions

client-go/deis.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,19 @@ Shortcut commands, use 'deis shortcuts' to see all::
6262
Use 'git push deis master' to deploy to an application.
6363
`
6464
// Reorganize some command line flags and commands.
65-
argv = parseArgs(argv)
65+
command, argv := parseArgs(argv)
6666
// Give docopt an optional final false arg so it doesn't call os.Exit().
67-
args, err := docopt.Parse(usage, argv, false, version.Version, true, false)
67+
_, err := docopt.Parse(usage, []string{command}, false, version.Version, true, false)
6868

6969
if err != nil {
7070
fmt.Println(err)
7171
return 1
7272
}
7373

74-
if len(args) == 0 {
74+
if len(argv) == 0 {
7575
return 0
7676
}
7777

78-
command := args["<command>"]
7978
// Dispatch the command, passing the argv through so subcommands can
8079
// re-parse it according to their usage strings.
8180
switch command {
@@ -110,18 +109,27 @@ Use 'git push deis master' to deploy to an application.
110109
case "help":
111110
fmt.Print(usage)
112111
return 0
112+
case "--version":
113+
return 0
113114
default:
114115
env := os.Environ()
115-
command = "deis-" + argv[0]
116+
extCmd := "deis-" + command
116117

117-
binary, err := exec.LookPath(command.(string))
118+
binary, err := exec.LookPath(extCmd)
118119
if err != nil {
119120
parser.PrintUsage()
120121
return 1
121122
}
122123

123-
cmdArgv := []string{command.(string)}
124-
cmdArgv = append(cmdArgv, argv[1:]...)
124+
cmdArgv := []string{extCmd}
125+
126+
cmdSplit := strings.Split(argv[0], command+":")
127+
128+
if len(cmdSplit) > 1 {
129+
argv[0] = cmdSplit[1]
130+
}
131+
132+
cmdArgv = append(cmdArgv, argv...)
125133

126134
err = syscall.Exec(binary, cmdArgv, env)
127135
if err != nil {
@@ -138,35 +146,35 @@ Use 'git push deis master' to deploy to an application.
138146

139147
// parseArgs returns the provided args with "--help" as the last arg if need be,
140148
// expands shortcuts and formats commands to be properly routed.
141-
func parseArgs(argv []string) []string {
149+
func parseArgs(argv []string) (string, []string) {
142150
if len(argv) == 1 {
143-
// rearrange "deisctl --help" as "deisctl help"
151+
// rearrange "deis --help" as "deis help"
144152
if argv[0] == "--help" || argv[0] == "-h" {
145153
argv[0] = "help"
146154
}
147155
}
148156

149157
if len(argv) >= 2 {
150-
// Rearrange "deisctl help <command>" to "deisctl <command> --help".
158+
// Rearrange "deis help <command>" to "deis <command> --help".
151159
if argv[0] == "help" || argv[0] == "--help" || argv[0] == "-h" {
152160
argv = append(argv[1:], "--help")
153161
}
154162
}
155163

156-
// Split commands with colons [apps:create, ...] -> [apps, create, ...].
157164
if len(argv) > 0 {
158165
argv[0] = replaceShortcut(argv[0])
159166

160167
index := strings.Index(argv[0], ":")
161168

162169
if index != -1 {
163170
command := argv[0]
164-
argv[0] = command[:index]
165-
argv = append(argv[:1], append([]string{command[(index + 1):]}, argv[1:]...)...)
171+
return command[:index], argv
166172
}
173+
174+
return argv[0], argv
167175
}
168176

169-
return argv
177+
return "", argv
170178
}
171179

172180
func replaceShortcut(command string) string {

client-go/deis_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ func TestHelpReformatting(t *testing.T) {
1212
expected := "help"
1313

1414
for _, test := range tests {
15-
actual := parseArgs([]string{test})
15+
actual, argv := parseArgs([]string{test})
1616

17-
if actual[0] != expected {
18-
t.Errorf("Expected %s, Got %s", expected, actual[0])
17+
if actual != expected {
18+
t.Errorf("Expected %s, Got %s", expected, actual)
19+
}
20+
21+
if len(argv) != 1 {
22+
t.Errorf("Expected length of 1, Got %d", len(argv))
1923
}
2024
}
2125
}
@@ -24,25 +28,36 @@ func TestHelpReformattingWithCommand(t *testing.T) {
2428
t.Parallel()
2529

2630
tests := []string{"--help", "-h", "help"}
27-
expected := "--help"
31+
expected := "test"
32+
expectedArgv := []string{"test", "--help"}
2833

2934
for _, test := range tests {
30-
actual := parseArgs([]string{test, "test"})
35+
actual, argv := parseArgs([]string{test, "test"})
36+
37+
if actual != expected {
38+
t.Errorf("Expected %s, Got %s", expected, actual)
39+
}
3140

32-
if actual[1] != expected {
33-
t.Errorf("Expected %s, Got %s", expected, actual[1])
41+
if !reflect.DeepEqual(expectedArgv, argv) {
42+
t.Errorf("Expected %v, Got %v", expectedArgv, argv)
3443
}
3544
}
3645
}
3746

3847
func TestCommandSplitting(t *testing.T) {
3948
t.Parallel()
4049

41-
expected := []string{"apps", "create", "test", "foo"}
42-
actual := parseArgs([]string{"apps:create", "test", "foo"})
50+
expected := "apps"
51+
expectedArgv := []string{"apps:create", "test", "foo"}
52+
53+
actual, argv := parseArgs(expectedArgv)
54+
55+
if actual != expected {
56+
t.Errorf("Expected %s, Got %s", expected, actual)
57+
}
4358

44-
if !reflect.DeepEqual(expected, actual) {
45-
t.Errorf("Expected %v, Got %v", expected, actual)
59+
if !reflect.DeepEqual(expectedArgv, argv) {
60+
t.Errorf("Expected %v, Got %v", expectedArgv, argv)
4661
}
4762
}
4863

client-go/parser/apps.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package parser
22

33
import (
4-
"fmt"
54
"strconv"
65
"strings"
76

@@ -24,29 +23,32 @@ apps:destroy destroy an application
2423
2524
Use 'deis help [command]' to learn more.
2625
`
27-
if len(argv) < 2 {
28-
return appsList([]string{"apps:list"})
29-
}
3026

31-
switch argv[1] {
32-
case "create":
33-
return appCreate(combineCommand(argv))
34-
case "list":
35-
return appsList(combineCommand(argv))
36-
case "info":
37-
return appInfo(combineCommand(argv))
38-
case "open":
39-
return appOpen(combineCommand(argv))
40-
case "logs":
41-
return appLogs(combineCommand(argv))
42-
case "run":
43-
return appRun(combineCommand(argv))
44-
case "destroy":
45-
return appDestroy(combineCommand(argv))
46-
case "--help":
47-
fmt.Print(usage)
48-
return nil
27+
switch argv[0] {
28+
case "apps:create":
29+
return appCreate(argv)
30+
case "apps:list":
31+
return appsList(argv)
32+
case "apps:info":
33+
return appInfo(argv)
34+
case "apps:open":
35+
return appOpen(argv)
36+
case "apps:logs":
37+
return appLogs(argv)
38+
case "apps:run":
39+
return appRun(argv)
40+
case "apps:destroy":
41+
return appDestroy(argv)
4942
default:
43+
if printHelp(argv, usage) {
44+
return nil
45+
}
46+
47+
if argv[0] == "apps" {
48+
argv[0] = "apps:list"
49+
return appsList(argv)
50+
}
51+
5052
PrintUsage()
5153
return nil
5254
}

client-go/parser/auth.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,23 @@ auth:regenerate regenerate user tokens
2222
2323
Use 'deis help [command]' to learn more.
2424
`
25-
if len(argv) < 2 {
26-
return nil
27-
}
2825

29-
switch argv[1] {
30-
case "register":
31-
return authRegister(combineCommand(argv))
32-
case "login":
33-
return authLogin(combineCommand(argv))
34-
case "logout":
35-
return authLogout(combineCommand(argv))
36-
case "passwd":
37-
return authPasswd(combineCommand(argv))
38-
case "whoami":
39-
return authWhoami(combineCommand(argv))
40-
case "cancel":
41-
return authCancel(combineCommand(argv))
42-
case "regenerate":
43-
return authRegenerate(combineCommand(argv))
44-
case "--help":
26+
switch argv[0] {
27+
case "auth:register":
28+
return authRegister(argv)
29+
case "auth:login":
30+
return authLogin(argv)
31+
case "auth:logout":
32+
return authLogout(argv)
33+
case "auth:passwd":
34+
return authPasswd(argv)
35+
case "auth:whoami":
36+
return authWhoami(argv)
37+
case "auth:cancel":
38+
return authCancel(argv)
39+
case "auth:regenerate":
40+
return authRegenerate(argv)
41+
case "auth":
4542
fmt.Print(usage)
4643
return nil
4744
default:

client-go/parser/builds.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package parser
22

33
import (
4-
"fmt"
5-
64
"github.com/deis/deis/client-go/cmd"
75
docopt "github.com/docopt/docopt-go"
86
)
@@ -17,19 +15,22 @@ builds:create imports an image and deploys as a new release
1715
1816
Use 'deis help [command]' to learn more.
1917
`
20-
if len(argv) < 2 {
21-
return buildsList([]string{"builds:list"})
22-
}
2318

24-
switch argv[1] {
25-
case "list":
26-
return buildsList(combineCommand(argv))
27-
case "create":
28-
return buildsCreate(combineCommand(argv))
29-
case "--help":
30-
fmt.Print(usage)
31-
return nil
19+
switch argv[0] {
20+
case "builds:list":
21+
return buildsList(argv)
22+
case "builds:create":
23+
return buildsCreate(argv)
3224
default:
25+
if printHelp(argv, usage) {
26+
return nil
27+
}
28+
29+
if argv[0] == "builds" {
30+
argv[0] = "builds:list"
31+
return buildsList(argv)
32+
}
33+
3334
PrintUsage()
3435
return nil
3536
}

client-go/parser/certs.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package parser
22

33
import (
4-
"fmt"
5-
64
"github.com/deis/deis/client-go/cmd"
75
docopt "github.com/docopt/docopt-go"
86
)
@@ -18,21 +16,24 @@ certs:remove remove an SSL certificate from an app
1816
1917
Use 'deis help [command]' to learn more.
2018
`
21-
if len(argv) < 2 {
22-
return certsList([]string{"certs:list"})
23-
}
2419

25-
switch argv[1] {
26-
case "list":
27-
return certsList(combineCommand(argv))
28-
case "add":
29-
return certAdd(combineCommand(argv))
30-
case "remove":
31-
return certRemove(combineCommand(argv))
32-
case "--help":
33-
fmt.Print(usage)
34-
return nil
20+
switch argv[0] {
21+
case "certs:list":
22+
return certsList(argv)
23+
case "certs:add":
24+
return certAdd(argv)
25+
case "certs:remove":
26+
return certRemove(argv)
3527
default:
28+
if printHelp(argv, usage) {
29+
return nil
30+
}
31+
32+
if argv[0] == "certs" {
33+
argv[0] = "certs:list"
34+
return certsList(argv)
35+
}
36+
3637
PrintUsage()
3738
return nil
3839
}

0 commit comments

Comments
 (0)