diff --git a/cmd/deploytarget.go b/cmd/deploytarget.go index 4d7bc93c..527819e0 100644 --- a/cmd/deploytarget.go +++ b/cmd/deploytarget.go @@ -332,7 +332,9 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } organizationName, err := cmd.Flags().GetString("name") if err != nil { return err @@ -356,7 +358,9 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{ debug) organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } deployTargetInput := s.AddDeployTargetToOrganizationInput{ DeployTarget: deploytarget, @@ -364,7 +368,9 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{ } deployTargetResponse, err := l.AddDeployTargetToOrganization(context.TODO(), &deployTargetInput, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", ResultData: map[string]interface{}{ @@ -386,7 +392,9 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } organizationName, err := cmd.Flags().GetString("name") if err != nil { @@ -413,7 +421,9 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{ debug) organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{ DeployTarget: deploytarget, @@ -422,7 +432,9 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{ if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deploytarget, organization.Name)) { _, err := l.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", ResultData: map[string]interface{}{ diff --git a/cmd/get.go b/cmd/get.go index 6f067d99..5e2c1820 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -285,7 +285,9 @@ var getOrganizationCmd = &cobra.Command{ &token, debug) organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err != nil { + return err + } if organization.Name == "" { output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions) diff --git a/cmd/groups.go b/cmd/groups.go index 3bf22a4b..c0ac6d9f 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -40,7 +40,9 @@ var addGroupCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } groupName, err := cmd.Flags().GetString("name") if err != nil { return err @@ -68,20 +70,26 @@ var addGroupCmd = &cobra.Command{ if organizationName != "" { organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } groupInput := s.AddGroupToOrganizationInput{ Name: groupName, Organization: organization.ID, AddOrgOwner: orgOwner, } _, err = l.AddGroupToOrganization(context.TODO(), &groupInput, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } } else { groupInput := s.AddGroupInput{ Name: groupName, } _, err = l.AddGroup(context.TODO(), &groupInput, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } } resultData := output.Result{ diff --git a/cmd/list.go b/cmd/list.go index 552c0fb0..1b868ccb 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -779,7 +779,9 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ &token, debug) deployTargets, err := l.ListDeployTargetsByOrganizationNameOrID(context.TODO(), nullStrCheck(organizationName), nullUintCheck(organizationID), lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } if len(*deployTargets) == 0 { outputOptions.Error = fmt.Sprintf("No associated deploy targets found for organization '%s'\n", organizationName) } @@ -834,9 +836,13 @@ var ListOrganizationUsersCmd = &cobra.Command{ &token, debug) organization, err := l.GetOrganizationByName(context.Background(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } users, err := l.UsersByOrganization(context.TODO(), organization.ID, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } data := []output.Data{} for _, user := range *users { diff --git a/cmd/organization.go b/cmd/organization.go index e69587ed..b6e0e1d1 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -80,7 +80,9 @@ var addOrganizationCmd = &cobra.Command{ } org := s.Organization{} err = lc.AddOrganization(context.TODO(), &organizationInput, &org) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", @@ -123,10 +125,14 @@ var deleteOrganizationCmd = &cobra.Command{ debug) organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } if yesNo(fmt.Sprintf("You are attempting to delete organization '%s', are you sure?", organization.Name)) { _, err := l.DeleteOrganization(context.TODO(), organization.ID, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: organization.Name, } diff --git a/cmd/project.go b/cmd/project.go index b6f485aa..9ec1c1ac 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -73,7 +73,9 @@ var addProjectCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } organizationName, err := cmd.Flags().GetString("organization") if err != nil { @@ -169,13 +171,17 @@ var addProjectCmd = &cobra.Command{ if organizationName != "" { organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } projectInput.Organization = organization.ID } project := s.Project{} err = lc.AddProject(context.TODO(), &projectInput, &project) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", ResultData: map[string]interface{}{ @@ -475,7 +481,9 @@ var removeProjectFromOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } organizationName, err := cmd.Flags().GetString("organization") if err != nil { @@ -495,9 +503,13 @@ var removeProjectFromOrganizationCmd = &cobra.Command{ debug) project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } projectInput := s.RemoveProjectFromOrganizationInput{ Project: project.ID, @@ -506,7 +518,9 @@ var removeProjectFromOrganizationCmd = &cobra.Command{ if yesNo(fmt.Sprintf("You are attempting to remove project '%s' from organization '%s'. This will return the project to a state where it has no groups or notifications associated, are you sure?", cmdProjectName, organization.Name)) { _, err := l.RemoveProjectFromOrganization(context.TODO(), &projectInput, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", ResultData: map[string]interface{}{ diff --git a/cmd/shared.go b/cmd/shared.go index 1d28cbc3..8500f653 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -53,6 +53,15 @@ func handleError(err error) { } } +func handleErr(err error) error { + if err != nil { + outputOptions.Error = err.Error() + output.RenderError(outputOptions.Error, outputOptions) + return err + } + return nil +} + func returnNonEmptyString(value string) string { if len(value) == 0 { return "-" diff --git a/cmd/users.go b/cmd/users.go index 965def99..499cdd8b 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -376,7 +376,9 @@ var addAdministratorToOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } organizationName, err := cmd.Flags().GetString("name") if err != nil { @@ -404,7 +406,9 @@ var addAdministratorToOrganizationCmd = &cobra.Command{ debug) organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } userInput := s.AddUserToOrganizationInput{ User: s.UserInput{Email: userEmail}, @@ -414,7 +418,9 @@ var addAdministratorToOrganizationCmd = &cobra.Command{ orgUser := s.Organization{} err = lc.AddUserToOrganization(context.TODO(), &userInput, &orgUser) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", @@ -437,7 +443,9 @@ var removeAdministratorFromOrganizationCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { debug, err := cmd.Flags().GetBool("debug") - handleError(err) + if err != nil { + return err + } organizationName, err := cmd.Flags().GetString("name") if err != nil { @@ -468,7 +476,9 @@ var removeAdministratorFromOrganizationCmd = &cobra.Command{ debug) organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } userInput := s.AddUserToOrganizationInput{ User: s.UserInput{Email: userEmail}, @@ -480,7 +490,9 @@ var removeAdministratorFromOrganizationCmd = &cobra.Command{ if yesNo(fmt.Sprintf("You are attempting to remove user '%s' from organization '%s'. This removes the users ability to view or manage the organizations groups, projects, & notifications, are you sure?", userEmail, organization.Name)) { err = lc.RemoveUserFromOrganization(context.TODO(), &userInput, &orgUser) - handleError(err) + if err := handleErr(err); err != nil { + return nil + } resultData := output.Result{ Result: "success", ResultData: map[string]interface{}{