Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better handling for nil results #374

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cmd/deploytargetconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ var deleteDeployTargetConfigCmd = &cobra.Command{
return err
}
if project.Name == "" {
outputOptions.Error = fmt.Sprintf("No details for project '%s'", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}

if yesNo(fmt.Sprintf("You are attempting to delete deploytarget configuration with id '%d' from project '%s', are you sure?", id, cmdProjectName)) {
Expand Down Expand Up @@ -291,14 +289,15 @@ var listDeployTargetConfigsCmd = &cobra.Command{
return err
}
if project.Name == "" {
outputOptions.Error = fmt.Sprintf("No details for project '%s'", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}
deployTargetConfigs, err := lagoon.GetDeployTargetConfigs(context.TODO(), int(project.ID), lc)
if err != nil {
return err
}
if len(*deployTargetConfigs) == 0 {
return handleNilResults("No deploytarget-configs for project '%s'\n", cmd, cmdProjectName)
}
data := []output.Data{}
for _, deployTargetConfig := range *deployTargetConfigs {
data = append(data, []string{
Expand Down
3 changes: 3 additions & 0 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ var listBackupsCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}
backupsResult, err := lagoon.GetBackupsForEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if err != nil {
return err
Expand Down
38 changes: 24 additions & 14 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +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)
return nil
return handleNilResults("No details for project '%s'\n", cmd, cmdProjectName)
}

devEnvironments := 0
Expand Down Expand Up @@ -251,6 +248,14 @@ var getEnvironmentCmd = &cobra.Command{
return err
}

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

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

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

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

projectKeys := []string{projectKey.PublicKey}
if projectKey.PrivateKey != "" {
projectKeys = append(projectKeys, strings.TrimSuffix(projectKey.PrivateKey, "\n"))
Expand All @@ -335,13 +352,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 Expand Up @@ -406,9 +416,9 @@ var getOrganizationCmd = &cobra.Command{
strconv.Itoa(int(organization.ID)),
organization.Name,
organization.Description,
strconv.Itoa(int(organization.QuotaProject)),
strconv.Itoa(int(organization.QuotaGroup)),
strconv.Itoa(int(organization.QuotaNotification)),
strconv.Itoa(organization.QuotaProject),
strconv.Itoa(organization.QuotaGroup),
strconv.Itoa(organization.QuotaNotification),
})

dataMain := output.Table{
Expand Down
8 changes: 2 additions & 6 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ var addProjectToGroupCmd = &cobra.Command{
return err
}
if len(project.Name) == 0 {
outputOptions.Error = fmt.Sprintf("Project '%s' not found", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}
_, err = lagoon.AddProjectToGroup(context.TODO(), projectGroup, lc)
if err != nil {
Expand Down Expand Up @@ -335,9 +333,7 @@ var deleteProjectFromGroupCmd = &cobra.Command{
return err
}
if len(project.Name) == 0 {
outputOptions.Error = fmt.Sprintf("Project '%s' not found", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
return nil
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}

if yesNo(fmt.Sprintf("You are attempting to delete project '%s' from group '%s', are you sure?", projectGroup.Project.Name, projectGroup.Groups[0].Name)) {
Expand Down
72 changes: 24 additions & 48 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var listProjectsCmd = &cobra.Command{
data = append(data, projData)
}
if len(data) == 0 {
outputOptions.Error = "No access to any projects in Lagoon\n"
return handleNilResults("No access to any projects in Lagoon\n", cmd)
}
projHeader := []string{"ID", "ProjectName", "GitUrl", "ProductionEnvironment", "ProductionRoute", "DevEnvironments"}
// if wide {
Expand Down Expand Up @@ -234,7 +234,7 @@ var listGroupsCmd = &cobra.Command{
})
}
if len(data) == 0 {
outputOptions.Error = "This account is not in any groups\n"
return handleNilResults("This account is not in any groups\n", cmd)
}
dataMain := output.Table{
Header: []string{"ID", "Name"},
Expand Down Expand Up @@ -311,13 +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)
return handleNilResults("There are no projects in group '%s'\n", cmd, groupName)
} else {
outputOptions.Error = "There are no projects in any groups\n"
return 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
}

dataMain := output.Table{
Expand Down Expand Up @@ -363,10 +360,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)
return nil
return handleNilResults("No environments found for project '%s'\n", cmd, cmdProjectName)
}

data := []output.Data{}
Expand Down Expand Up @@ -461,13 +455,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)
return 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)
return 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{
Header: header,
Expand Down Expand Up @@ -507,6 +498,9 @@ var listDeploymentsCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}

deployments, err := lagoon.GetDeploymentsByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
Expand All @@ -527,10 +521,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)
return nil
return handleNilResults("There are no deployments for environment '%s' in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName)
}
dataMain := output.Table{
Header: []string{"ID", "RemoteID", "Name", "Status", "Created", "Started", "Completed"},
Expand Down Expand Up @@ -571,6 +562,9 @@ var listTasksCmd = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}

tasks, err := lagoon.GetTasksByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
Expand All @@ -592,10 +586,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)
return nil
return handleNilResults("There are no tasks for environment '%s' in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName)
}
dataMain := output.Table{
Header: []string{"ID", "RemoteID", "Name", "Status", "Created", "Started", "Completed", "Service"},
Expand Down Expand Up @@ -809,6 +800,9 @@ var listInvokableTasks = &cobra.Command{
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}
tasks, err := lagoon.GetInvokableAdvancedTaskDefinitionsByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
return err
Expand All @@ -823,10 +817,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)
return nil
return handleNilResults("There are no user defined tasks for environment %s\n", cmd, cmdProjectEnvironment)
}
dataMain := output.Table{
Header: []string{"Task Name", "Description"},
Expand Down Expand Up @@ -877,10 +868,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)
return nil
return handleNilResults("There are no groups for project '%s'\n", cmd, cmdProjectName)
}

data := []output.Data{}
Expand Down Expand Up @@ -947,10 +935,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)
return nil
return handleNilResults("No associated projects found for organization '%s'\n", cmd, organizationName)
}

data := []output.Data{}
Expand Down Expand Up @@ -1012,10 +997,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)
return nil
return handleNilResults("No associated groups found for organization '%s'\n", cmd, organizationName)
}

data := []output.Data{}
Expand Down Expand Up @@ -1074,10 +1056,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)
return nil
return handleNilResults("No associated deploy targets found for organization '%s'\n", cmd, organizationName)
}

data := []output.Data{}
Expand Down Expand Up @@ -1188,10 +1167,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)
return nil
return handleNilResults("No associated users found for organization '%s'\n", cmd, organizationName)
}
data := []output.Data{}
for _, user := range *users {
Expand Down
30 changes: 20 additions & 10 deletions cmd/notificationsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ var listProjectEmailsCmd = &cobra.Command{
return err
}
if len(result.Name) == 0 {
outputOptions.Error = fmt.Sprintf("No project found for '%s'\n", cmdProjectName)
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
} else if len(result.Notifications.Email) == 0 {
outputOptions.Error = fmt.Sprintf("No email notificatons found for project: '%s'\n", cmdProjectName)
return handleNilResults("No email notificatons found for project: '%s'\n", cmd, cmdProjectName)
}

data := []output.Data{}
Expand Down Expand Up @@ -268,15 +268,24 @@ var deleteProjectEmailNotificationCmd = &cobra.Command{
if err := requiredInputCheck("Project name", cmdProjectName, "Notification name", name); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)

project, err := lagoon.GetProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}
if yesNo(fmt.Sprintf("You are attempting to delete email notification '%s' from project '%s', are you sure?", name, cmdProjectName)) {
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
notification := &schema.RemoveNotificationFromProjectInput{
NotificationType: schema.EmailNotification,
NotificationName: name,
Expand Down Expand Up @@ -315,6 +324,7 @@ var deleteEmailNotificationCmd = &cobra.Command{
if err := requiredInputCheck("Notification name", name); err != nil {
return err
}
// Todo: Verify notifcation name exists - requires #PR https://github.com/uselagoon/lagoon/pull/3740
if yesNo(fmt.Sprintf("You are attempting to delete email notification '%s', are you sure?", name)) {
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
Expand Down
Loading
Loading