Skip to content

Commit

Permalink
Updates error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed May 17, 2024
1 parent 1ff4128 commit 2fcc9c7
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 30 deletions.
24 changes: 18 additions & 6 deletions cmd/deploytarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -356,15 +358,19 @@ 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,
Organization: organization.ID,
}

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{}{
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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{}{
Expand Down
4 changes: 3 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
12 changes: 9 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
}
Expand Down
28 changes: 21 additions & 7 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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{}{
Expand Down
9 changes: 9 additions & 0 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "-"
Expand Down
24 changes: 18 additions & 6 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
Expand All @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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},
Expand All @@ -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{}{
Expand Down

0 comments on commit 2fcc9c7

Please sign in to comment.