()
| 28 | } |
| 29 | |
| 30 | func createUserCmd() *cli.Command { |
| 31 | tCfg := NewCreateUserConfig() |
| 32 | |
| 33 | loaders := []cli.ResourceLoader{ |
| 34 | &cli.FlagLoader{}, |
| 35 | } |
| 36 | |
| 37 | return &cli.Command{ |
| 38 | Name: "create", |
| 39 | Description: "Create a user", |
| 40 | Configuration: tCfg, |
| 41 | Resources: loaders, |
| 42 | Run: func(_ []string) error { |
| 43 | tlog.NewSimpleLogger().Init() |
| 44 | |
| 45 | if tCfg.Interactive { |
| 46 | form := huh.NewForm( |
| 47 | huh.NewGroup( |
| 48 | huh.NewInput().Title("Username").Value(&tCfg.Username).Validate((func(s string) error { |
| 49 | if s == "" { |
| 50 | return errors.New("username cannot be empty") |
| 51 | } |
| 52 | return nil |
| 53 | })), |
| 54 | huh.NewInput().Title("Password").Value(&tCfg.Password).Validate((func(s string) error { |
| 55 | if s == "" { |
| 56 | return errors.New("password cannot be empty") |
| 57 | } |
| 58 | return nil |
| 59 | })), |
| 60 | huh.NewSelect[bool]().Title("Format the output for Docker?").Options(huh.NewOption("Yes", true), huh.NewOption("No", false)).Value(&tCfg.Docker), |
| 61 | ), |
| 62 | ) |
| 63 | |
| 64 | theme := new(themeBase) |
| 65 | err := form.WithTheme(theme).Run() |
| 66 | |
| 67 | if err != nil { |
| 68 | return fmt.Errorf("failed to run interactive prompt: %w", err) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if tCfg.Username == "" || tCfg.Password == "" { |
| 73 | return errors.New("username and password cannot be empty") |
| 74 | } |
| 75 | |
| 76 | tlog.App.Info().Str("username", tCfg.Username).Msg("Creating user") |
| 77 | |
| 78 | passwd, err := bcrypt.GenerateFromPassword([]byte(tCfg.Password), bcrypt.DefaultCost) |
| 79 | if err != nil { |
| 80 | return fmt.Errorf("failed to hash password: %w", err) |
| 81 | } |
| 82 | |
| 83 | // If docker format is enabled, escape the dollar sign |
| 84 | passwdStr := string(passwd) |
| 85 | if tCfg.Docker { |
| 86 | passwdStr = strings.ReplaceAll(passwdStr, "$", "$$") |
| 87 | } |
no test coverage detected