[MM-12365] Create CLI command "team rename" (#9764) (#10083)

Allow renaming teams like in `channel rename` command

Example: `team rename myteam newteamname --display_name "My New Team Name`
Этот коммит содержится в:
Dmytro Chukmasov
2019-03-06 19:48:49 +02:00
коммит произвёл Hanzei
родитель 06b579d18a
Коммит 3716918c57
4 изменённых файлов: 143 добавлений и 1 удалений

Просмотреть файл

@@ -90,6 +90,16 @@ var RestoreTeamsCmd = &cobra.Command{
RunE: restoreTeamsCmdF,
}
var TeamRenameCmd = &cobra.Command{
Use: "rename",
Short: "Rename a team",
Long: `Rename a team.`,
Example: ` team rename myteam newteamname --display_name "My New Team Name"
team rename myteam - --display_name "My New Team Name"`,
Args: cobra.MinimumNArgs(2),
RunE: renameTeamCmdF,
}
func init() {
TeamCreateCmd.Flags().String("name", "", "Team Name")
TeamCreateCmd.Flags().String("display_name", "", "Team Display Name")
@@ -98,6 +108,8 @@ func init() {
DeleteTeamsCmd.Flags().Bool("confirm", false, "Confirm you really want to delete the team and a DB backup has been performed.")
TeamRenameCmd.Flags().String("display_name", "", "Team Display Name")
TeamCmd.AddCommand(
TeamCreateCmd,
RemoveUsersCmd,
@@ -107,6 +119,7 @@ func init() {
SearchTeamCmd,
ArchiveTeamCmd,
RestoreTeamsCmd,
TeamRenameCmd,
)
RootCmd.AddCommand(TeamCmd)
}
@@ -362,3 +375,38 @@ func archiveTeamCmdF(command *cobra.Command, args []string) error {
return nil
}
func renameTeamCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
if err != nil {
return err
}
defer a.Shutdown()
team := getTeamFromTeamArg(a, args[0])
if team == nil {
return errors.New("Unable to find team '" + args[0] + "'")
}
var newDisplayName, newTeamName string
newTeamName = args[1]
// let user use old team Name when only Display Name change is wanted
if newTeamName == team.Name {
newTeamName = "-"
}
newDisplayName, errdn := command.Flags().GetString("display_name")
if errdn != nil {
return errdn
}
_, errrt := a.RenameTeam(team, newTeamName, newDisplayName)
if errrt != nil {
CommandPrintErrorln("Unable to rename team to '"+newTeamName+"' error: ", errrt)
}
return nil
}

Просмотреть файл

@@ -205,3 +205,70 @@ func TestRestoreTeams(t *testing.T) {
require.True(t, found)
}
func TestRenameTeam(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
team := th.CreateTeam()
newTeamName := "newteamnamex3"
newDisplayName := "New Display NameX"
th.CheckCommand(t, "team", "rename", team.Name, newTeamName, "--display_name", newDisplayName)
// Get the team from the DB
updatedTeam, _ := th.App.GetTeam(team.Id)
if updatedTeam.Name != newTeamName {
t.Fatal("failed renaming team")
}
if updatedTeam.DisplayName != newDisplayName {
t.Fatal("failed updating team display name")
}
// Try to rename to occupied name
team2 := th.CreateTeam()
n := team2.Name
dn := team2.DisplayName
th.CheckCommand(t, "team", "rename", team2.Name, newTeamName, "--display_name", newDisplayName)
// No renaming should have occured
if team2.Name != n {
t.Fatal("team was renamed when it should have not been")
}
if team2.DisplayName != dn {
t.Fatal("team display name was changed when it should have not been")
}
// Try to change only Display Name
team3 := th.CreateTeam()
// trying to change only Display Name (using "-" as a new team name)
th.CheckCommand(t, "team", "rename", team3.Name, "-", "--display_name", newDisplayName)
// Get the team from the DB
updatedTeam, _ = th.App.GetTeam(team3.Id)
if updatedTeam.Name == "-" {
t.Fatal("team was renamed to `-` but only display name should have been changed")
}
if updatedTeam.DisplayName != newDisplayName {
t.Fatal("team Display Name was not properly updated")
}
// now try to change Display Name using old team name
th.CheckCommand(t, "team", "rename", team3.Name, team3.Name, "--display_name", "Brand New DName")
// Get the team from the DB
updatedTeam, _ = th.App.GetTeam(team3.Id)
if updatedTeam.DisplayName != "Brand New DName" {
t.Fatal("team Display Name was not properly updated")
}
}