diff --git a/cmd/mattermost/commands/channel.go b/cmd/mattermost/commands/channel.go index 3da8ecc799..dc47a3b000 100644 --- a/cmd/mattermost/commands/channel.go +++ b/cmd/mattermost/commands/channel.go @@ -116,6 +116,17 @@ Channel can be specified by [team]:[channel]. ie. myteam:mychannel or by channel RunE: modifyChannelCmdF, } +var SearchChannelCmd = &cobra.Command{ + Use: "search [channel]\n mattermost search --team [team] [channel]", + Short: "Search a channel", + Long: `Search a channel by channel name. +Channel can be specified by team. ie. --team myTeam myChannel or by team ID.`, + Example: ` channel search myChannel + channel search --team myTeam myChannel`, + Args: cobra.ExactArgs(1), + RunE: searchChannelCmdF, +} + func init() { ChannelCreateCmd.Flags().String("name", "", "Channel Name") ChannelCreateCmd.Flags().String("display_name", "", "Channel Display Name") @@ -134,6 +145,7 @@ func init() { ModifyChannelCmd.Flags().String("username", "", "Required. Username who changes the channel privacy.") ChannelRenameCmd.Flags().String("display_name", "", "Channel Display Name") + SearchChannelCmd.Flags().String("team", "", "Team name or ID") RemoveChannelUsersCmd.Flags().Bool("all-users", false, "Remove all users from the indicated channel.") @@ -148,6 +160,7 @@ func init() { RestoreChannelsCmd, ModifyChannelCmd, ChannelRenameCmd, + SearchChannelCmd, ) RootCmd.AddCommand(ChannelCmd) @@ -537,3 +550,53 @@ func renameChannelCmdF(command *cobra.Command, args []string) error { return nil } + +func searchChannelCmdF(command *cobra.Command, args []string) error { + + a, err := InitDBCommandContextCobra(command) + if err != nil { + return errors.Wrap(err, "failed to InitDBCommandContextCobra") + } + defer a.Shutdown() + + var channel *model.Channel + + if teamArg, _ := command.Flags().GetString("team"); teamArg != "" { + team := getTeamFromTeamArg(a, teamArg) + if team == nil { + CommandPrettyPrintln(fmt.Sprintf("Team %s is not found", teamArg)) + return nil + } + + var aErr *model.AppError + channel, aErr = a.GetChannelByName(args[0], team.Id, true) + if aErr != nil || channel == nil { + CommandPrettyPrintln(fmt.Sprintf("Channel %s is not found in team %s", args[0], teamArg)) + return nil + } + } else { + teams, aErr := a.GetAllTeams() + if aErr != nil { + return errors.Wrap(err, "failed to GetAllTeams") + } + + for _, team := range teams { + channel, _ = a.GetChannelByName(args[0], team.Id, true) + if channel != nil && channel.Name == args[0] { + break + } + } + + if channel == nil { + CommandPrettyPrintln(fmt.Sprintf("Channel %s is not found in any team", args[0])) + return nil + } + } + + if channel.DeleteAt > 0 { + CommandPrettyPrintln(fmt.Sprintf(`Channel Name :%s, Display Name :%s, Channel ID :%s (archived)`, channel.Name, channel.DisplayName, channel.Id)) + } else { + CommandPrettyPrintln(fmt.Sprintf(`Channel Name :%s, Display Name :%s, Channel ID :%s`, channel.Name, channel.DisplayName, channel.Id)) + } + return nil +} diff --git a/cmd/mattermost/commands/channel_test.go b/cmd/mattermost/commands/channel_test.go index 5ac338b668..31c49864be 100644 --- a/cmd/mattermost/commands/channel_test.go +++ b/cmd/mattermost/commands/channel_test.go @@ -4,6 +4,7 @@ package commands import ( + "fmt" "strings" "testing" "time" @@ -131,3 +132,70 @@ func TestRenameChannel(t *testing.T) { assert.Equal(t, "newchannelname10", updatedChannel.Name) assert.Equal(t, "New Display Name", updatedChannel.DisplayName) } + +func Test_searchChannelCmdF(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + channel := th.CreatePublicChannel() + channel2 := th.CreatePublicChannel() + th.Client.DeleteChannel(channel2.Id) + + tests := []struct { + Name string + Args []string + Expected string + }{ + { + "Success find Channel in any team", + []string{"channel", "search", channel.Name}, + fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id), + }, + { + "Failed find Channel in any team", + []string{"channel", "search", channel.Name + "404"}, + fmt.Sprintf("Channel %s is not found in any team", channel.Name+"404"), + }, + { + "Success find Channel with param team ID", + []string{"channel", "search", "--team", channel.TeamId, channel.Name}, + fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id), + }, + { + "Failed find Channel with param team ID", + []string{"channel", "search", "--team", channel.TeamId, channel.Name + "404"}, + fmt.Sprintf("Channel %s is not found in team %s", channel.Name+"404", channel.TeamId), + }, + { + "Success find archived Channel in any team", + []string{"channel", "search", channel2.Name}, + fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s (archived)", channel2.Name, channel2.DisplayName, channel2.Id), + }, + { + "Success find archived Channel with param team ID", + []string{"channel", "search", "--team", channel2.TeamId, channel2.Name}, + fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s (archived)", channel2.Name, channel2.DisplayName, channel2.Id), + }, + { + "Failed find team", + []string{"channel", "search", "--team", channel.TeamId + "404", channel.Name}, + fmt.Sprintf("Team %s is not found", channel.TeamId+"404"), + }, + { + "Success find Channel with param team ID", + []string{"channel", "search", channel.Name, "--team", channel.TeamId}, + fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id), + }, + { + "Success find Channel with param team ID", + []string{"channel", "search", channel.Name, "--team=" + channel.TeamId}, + fmt.Sprintf("Channel Name :%s, Display Name :%s, Channel ID :%s", channel.Name, channel.DisplayName, channel.Id), + }, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + assert.Contains(t, th.CheckCommand(t, test.Args...), test.Expected) + }) + } +}