updated modifyTeamsCmdF to return error instead of nil when encounting error (#26606)

This commit is contained in:
Ezekiel 2024-04-10 13:32:41 +08:00 committed by GitHub
parent 65c6717e6b
commit db2cfe953b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 14 deletions

View File

@ -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
}
return nil
printer.PrintT("Modified team '{{.Name}}'", updatedTeam)
}
for _, err := range errs.WrappedErrors() {
printer.PrintError(err.Error())
}
return errs.ErrorOrNil()
}
func restoreTeamsCmdF(c client.Client, cmd *cobra.Command, args []string) error {

View File

@ -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)

View File

@ -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])
})
}