Skip to content

Commit 12ee395

Browse files
jackwrfullershreddedbacon
authored andcommitted
refactor error to be checked in logs
1 parent e2fbde0 commit 12ee395

3 files changed

Lines changed: 11 additions & 36 deletions

File tree

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: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"strconv"
@@ -10,8 +9,6 @@ import (
109
"github.com/spf13/cobra"
1110

1211
"github.com/uselagoon/lagoon-cli/pkg/output"
13-
lclient "github.com/uselagoon/machinery/api/lagoon/client"
14-
"github.com/uselagoon/machinery/api/schema"
1512
)
1613

1714
// config vars
@@ -129,31 +126,3 @@ func quotaCheck(quota int) string {
129126
}
130127
return quotaRoute
131128
}
132-
133-
func environmentCheck(project string, environment string, debug bool) error {
134-
135-
current := lagoonCLIConfig.Current
136-
token := lagoonCLIConfig.Lagoons[current].Token
137-
lc := lclient.New(
138-
lagoonCLIConfig.Lagoons[current].GraphQL,
139-
lagoonCLIVersion,
140-
lagoonCLIConfig.Lagoons[current].Version,
141-
&token,
142-
debug)
143-
144-
var environments []schema.Environment
145-
err := lc.EnvironmentsByProjectName(context.TODO(), project, &environments)
146-
if err != nil {
147-
return err
148-
}
149-
150-
names := make([]string, len(environments))
151-
for i, e := range environments {
152-
names[i] = e.AddEnvironmentInput.Name
153-
if environment == names[i] {
154-
return nil
155-
}
156-
}
157-
158-
return fmt.Errorf("invalid environment for project %s: %s. Valid options are %s.\n", project, environment, strings.Join(names, ", "))
159-
}

cmd/ssh.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ var sshEnvCmd = &cobra.Command{
2626
return err
2727
}
2828
debug, err := cmd.Flags().GetBool("debug")
29-
if err != nil {
30-
return nil
31-
}
32-
err = environmentCheck(cmdProjectName, cmdProjectEnvironment, debug)
3329
if err != nil {
3430
return err
3531
}

0 commit comments

Comments
 (0)