create.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package user
  2. import (
  3. "github.com/gravitl/netmaker/cli/functions"
  4. "github.com/gravitl/netmaker/models"
  5. "github.com/spf13/cobra"
  6. )
  7. var userCreateCmd = &cobra.Command{
  8. Use: "create",
  9. Args: cobra.NoArgs,
  10. Short: "Create a new user",
  11. Long: `Create a new user`,
  12. Run: func(cmd *cobra.Command, args []string) {
  13. user := &models.User{UserName: username, Password: password, PlatformRoleID: models.UserRoleID(platformID)}
  14. if len(groups) > 0 {
  15. grMap := make(map[models.UserGroupID]struct{})
  16. for _, groupID := range groups {
  17. grMap[models.UserGroupID(groupID)] = struct{}{}
  18. }
  19. user.UserGroups = grMap
  20. }
  21. functions.PrettyPrint(functions.CreateUser(user))
  22. },
  23. }
  24. func init() {
  25. userCreateCmd.Flags().StringVar(&username, "name", "", "Name of the user")
  26. userCreateCmd.Flags().StringVar(&password, "password", "", "Password of the user")
  27. userCreateCmd.Flags().StringVarP(&platformID, "platform-role", "r", models.ServiceUser.String(),
  28. "Platform Role of the user; run `nmctl roles list` to see available user roles")
  29. userCreateCmd.MarkFlagRequired("name")
  30. userCreateCmd.MarkFlagRequired("password")
  31. userCreateCmd.PersistentFlags().StringToStringVarP(&networkRoles, "network-roles", "n", nil,
  32. "Mapping of networkID and list of roles user will be part of (comma separated)")
  33. userCreateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ? (deprecated v0.25.0 onwards)")
  34. userCreateCmd.Flags().StringArrayVarP(&groups, "groups", "g", nil, "List of user groups the user will be part of (comma separated)")
  35. rootCmd.AddCommand(userCreateCmd)
  36. }