diff --git a/go.mod b/go.mod index e7c7671..5a6f87b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/loft-sh/devpod-provider-ssh go 1.20 require ( + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/loft-sh/devpod v0.1.10 github.com/onsi/ginkgo/v2 v2.9.1 github.com/onsi/gomega v1.27.4 @@ -43,7 +44,6 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect - github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/klauspost/compress v1.15.12 // indirect github.com/loft-sh/utils v0.0.16 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/pkg/options/options.go b/pkg/options/options.go index 58c0cfa..a4574a7 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -22,16 +22,6 @@ type Options struct { ExtraFlags string } -func ConfigFromEnv() (Options, error) { - return Options{ - DockerPath: os.Getenv(DOCKER_PATH), - AgentPath: os.Getenv(AGENT_PATH), - Host: os.Getenv(HOST), - Port: os.Getenv(PORT), - ExtraFlags: os.Getenv(EXTRA_FLAGS), - }, nil -} - func FromEnv() (*Options, error) { retOptions := &Options{} @@ -47,6 +37,8 @@ func FromEnv() (*Options, error) { return nil, err } + retOptions.ExtraFlags = os.Getenv(EXTRA_FLAGS) + retOptions.Host, err = fromEnvOrError(HOST) if err != nil { return nil, err diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index e84f857..32a18a7 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -9,6 +9,7 @@ import ( "path" "strings" + "github.com/kballard/go-shellquote" "github.com/loft-sh/devpod-provider-ssh/pkg/options" "github.com/loft-sh/devpod/pkg/log" ) @@ -36,10 +37,16 @@ func NewProvider(logs log.Logger) (*SSHProvider, error) { func returnSSHError(provider *SSHProvider, command string) error { sshError := "Please make sure you have configured the correct SSH host\nand the following command can be executed on your system:\n" - return fmt.Errorf(sshError + "ssh" + strings.Join(getSSHCommand(provider), " ") + " " + command) + + sshcmd, err := getSSHCommand(provider) + if err != nil { + return err + } + + return fmt.Errorf(sshError + "ssh " + strings.Join(sshcmd, " ") + " " + command) } -func getSSHCommand(provider *SSHProvider) []string { +func getSSHCommand(provider *SSHProvider) ([]string, error) { result := []string{"-oStrictHostKeyChecking=no", "-oBatchMode=yes"} if provider.Config.Port != "22" { @@ -47,15 +54,24 @@ func getSSHCommand(provider *SSHProvider) []string { } if provider.Config.ExtraFlags != "" { - result = append(result, strings.Split(provider.Config.ExtraFlags, " ")...) + flags, err := shellquote.Split(provider.Config.ExtraFlags) + if err != nil { + return nil, fmt.Errorf("error managing EXTRA_ARGS, %v", err) + } + + result = append(result, flags...) } result = append(result, provider.Config.Host) - return result + return result, nil } func execSSHCommand(provider *SSHProvider, command string, output io.Writer) error { - commandToRun := getSSHCommand(provider) + commandToRun, err := getSSHCommand(provider) + if err != nil { + return err + } + commandToRun = append(commandToRun, command) cmd := exec.Command("ssh", commandToRun...)