diff --git a/cmd/environment.go b/cmd/environment.go index ecd113a3..f7dddd4b 100644 --- a/cmd/environment.go +++ b/cmd/environment.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + s "github.com/uselagoon/machinery/api/schema" "os" "strings" @@ -13,7 +14,6 @@ import ( "github.com/uselagoon/lagoon-cli/pkg/output" l "github.com/uselagoon/machinery/api/lagoon" lclient "github.com/uselagoon/machinery/api/lagoon/client" - s "github.com/uselagoon/machinery/api/schema" ) // @TODO re-enable this at some point if more environment based commands are made available @@ -131,10 +131,16 @@ var updateEnvironmentCmd = &cobra.Command{ } if environmentType != "" { envType := s.EnvType(strings.ToUpper(environmentType)) + if validationErr := s.ValidateType(envType); validationErr != nil { + handleError(validationErr) + } environmentFlags.EnvironmentType = &envType } if deployT != "" { deployType := s.DeployType(strings.ToUpper(deployT)) + if validationErr := s.ValidateType(deployType); validationErr != nil { + handleError(validationErr) + } environmentFlags.DeployType = &deployType } diff --git a/cmd/list.go b/cmd/list.go index f4fcdddf..d77f7188 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -287,17 +287,10 @@ var listVariablesCmd = &cobra.Command{ env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Scope))) env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Name))) if reveal { - env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Value))) + env = append(env, fmt.Sprintf("%v", envvar.Value)) } data = append(data, env) } - if len(data) == 0 { - if cmdProjectEnvironment != "" { - return fmt.Errorf("There are no variables for environment '%s' in project '%s'", cmdProjectEnvironment, cmdProjectName) - } else { - return fmt.Errorf("There are no variables for project '%s'", cmdProjectName) - } - } header := []string{ "ID", "Project", @@ -310,6 +303,13 @@ var listVariablesCmd = &cobra.Command{ if reveal { header = append(header, "Value") } + if len(data) == 0 { + if cmdProjectEnvironment != "" { + outputOptions.Error = fmt.Sprintf("There are no variables for environment '%s' in project '%s'", cmdProjectEnvironment, cmdProjectName) + } else { + outputOptions.Error = fmt.Sprintf("There are no variables for project '%s'\n", cmdProjectName) + } + } output.RenderOutput(output.Table{ Header: header, Data: data, diff --git a/cmd/ssh.go b/cmd/ssh.go index 61896674..37b97350 100644 --- a/cmd/ssh.go +++ b/cmd/ssh.go @@ -41,28 +41,28 @@ var sshEnvCmd = &cobra.Command{ // set the default ssh host and port to the core ssh endpoint sshHost := lagoonCLIConfig.Lagoons[current].HostName sshPort := lagoonCLIConfig.Lagoons[current].Port + isPortal := false // if the config for this lagoon is set to use ssh portal support, handle that here - if lagoonCLIConfig.Lagoons[current].SSHPortal { - lc := client.New( - lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, - lagoonCLIVersion, - debug) - project, err := lagoon.GetSSHEndpointsByProject(context.TODO(), cmdProjectName, lc) - if err != nil { - return err - } - // check all the environments for this project - for _, env := range project.Environments { - // if the env name matches the requested environment then check if the deploytarget supports regional ssh endpoints - if env.Name == environmentName { - // if the deploytarget supports regional endpoints, then set these as the host and port for ssh - if env.DeployTarget.SSHHost != "" && env.DeployTarget.SSHPort != "" { - sshHost = env.DeployTarget.SSHHost - sshPort = env.DeployTarget.SSHPort - } + lc := client.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIConfig.Lagoons[current].Token, + lagoonCLIConfig.Lagoons[current].Version, + lagoonCLIVersion, + debug) + project, err := lagoon.GetSSHEndpointsByProject(context.TODO(), cmdProjectName, lc) + if err != nil { + return err + } + // check all the environments for this project + for _, env := range project.Environments { + // if the env name matches the requested environment then check if the deploytarget supports regional ssh endpoints + if env.Name == environmentName { + // if the deploytarget supports regional endpoints, then set these as the host and port for ssh + if env.DeployTarget.SSHHost != "" && env.DeployTarget.SSHPort != "" { + sshHost = env.DeployTarget.SSHHost + sshPort = env.DeployTarget.SSHPort + isPortal = true } } } @@ -88,7 +88,7 @@ var sshEnvCmd = &cobra.Command{ "sshkey": privateKey, } if sshConnString { - fmt.Println(generateSSHConnectionString(sshConfig, sshService, sshContainer)) + fmt.Println(generateSSHConnectionString(sshConfig, sshService, sshContainer, isPortal)) } else { // start an interactive ssh session @@ -127,8 +127,15 @@ func init() { } // generateSSHConnectionString . -func generateSSHConnectionString(lagoon map[string]string, service string, container string) string { - connString := fmt.Sprintf("ssh -t %s-o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -p %v %s@%s", lagoon["sshKey"], lagoon["port"], lagoon["username"], lagoon["hostname"]) +func generateSSHConnectionString(lagoon map[string]string, service string, container string, isPortal bool) string { + connString := fmt.Sprintf("ssh -t") + if lagoon["sshKey"] != "" { + connString = fmt.Sprintf("%s -i %s", connString, lagoon["sshKey"]) + } + if !isPortal { + connString = fmt.Sprintf("%s -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\"", connString) + } + connString = fmt.Sprintf("%s -p %v %s@%s", connString, lagoon["port"], lagoon["username"], lagoon["hostname"]) if service != "" { connString = fmt.Sprintf("%s service=%s", connString, service) } diff --git a/cmd/ssh_test.go b/cmd/ssh_test.go index e062f5df..fb2adfa2 100644 --- a/cmd/ssh_test.go +++ b/cmd/ssh_test.go @@ -11,6 +11,7 @@ func Test_generateSSHConnectionString(t *testing.T) { lagoon map[string]string service string container string + isPortal bool } tests := []struct { name string @@ -65,7 +66,36 @@ func Test_generateSSHConnectionString(t *testing.T) { service: "cli", container: "cli", }, - want: `ssh -t /home/user/.ssh/my-key-o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -p 22 example-com-main@lagoon.example.com service=cli container=cli`, + want: `ssh -t -i /home/user/.ssh/my-key -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -p 22 example-com-main@lagoon.example.com service=cli container=cli`, + }, + { + name: "test5 - sshportal", + args: args{ + lagoon: map[string]string{ + "hostname": "lagoon.example.com", + "port": "22", + "username": "example-com-main", + "sshKey": "/home/user/.ssh/my-key", + }, + isPortal: true, + service: "cli", + container: "cli", + }, + want: `ssh -t -i /home/user/.ssh/my-key -p 22 example-com-main@lagoon.example.com service=cli container=cli`, + }, + { + name: "test6 - sshportal", + args: args{ + lagoon: map[string]string{ + "hostname": "lagoon.example.com", + "port": "22", + "username": "example-com-main", + }, + isPortal: true, + service: "cli", + container: "cli", + }, + want: `ssh -t -p 22 example-com-main@lagoon.example.com service=cli container=cli`, }, } for _, tt := range tests { @@ -73,7 +103,7 @@ func Test_generateSSHConnectionString(t *testing.T) { cmdProjectName = tt.args.project cmdProjectEnvironment = tt.args.environment - if got := generateSSHConnectionString(tt.args.lagoon, tt.args.service, tt.args.container); got != tt.want { + if got := generateSSHConnectionString(tt.args.lagoon, tt.args.service, tt.args.container, tt.args.isPortal); got != tt.want { t.Errorf("generateSSHConnectionString() = %v, want %v", got, tt.want) } }) diff --git a/go.mod b/go.mod index 7b53381d..2d25de43 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/guregu/null v4.0.0+incompatible // workaround for https://github.com/manifoldco/promptui/issues/98 github.com/nicksnyder/go-i18n v1.10.1 // indirect - github.com/uselagoon/machinery v0.0.12-0.20230828053336-cb8156419c94 + github.com/uselagoon/machinery v0.0.13 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect diff --git a/go.sum b/go.sum index 0770abdb..c10dbdde 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE= github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 h1:I4BOK3PBMjhWfQM2zPJKK7lOBGsrsvOB7kBELP33hiE= github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= @@ -115,6 +116,7 @@ github.com/uselagoon/machinery v0.0.12-0.20230825064934-e3ebd4b50b51 h1:xGymM19k github.com/uselagoon/machinery v0.0.12-0.20230825064934-e3ebd4b50b51/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE= github.com/uselagoon/machinery v0.0.12-0.20230828053336-cb8156419c94 h1:GZQox2sqLVBPyCQqG7A163oFCMJ/+Egkoa1u/RXxHdU= github.com/uselagoon/machinery v0.0.12-0.20230828053336-cb8156419c94/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE= +github.com/uselagoon/machinery v0.0.13/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/pkg/output/main.go b/pkg/output/main.go index 756bd69c..05ff549e 100644 --- a/pkg/output/main.go +++ b/pkg/output/main.go @@ -26,6 +26,7 @@ type Options struct { JSON bool Pretty bool Debug bool + Error string } // Result . @@ -125,6 +126,9 @@ func RenderOutput(data Table, opts Options) { RenderJSON(returnedData, opts) } else { // otherwise render a table + if opts.Error != "" { + os.Stderr.WriteString(opts.Error) + } table := tablewriter.NewWriter(os.Stdout) opts.Header = !opts.Header if opts.Header {