From b6835ab984aece679cb0d6bea548d3f2ed1c9af2 Mon Sep 17 00:00:00 2001 From: Wasim Thabraze Date: Wed, 3 Oct 2018 13:01:53 +0530 Subject: [MM-12367] Added CLI command 'team search' (#9512) * Added 'search' sub-command for the command 'team' to search across teams with name * Addressed code review * Moved 'removeDuplicatesAndSortTeams' function to team.go Addressed more code reviews * Added unit test case for team search command * Added unit test case to test searching of teams by display name --- cmd/mattermost/commands/team.go | 53 ++++++++++++++++++++++++++++++++++++ cmd/mattermost/commands/team_test.go | 34 +++++++++++++++++++++++ 2 files changed, 87 insertions(+) (limited to 'cmd') diff --git a/cmd/mattermost/commands/team.go b/cmd/mattermost/commands/team.go index 59570d5e7..587062534 100644 --- a/cmd/mattermost/commands/team.go +++ b/cmd/mattermost/commands/team.go @@ -6,6 +6,7 @@ package commands import ( "errors" "fmt" + "sort" "github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/model" @@ -59,6 +60,15 @@ var ListTeamsCmd = &cobra.Command{ RunE: listTeamsCmdF, } +var SearchTeamCmd = &cobra.Command{ + Use: "search [teams]", + Short: "Search for teams", + Long: "Search for teams based on name", + Example: " team search team1", + Args: cobra.MinimumNArgs(1), + RunE: searchTeamCmdF, +} + func init() { TeamCreateCmd.Flags().String("name", "", "Team Name") TeamCreateCmd.Flags().String("display_name", "", "Team Display Name") @@ -73,6 +83,7 @@ func init() { AddUsersCmd, DeleteTeamsCmd, ListTeamsCmd, + SearchTeamCmd, ) RootCmd.AddCommand(TeamCmd) } @@ -247,3 +258,45 @@ func listTeamsCmdF(command *cobra.Command, args []string) error { return nil } + +func searchTeamCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + var teams []*model.Team + + for _, searchTerm := range args { + foundTeams, err := a.SearchAllTeams(searchTerm) + if err != nil { + return err + } + teams = append(teams, foundTeams...) + } + + sortedTeams := removeDuplicatesAndSortTeams(teams) + + for _, team := range sortedTeams { + CommandPrettyPrintln(team.Name + ": " + team.DisplayName + " (" + team.Id + ")") + } + + return nil +} + +// Removes duplicates and sorts teams by name +func removeDuplicatesAndSortTeams(teams []*model.Team) []*model.Team { + keys := make(map[string]bool) + result := []*model.Team{} + for _, team := range teams { + if _, value := keys[team.Name]; !value { + keys[team.Name] = true + result = append(result, team) + } + } + sort.Slice(result, func(i, j int) bool { + return result[i].Name < result[j].Name + }) + return result +} diff --git a/cmd/mattermost/commands/team_test.go b/cmd/mattermost/commands/team_test.go index 20e04bdc9..559198256 100644 --- a/cmd/mattermost/commands/team_test.go +++ b/cmd/mattermost/commands/team_test.go @@ -95,3 +95,37 @@ func TestListTeams(t *testing.T) { t.Fatal("should have the created team") } } + +func TestSearchTeamsByName(t *testing.T) { + th := api4.Setup().InitBasic() + defer th.TearDown() + + id := model.NewId() + name := "name" + id + displayName := "Name " + id + + CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName) + + output := CheckCommand(t, "team", "search", name) + + if !strings.Contains(string(output), name) { + t.Fatal("should have the created team") + } +} + +func TestSearchTeamsByDisplayName(t *testing.T) { + th := api4.Setup().InitBasic() + defer th.TearDown() + + id := model.NewId() + name := "name" + id + displayName := "Name " + id + + CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName) + + output := CheckCommand(t, "team", "search", displayName) + + if !strings.Contains(string(output), name) { + t.Fatal("should have the created team") + } +} -- cgit v1.2.3-1-g7c22