From db2cfe953ba8e99dd352a386993161fd3f41a781 Mon Sep 17 00:00:00 2001 From: Ezekiel Date: Wed, 10 Apr 2024 13:32:41 +0800 Subject: [PATCH] updated modifyTeamsCmdF to return error instead of nil when encounting error (#26606) --- server/cmd/mmctl/commands/team.go | 21 +++++++++++++++------ server/cmd/mmctl/commands/team_e2e_test.go | 12 ++++++++---- server/cmd/mmctl/commands/team_test.go | 12 ++++++++---- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/server/cmd/mmctl/commands/team.go b/server/cmd/mmctl/commands/team.go index 332708605f..4aee20939d 100644 --- a/server/cmd/mmctl/commands/team.go +++ b/server/cmd/mmctl/commands/team.go @@ -336,6 +336,8 @@ func modifyTeamsCmdF(c client.Client, cmd *cobra.Command, args []string) error { return errors.New("must specify one of --private or --public") } + var errs *multierror.Error + // I = invite only (private) // O = open (public) privacy := model.TeamInvite @@ -346,17 +348,24 @@ func modifyTeamsCmdF(c client.Client, cmd *cobra.Command, args []string) error { teams := getTeamsFromTeamArgs(c, args) for i, team := range teams { if team == nil { - printer.PrintError("Unable to find team '" + args[i] + "'") + errs = multierror.Append(errs, errors.New("Unable to find team '"+args[i]+"'")) continue } - if updatedTeam, _, err := c.UpdateTeamPrivacy(context.TODO(), team.Id, privacy); err != nil { - printer.PrintError("Unable to modify team '" + team.Name + "' error: " + err.Error()) - } else { - printer.PrintT("Modified team '{{.Name}}'", updatedTeam) + + updatedTeam, _, err := c.UpdateTeamPrivacy(context.TODO(), team.Id, privacy) + if err != nil { + errs = multierror.Append(errs, errors.New("Unable to modify team '"+team.Name+"' error: "+err.Error())) + continue } + + printer.PrintT("Modified team '{{.Name}}'", updatedTeam) } - return nil + for _, err := range errs.WrappedErrors() { + printer.PrintError(err.Error()) + } + + return errs.ErrorOrNil() } func restoreTeamsCmdF(c client.Client, cmd *cobra.Command, args []string) error { diff --git a/server/cmd/mmctl/commands/team_e2e_test.go b/server/cmd/mmctl/commands/team_e2e_test.go index a37e1899d4..a865128fb6 100644 --- a/server/cmd/mmctl/commands/team_e2e_test.go +++ b/server/cmd/mmctl/commands/team_e2e_test.go @@ -182,10 +182,12 @@ func (s *MmctlE2ETestSuite) TestModifyTeamsCmdF() { cmd := &cobra.Command{} cmd.Flags().Bool("private", true, "") err := modifyTeamsCmdF(s.th.Client, cmd, []string{teamID}) - s.Require().NoError(err) + + expectedError := fmt.Sprintf("Unable to modify team '%s' error: You do not have the appropriate permissions.", s.th.BasicTeam.Name) + s.Require().ErrorContains(err, expectedError) s.Require().Contains( printer.GetErrorLines()[0], - fmt.Sprintf("Unable to modify team '%s' error: You do not have the appropriate permissions.", s.th.BasicTeam.Name), + expectedError, ) t, appErr := s.th.App.GetTeam(teamID) s.Require().Nil(appErr) @@ -199,10 +201,12 @@ func (s *MmctlE2ETestSuite) TestModifyTeamsCmdF() { cmd.Flags().Bool("private", true, "") s.th.LoginBasic2() err := modifyTeamsCmdF(s.th.Client, cmd, []string{teamID}) - s.Require().NoError(err) + + expectedError := fmt.Sprintf("Unable to modify team '%s' error: You do not have the appropriate permissions.", s.th.BasicTeam.Name) + s.Require().ErrorContains(err, expectedError) s.Require().Contains( printer.GetErrorLines()[0], - fmt.Sprintf("Unable to modify team '%s' error: You do not have the appropriate permissions.", s.th.BasicTeam.Name), + expectedError, ) t, appErr := s.th.App.GetTeam(teamID) s.Require().Nil(appErr) diff --git a/server/cmd/mmctl/commands/team_test.go b/server/cmd/mmctl/commands/team_test.go index 2080ed233f..71ad97c235 100644 --- a/server/cmd/mmctl/commands/team_test.go +++ b/server/cmd/mmctl/commands/team_test.go @@ -734,8 +734,10 @@ func (s *MmctlUnitTestSuite) TestModifyTeamsCmd() { cmd.Flags().Bool("private", true, "") err := modifyTeamsCmdF(s.client, cmd, []string{"team1"}) - s.Require().Nil(err) - s.Require().Equal("Unable to find team 'team1'", printer.GetErrorLines()[0]) + + expectedError := "Unable to find team 'team1'" + s.Require().ErrorContains(err, expectedError) + s.Require().Equal(expectedError, printer.GetErrorLines()[0]) }) s.Run("Modify teams, set to private", func() { @@ -823,8 +825,10 @@ func (s *MmctlUnitTestSuite) TestModifyTeamsCmd() { cmd.Flags().Bool("public", true, "") err := modifyTeamsCmdF(s.client, cmd, []string{"team1"}) - s.Require().Nil(err) - s.Require().Equal("Unable to modify team 'team1' error: an error occurred modifying a team", + + expectedError := "Unable to modify team 'team1' error: an error occurred modifying a team" + s.Require().ErrorContains(err, expectedError) + s.Require().Equal(expectedError, printer.GetErrorLines()[0]) }) }