Skip to content

Commit

Permalink
fix(client): do not add top level command to cmdArgs
Browse files Browse the repository at this point in the history
this is a case for Deis CLI plugins. If you have `deis ssh` command
it should run `deis-ssh` instead of `deis-ssh ssh`.
  • Loading branch information
smt116 committed Oct 3, 2016
1 parent e0e2e29 commit 0dcdd33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
27 changes: 16 additions & 11 deletions client/deis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
docopt "github.com/docopt/docopt-go"
)

const extensionPrefix = "deis-"

// main exits with the return value of Command(os.Args[1:]), deferring all logic to
// a func we can test.
func main() {
Expand Down Expand Up @@ -119,23 +121,14 @@ Use 'git push deis master' to deploy to an application.
return 0
default:
env := os.Environ()
extCmd := "deis-" + command

binary, err := exec.LookPath(extCmd)
binary, err := exec.LookPath(extensionPrefix + command)
if err != nil {
parser.PrintUsage()
return 1
}

cmdArgv := []string{extCmd}

cmdSplit := strings.Split(argv[0], command+":")

if len(cmdSplit) > 1 {
argv[0] = cmdSplit[1]
}

cmdArgv = append(cmdArgv, argv...)
cmdArgv := prepareCmdArgs(command, argv)

err = syscall.Exec(binary, cmdArgv, env)
if err != nil {
Expand Down Expand Up @@ -186,6 +179,18 @@ func parseArgs(argv []string) (string, []string) {
return "", argv
}

// split original command and pass its first element in arguments
func prepareCmdArgs(command string, argv []string) []string {
cmdArgv := []string{extensionPrefix + command}
cmdSplit := strings.Split(argv[0], command+":")

if len(cmdSplit) > 1 {
cmdArgv = append(cmdArgv, cmdSplit[1])
}

return append(cmdArgv, argv[1:]...)
}

func replaceShortcut(command string) string {
shortcuts := map[string]string{
"create": "apps:create",
Expand Down
26 changes: 26 additions & 0 deletions client/deis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ func TestCommandSplitting(t *testing.T) {
}
}

func TestTopLevelCommandArgsPreparing(t *testing.T) {
t.Parallel()

command := "ssh"
argv := []string{"ssh"}
expected := []string{"deis-ssh"}
actual := prepareCmdArgs(command, argv)

if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected %v, Got %v", expected, actual)
}
}

func TestCommandWithParameterArgsPreparing(t *testing.T) {
t.Parallel()

command := "ssh --help"
argv := []string{"ssh --help"}
expected := []string{"deis-ssh --help"}
actual := prepareCmdArgs(command, argv)

if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected %v, Got %v", expected, actual)
}
}

func TestReplaceShortcutRepalce(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 0dcdd33

Please sign in to comment.