NewPasswordsCommand creates and returns the cobra command for creating a password entry.
()
| 13 | |
| 14 | // NewPasswordsCommand creates and returns the cobra command for creating a password entry. |
| 15 | func NewPasswordsCommand() *cobra.Command { |
| 16 | cmd := &cobra.Command{ |
| 17 | Use: "passwords", |
| 18 | Args: cobra.NoArgs, |
| 19 | Short: "Create an alternative password", |
| 20 | Long: "Create an alternative password (interactive prompt)", |
| 21 | Aliases: []string{"password", "pass", "pwd"}, |
| 22 | Run: func(_ *cobra.Command, _ []string) { |
| 23 | fmt.Print("Enter password: ") |
| 24 | pwdBytes, err := term.ReadPassword(int(os.Stdin.Fd())) |
| 25 | fmt.Println() |
| 26 | if err != nil { |
| 27 | utils.Fatalln("Failed to read password:", err) |
| 28 | } |
| 29 | password := string(pwdBytes) |
| 30 | for i := range pwdBytes { |
| 31 | pwdBytes[i] = 0 |
| 32 | } |
| 33 | if password == "" { |
| 34 | utils.Fatalln("Password cannot be empty") |
| 35 | } |
| 36 | |
| 37 | fmt.Print("Confirm password: ") |
| 38 | confirmBytes, err := term.ReadPassword(int(os.Stdin.Fd())) |
| 39 | fmt.Println() |
| 40 | if err != nil { |
| 41 | utils.Fatalln("Failed to read password:", err) |
| 42 | } |
| 43 | match := string(confirmBytes) == password |
| 44 | for i := range confirmBytes { |
| 45 | confirmBytes[i] = 0 |
| 46 | } |
| 47 | if !match { |
| 48 | utils.Fatalln("Passwords do not match") |
| 49 | } |
| 50 | |
| 51 | configuration, err := config.LoadConfig() |
| 52 | if err != nil { |
| 53 | utils.Fatalln(err) |
| 54 | } |
| 55 | controller := control.NewCreateController(control.TypePasswords, password, configuration) |
| 56 | controller.ExecuteCreate() |
| 57 | }, |
| 58 | } |
| 59 | return cmd |
| 60 | } |