Skip to content

Commit 1fb144c

Browse files
authored
Merge pull request #255 from smt116/master
fix(deis.go): do not add top level command to cmdArgs
2 parents a6cd7e6 + 1622cf6 commit 1fb144c

3 files changed

Lines changed: 46 additions & 21 deletions

File tree

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,19 @@ $ mv deis /usr/local/bin
7979

8080
To compile the client from scratch, ensure you have Docker installed and run
8181

82-
$ make bootstrap
83-
$ make build
84-
85-
`make bootstrap` will fetch all required dependencies, while `make build` will compile and install
86-
the client in the current directory.
87-
88-
$ ./deis --version
82+
$ make
8983

9084
### From Scratch on Windows
9185

9286
To compile the client from scratch, open PowerShell and execute the following commands in the source directory.
9387

94-
$ .\make bootstrap
95-
$ .\make build
88+
$ .\make bootstrap
89+
$ .\make build
9690

9791
`.\make bootstrap` will fetch all required dependencies, while `.\make build` will compile and install
9892
the client in the current directory.
9993

100-
$ .\deis --version
94+
$ .\deis --version
10195

10296
## Usage
10397

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)