create.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package user
  2. import (
  3. "strings"
  4. "github.com/gravitl/netmaker/cli/functions"
  5. "github.com/gravitl/netmaker/models"
  6. "github.com/spf13/cobra"
  7. )
  8. var userCreateCmd = &cobra.Command{
  9. Use: "create",
  10. Args: cobra.NoArgs,
  11. Short: "Create a new user",
  12. Long: `Create a new user`,
  13. Run: func(cmd *cobra.Command, args []string) {
  14. user := &models.User{UserName: username, Password: password, IsAdmin: admin}
  15. if networks != "" {
  16. user.Networks = strings.Split(networks, ",")
  17. }
  18. if groups != "" {
  19. user.Groups = strings.Split(groups, ",")
  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.MarkFlagRequired("name")
  28. userCreateCmd.MarkFlagRequired("password")
  29. userCreateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ?")
  30. userCreateCmd.Flags().StringVar(&networks, "networks", "", "List of networks the user will access to (comma separated)")
  31. userCreateCmd.Flags().StringVar(&groups, "groups", "", "List of user groups the user will be part of (comma separated)")
  32. rootCmd.AddCommand(userCreateCmd)
  33. }