Skip to content

Commit

Permalink
Merge pull request #14 from loft-sh/fix/multiple_bugs
Browse files Browse the repository at this point in the history
Fix/multiple bugs
  • Loading branch information
89luca89 authored Sep 5, 2023
2 parents d5c8c34 + 6f706e2 commit e084aae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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
Expand Down
26 changes: 21 additions & 5 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -36,26 +37,41 @@ 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" {
result = append(result, []string{"-p", provider.Config.Port}...)
}

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...)
Expand Down

0 comments on commit e084aae

Please sign in to comment.