Skip to content

Commit

Permalink
Adds better handling for nil results
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Aug 8, 2024
1 parent dfae353 commit 5318f7c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 49 deletions.
8 changes: 6 additions & 2 deletions cmd/deploytargetconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ var deleteDeployTargetConfigCmd = &cobra.Command{
return err
}
if project.Name == "" {
outputOptions.Error = fmt.Sprintf("No details for project '%s'", cmdProjectName)
outputOptions.Error = fmt.Sprintf("No details for project '%s'\n", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
}
Expand Down Expand Up @@ -291,14 +291,18 @@ var listDeployTargetConfigsCmd = &cobra.Command{
return err
}
if project.Name == "" {
outputOptions.Error = fmt.Sprintf("No details for project '%s'", cmdProjectName)
outputOptions.Error = fmt.Sprintf("No details for project '%s'\n", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
}
deployTargetConfigs, err := lagoon.GetDeployTargetConfigs(context.TODO(), int(project.ID), lc)
if err != nil {
return err
}
if len(*deployTargetConfigs) == 0 {
handleNilResults("No deploytarget-configs for project '%s'\n", cmd, cmdProjectName)
return nil
}
data := []output.Data{}
for _, deployTargetConfig := range *deployTargetConfigs {
data = append(data, []string{
Expand Down
4 changes: 4 additions & 0 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ var listBackupsCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
return nil
}
backupsResult, err := lagoon.GetBackupsForEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if err != nil {
return err
Expand Down
34 changes: 24 additions & 10 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ var getProjectCmd = &cobra.Command{
}

if project.Name == "" {
outputOptions.Error = fmt.Sprintf("No details for project '%s'\n", cmdProjectName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("No details for project '%s'\n", cmd, cmdProjectName)
return nil
}

Expand Down Expand Up @@ -251,6 +249,15 @@ var getEnvironmentCmd = &cobra.Command{
return err
}

if project.Name == "" || environment.Name == "" {
if project.Name == "" {
handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
} else {
handleNilResults("Environment '%s' not found in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName)
}
return nil
}

data := []output.Data{}
var envRoute = "none"
if environment.Route != "" {
Expand Down Expand Up @@ -317,10 +324,24 @@ var getProjectKeyCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
return nil
}

projectKey, err := lagoon.GetProjectKeyByName(context.TODO(), cmdProjectName, revealValue, lc)
if err != nil {
return err
}
if projectKey.PublicKey == "" && projectKey.PrivateKey == "" {
handleNilResults("No project-key for project '%s'\n", cmd, cmdProjectName)
return nil
}

projectKeys := []string{projectKey.PublicKey}
if projectKey.PrivateKey != "" {
projectKeys = append(projectKeys, strings.TrimSuffix(projectKey.PrivateKey, "\n"))
Expand All @@ -335,13 +356,6 @@ var getProjectKeyCmd = &cobra.Command{
Data: data,
}

if len(dataMain.Data) == 0 {
outputOptions.Error = fmt.Sprintf("No project-key for project '%s'", cmdProjectName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
return nil
}

if projectKey.PrivateKey != "" {
dataMain.Header = append(dataMain.Header, "PrivateKey")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ var addProjectToGroupCmd = &cobra.Command{
return err
}
if len(project.Name) == 0 {
outputOptions.Error = fmt.Sprintf("Project '%s' not found", cmdProjectName)
outputOptions.Error = fmt.Sprintf("Project '%s' not found\n", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
}
Expand Down Expand Up @@ -335,7 +335,7 @@ var deleteProjectFromGroupCmd = &cobra.Command{
return err
}
if len(project.Name) == 0 {
outputOptions.Error = fmt.Sprintf("Project '%s' not found", cmdProjectName)
outputOptions.Error = fmt.Sprintf("Project '%s' not found\n", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
}
Expand Down
60 changes: 25 additions & 35 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,10 @@ var listGroupProjectsCmd = &cobra.Command{
}
if len(data) == 0 {
if !listAllProjects {
outputOptions.Error = fmt.Sprintf("There are no projects in group '%s'\n", groupName)
handleNilResults("There are no projects in group '%s'\n", cmd, groupName)
} else {
outputOptions.Error = "There are no projects in any groups\n"
handleNilResults("There are no projects in any groups\n", cmd)
}
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
return nil
}

Expand Down Expand Up @@ -363,9 +361,7 @@ var listEnvironmentsCmd = &cobra.Command{
}

if len(*environments) == 0 {
outputOptions.Error = fmt.Sprintf("No environments found for project '%s'\n", cmdProjectName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("No environments found for project '%s'\n", cmd, cmdProjectName)
return nil
}

Expand Down Expand Up @@ -461,12 +457,10 @@ var listVariablesCmd = &cobra.Command{
}
if len(data) == 0 {
if cmdProjectEnvironment != "" {
outputOptions.Error = fmt.Sprintf("There are no variables for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName)
handleNilResults("There are no variables for environment '%s' in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName)
} else {
outputOptions.Error = fmt.Sprintf("There are no variables for project '%s'\n", cmdProjectName)
handleNilResults("There are no variables for project '%s'\n", cmd, cmdProjectName)
}
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
return nil
}
r := output.RenderOutput(output.Table{
Expand Down Expand Up @@ -507,6 +501,10 @@ var listDeploymentsCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
return nil
}

deployments, err := lagoon.GetDeploymentsByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
Expand All @@ -527,9 +525,7 @@ var listDeploymentsCmd = &cobra.Command{
}

if len(data) == 0 {
outputOptions.Error = fmt.Sprintf("There are no deployments for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("There are no deployments for environment '%s' in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName)
return nil
}
dataMain := output.Table{
Expand Down Expand Up @@ -571,6 +567,10 @@ var listTasksCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
return nil
}

tasks, err := lagoon.GetTasksByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
Expand All @@ -592,9 +592,7 @@ var listTasksCmd = &cobra.Command{
}

if len(data) == 0 {
outputOptions.Error = fmt.Sprintf("There are no tasks for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("There are no tasks for environment '%s' in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName)
return nil
}
dataMain := output.Table{
Expand Down Expand Up @@ -809,6 +807,10 @@ var listInvokableTasks = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
return nil
}
tasks, err := lagoon.GetInvokableAdvancedTaskDefinitionsByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
return err
Expand All @@ -823,9 +825,7 @@ var listInvokableTasks = &cobra.Command{
}

if len(data) == 0 {
outputOptions.Error = "There are no user defined tasks for this environment\n"
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("There are no user defined tasks for environment %s\n", cmd, cmdProjectEnvironment)
return nil
}
dataMain := output.Table{
Expand Down Expand Up @@ -877,9 +877,7 @@ var listProjectGroupsCmd = &cobra.Command{
}

if len(projectGroups.Groups) == 0 {
outputOptions.Error = fmt.Sprintf("There are no groups for project '%s'\n", cmdProjectName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("There are no groups for project '%s'\n", cmd, cmdProjectName)
return nil
}

Expand Down Expand Up @@ -947,9 +945,7 @@ var listOrganizationProjectsCmd = &cobra.Command{
}

if len(*orgProjects) == 0 {
outputOptions.Error = fmt.Sprintf("No associated projects found for organization '%s'\n", organizationName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("No associated projects found for organization '%s'\n", cmd, organizationName)
return nil
}

Expand Down Expand Up @@ -1012,9 +1008,7 @@ var listOrganizationGroupsCmd = &cobra.Command{
return err
}
if len(*orgGroups) == 0 {
outputOptions.Error = fmt.Sprintf("No associated groups found for organization '%s'\n", organizationName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("No associated groups found for organization '%s'\n", cmd, organizationName)
return nil
}

Expand Down Expand Up @@ -1074,9 +1068,7 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{
return err
}
if len(*deployTargets) == 0 {
outputOptions.Error = fmt.Sprintf("No associated deploy targets found for organization '%s'\n", organizationName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("No associated deploy targets found for organization '%s'\n", cmd, organizationName)
return nil
}

Expand Down Expand Up @@ -1188,9 +1180,7 @@ var listOrganizationAdminsCmd = &cobra.Command{
return err
}
if len(*users) == 0 {
outputOptions.Error = fmt.Sprintf("No associated users found for organization '%s'\n", organizationName)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
handleNilResults("No associated users found for organization '%s'\n", cmd, organizationName)
return nil
}
data := []output.Data{}
Expand Down
4 changes: 4 additions & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ var removeProjectFromOrganizationCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
return nil
}
organization, err := lagoon.GetOrganizationByName(context.TODO(), organizationName, lc)
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
"strings"

Expand Down Expand Up @@ -107,3 +108,10 @@ func requiredInputCheck(fieldsAndValues ...string) error {
}
return nil
}

// Outputs the message in a way that can be captured by testing
func handleNilResults(message string, cmd *cobra.Command, fields ...interface{}) {
outputOptions.Error = fmt.Sprintf(message, fields...)
r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
}

0 comments on commit 5318f7c

Please sign in to comment.