github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/team.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/mattermost/mattermost-server/app"
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var TeamCmd = &cobra.Command{
    16  	Use:   "team",
    17  	Short: "Management of teams",
    18  }
    19  
    20  var TeamCreateCmd = &cobra.Command{
    21  	Use:   "create",
    22  	Short: "Create a team",
    23  	Long:  `Create a team.`,
    24  	Example: `  team create --name mynewteam --display_name "My New Team"
    25    team create --name private --display_name "My New Private Team" --private`,
    26  	RunE: createTeamCmdF,
    27  }
    28  
    29  var RemoveUsersCmd = &cobra.Command{
    30  	Use:     "remove [team] [users]",
    31  	Short:   "Remove users from team",
    32  	Long:    "Remove some users from team",
    33  	Example: "  team remove myteam user@example.com username",
    34  	RunE:    removeUsersCmdF,
    35  }
    36  
    37  var AddUsersCmd = &cobra.Command{
    38  	Use:     "add [team] [users]",
    39  	Short:   "Add users to team",
    40  	Long:    "Add some users to team",
    41  	Example: "  team add myteam user@example.com username",
    42  	RunE:    addUsersCmdF,
    43  }
    44  
    45  var DeleteTeamsCmd = &cobra.Command{
    46  	Use:   "delete [teams]",
    47  	Short: "Delete teams",
    48  	Long: `Permanently delete some teams.
    49  Permanently deletes a team along with all related information including posts from the database.`,
    50  	Example: "  team delete myteam",
    51  	RunE:    deleteTeamsCmdF,
    52  }
    53  
    54  var ListTeamsCmd = &cobra.Command{
    55  	Use:     "list",
    56  	Short:   "List all teams.",
    57  	Long:    `List all teams on the server.`,
    58  	Example: "  team list",
    59  	RunE:    listTeamsCmdF,
    60  }
    61  
    62  func init() {
    63  	TeamCreateCmd.Flags().String("name", "", "Team Name")
    64  	TeamCreateCmd.Flags().String("display_name", "", "Team Display Name")
    65  	TeamCreateCmd.Flags().Bool("private", false, "Create a private team.")
    66  	TeamCreateCmd.Flags().String("email", "", "Administrator Email (anyone with this email is automatically a team admin)")
    67  
    68  	DeleteTeamsCmd.Flags().Bool("confirm", false, "Confirm you really want to delete the team and a DB backup has been performed.")
    69  
    70  	TeamCmd.AddCommand(
    71  		TeamCreateCmd,
    72  		RemoveUsersCmd,
    73  		AddUsersCmd,
    74  		DeleteTeamsCmd,
    75  		ListTeamsCmd,
    76  	)
    77  	RootCmd.AddCommand(TeamCmd)
    78  }
    79  
    80  func createTeamCmdF(command *cobra.Command, args []string) error {
    81  	a, err := InitDBCommandContextCobra(command)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	defer a.Shutdown()
    86  
    87  	name, errn := command.Flags().GetString("name")
    88  	if errn != nil || name == "" {
    89  		return errors.New("Name is required")
    90  	}
    91  	displayname, errdn := command.Flags().GetString("display_name")
    92  	if errdn != nil || displayname == "" {
    93  		return errors.New("Display Name is required")
    94  	}
    95  	email, _ := command.Flags().GetString("email")
    96  	useprivate, _ := command.Flags().GetBool("private")
    97  
    98  	teamType := model.TEAM_OPEN
    99  	if useprivate {
   100  		teamType = model.TEAM_INVITE
   101  	}
   102  
   103  	team := &model.Team{
   104  		Name:        name,
   105  		DisplayName: displayname,
   106  		Email:       email,
   107  		Type:        teamType,
   108  	}
   109  
   110  	if _, err := a.CreateTeam(team); err != nil {
   111  		return errors.New("Team creation failed: " + err.Error())
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func removeUsersCmdF(command *cobra.Command, args []string) error {
   118  	a, err := InitDBCommandContextCobra(command)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	defer a.Shutdown()
   123  
   124  	if len(args) < 2 {
   125  		return errors.New("Not enough arguments.")
   126  	}
   127  
   128  	team := getTeamFromTeamArg(a, args[0])
   129  	if team == nil {
   130  		return errors.New("Unable to find team '" + args[0] + "'")
   131  	}
   132  
   133  	users := getUsersFromUserArgs(a, args[1:])
   134  	for i, user := range users {
   135  		removeUserFromTeam(a, team, user, args[i+1])
   136  	}
   137  
   138  	return nil
   139  }
   140  
   141  func removeUserFromTeam(a *app.App, team *model.Team, user *model.User, userArg string) {
   142  	if user == nil {
   143  		CommandPrintErrorln("Can't find user '" + userArg + "'")
   144  		return
   145  	}
   146  	if err := a.LeaveTeam(team, user, ""); err != nil {
   147  		CommandPrintErrorln("Unable to remove '" + userArg + "' from " + team.Name + ". Error: " + err.Error())
   148  	}
   149  }
   150  
   151  func addUsersCmdF(command *cobra.Command, args []string) error {
   152  	a, err := InitDBCommandContextCobra(command)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	defer a.Shutdown()
   157  
   158  	if len(args) < 2 {
   159  		return errors.New("Not enough arguments.")
   160  	}
   161  
   162  	team := getTeamFromTeamArg(a, args[0])
   163  	if team == nil {
   164  		return errors.New("Unable to find team '" + args[0] + "'")
   165  	}
   166  
   167  	users := getUsersFromUserArgs(a, args[1:])
   168  	for i, user := range users {
   169  		addUserToTeam(a, team, user, args[i+1])
   170  	}
   171  
   172  	return nil
   173  }
   174  
   175  func addUserToTeam(a *app.App, team *model.Team, user *model.User, userArg string) {
   176  	if user == nil {
   177  		CommandPrintErrorln("Can't find user '" + userArg + "'")
   178  		return
   179  	}
   180  	if err := a.JoinUserToTeam(team, user, ""); err != nil {
   181  		CommandPrintErrorln("Unable to add '" + userArg + "' to " + team.Name)
   182  	}
   183  }
   184  
   185  func deleteTeamsCmdF(command *cobra.Command, args []string) error {
   186  	a, err := InitDBCommandContextCobra(command)
   187  	if err != nil {
   188  		return err
   189  	}
   190  	defer a.Shutdown()
   191  
   192  	if len(args) < 1 {
   193  		return errors.New("Not enough arguments.")
   194  	}
   195  
   196  	confirmFlag, _ := command.Flags().GetBool("confirm")
   197  	if !confirmFlag {
   198  		var confirm string
   199  		CommandPrettyPrintln("Have you performed a database backup? (YES/NO): ")
   200  		fmt.Scanln(&confirm)
   201  
   202  		if confirm != "YES" {
   203  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
   204  		}
   205  		CommandPrettyPrintln("Are you sure you want to delete the teams specified?  All data will be permanently deleted? (YES/NO): ")
   206  		fmt.Scanln(&confirm)
   207  		if confirm != "YES" {
   208  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
   209  		}
   210  	}
   211  
   212  	teams := getTeamsFromTeamArgs(a, args)
   213  	for i, team := range teams {
   214  		if team == nil {
   215  			CommandPrintErrorln("Unable to find team '" + args[i] + "'")
   216  			continue
   217  		}
   218  		if err := deleteTeam(a, team); err != nil {
   219  			CommandPrintErrorln("Unable to delete team '" + team.Name + "' error: " + err.Error())
   220  		} else {
   221  			CommandPrettyPrintln("Deleted team '" + team.Name + "'")
   222  		}
   223  	}
   224  
   225  	return nil
   226  }
   227  
   228  func deleteTeam(a *app.App, team *model.Team) *model.AppError {
   229  	return a.PermanentDeleteTeam(team)
   230  }
   231  
   232  func listTeamsCmdF(command *cobra.Command, args []string) error {
   233  	a, err := InitDBCommandContextCobra(command)
   234  	if err != nil {
   235  		return err
   236  	}
   237  	defer a.Shutdown()
   238  
   239  	teams, err2 := a.GetAllTeams()
   240  	if err2 != nil {
   241  		return err2
   242  	}
   243  
   244  	for _, team := range teams {
   245  		CommandPrettyPrintln(team.Name)
   246  	}
   247  
   248  	return nil
   249  }