Skip to content

Commit 1622cf6

Browse files
committed
fix(deis.go): do not add top level command to cmdArgs
the problem was for Deis CLI extensions that implements top level commands (for example `deis ssh`). In such case CLI used `deis-ssh ssh` command instead of `deis-ssh` Do not pass top-level command in the argv.
1 parent dcdddaf commit 1622cf6

2 files changed

Lines changed: 42 additions & 11 deletions

File tree

deis.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
docopt "github.com/docopt/docopt-go"
1515
)
1616

17+
const extensionPrefix = "deis-"
18+
1719
// main exits with the return value of Command(os.Args[1:]), deferring all logic to
1820
// a func we can test.
1921
func main() {
@@ -154,23 +156,14 @@ Use 'git push deis master' to deploy to an application.
154156
err = parser.Whitelist(argv, &cmdr)
155157
default:
156158
env := os.Environ()
157-
extCmd := "deis-" + command
158159

159-
binary, err := exec.LookPath(extCmd)
160+
binary, err := exec.LookPath(extensionPrefix + command)
160161
if err != nil {
161162
parser.PrintUsage(&cmdr)
162163
return 1
163164
}
164165

165-
cmdArgv := []string{extCmd}
166-
167-
cmdSplit := strings.Split(argv[0], command+":")
168-
169-
if len(cmdSplit) > 1 {
170-
argv[0] = cmdSplit[1]
171-
}
172-
173-
cmdArgv = append(cmdArgv, argv...)
166+
cmdArgv := prepareCmdArgs(command, argv)
174167

175168
err = syscall.Exec(binary, cmdArgv, env)
176169
if err != nil {
@@ -249,6 +242,18 @@ func parseArgs(argv []string) (string, []string) {
249242
return "", argv
250243
}
251244

245+
// split original command and pass its first element in arguments
246+
func prepareCmdArgs(command string, argv []string) []string {
247+
cmdArgv := []string{extensionPrefix + command}
248+
cmdSplit := strings.Split(argv[0], command+":")
249+
250+
if len(cmdSplit) > 1 {
251+
cmdArgv = append(cmdArgv, cmdSplit[1])
252+
}
253+
254+
return append(cmdArgv, argv[1:]...)
255+
}
256+
252257
func replaceShortcut(command string) string {
253258
expandedCommand := cli.Shortcuts[command]
254259
if expandedCommand == "" {

deis_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ func TestCommandSplitting(t *testing.T) {
6363
}
6464
}
6565

66+
func TestTopLevelCommandArgsPreparing(t *testing.T) {
67+
t.Parallel()
68+
69+
command := "ssh"
70+
argv := []string{"ssh"}
71+
expected := []string{"deis-ssh"}
72+
actual := prepareCmdArgs(command, argv)
73+
74+
if !reflect.DeepEqual(expected, actual) {
75+
t.Errorf("Expected %v, Got %v", expected, actual)
76+
}
77+
}
78+
79+
func TestCommandWithParameterArgsPreparing(t *testing.T) {
80+
t.Parallel()
81+
82+
command := "ssh --help"
83+
argv := []string{"ssh --help"}
84+
expected := []string{"deis-ssh --help"}
85+
actual := prepareCmdArgs(command, argv)
86+
87+
if !reflect.DeepEqual(expected, actual) {
88+
t.Errorf("Expected %v, Got %v", expected, actual)
89+
}
90+
}
91+
6692
func TestReplaceShortcutRepalce(t *testing.T) {
6793
t.Parallel()
6894

0 commit comments

Comments
 (0)