Skip to content

Commit ceed7fe

Browse files
authored
feat: improved error message for ssh on invalid environment name (#463)
* add environment check * refactor error to be checked in logs
1 parent 2378cec commit ceed7fe

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

cmd/logs.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strings"
78
"time"
89

910
"github.com/spf13/cobra"
@@ -82,9 +83,13 @@ func getSSHHostPort(environmentName string, debug bool) (string, string, string,
8283
username := fmt.Sprintf("%s-%s", cmdProjectName, environmentName)
8384

8485
// check all the environments for this project
85-
for _, env := range project.Environments {
86+
found := false
87+
names := make([]string, len(project.Environments))
88+
for i, env := range project.Environments {
89+
names[i] = env.Name
8690
// if the env name matches the requested or computed environment then check if the deploytarget supports regional ssh endpoints
8791
if env.OpenshiftProjectName == namespace.GenerateNamespaceName("", cmdProjectEnvironment, cmdProjectName, "", "", false) || env.Name == environmentName || env.Name == cmdProjectEnvironment {
92+
found = true
8893
// if the deploytarget supports regional endpoints, then set these as the host and port for ssh
8994
if env.DeployTarget.SSHHost != "" && env.DeployTarget.SSHPort != "" {
9095
sshHost = env.DeployTarget.SSHHost
@@ -95,6 +100,11 @@ func getSSHHostPort(environmentName string, debug bool) (string, string, string,
95100
username = env.OpenshiftProjectName
96101
}
97102
}
103+
104+
if !found {
105+
return sshHost, sshPort, username, portal, fmt.Errorf("invalid environment for project %s: %s. Valid options are %s.\n", cmdProjectName, environmentName, strings.Join(names, ", "))
106+
}
107+
98108
return sshHost, sshPort, username, portal, nil
99109
}
100110

cmd/shared.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/spf13/cobra"
65
"os"
76
"strconv"
87
"strings"
98

9+
"github.com/spf13/cobra"
10+
1011
"github.com/uselagoon/lagoon-cli/pkg/output"
1112
)
1213

0 commit comments

Comments
 (0)