Add user create command
Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
parent
efa052f8ad
commit
b09df25324
|
@ -21,14 +21,31 @@ import (
|
|||
"code.vikunja.io/api/pkg/initialize"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"fmt"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
userCreateFlagUsername string
|
||||
userCreateFlagEmail string
|
||||
userCreateFlagPassword string
|
||||
)
|
||||
|
||||
func init() {
|
||||
// User create flags
|
||||
userCreateCmd.Flags().StringVarP(&userCreateFlagUsername, "username", "u", "", "The username of the new user.")
|
||||
_ = userCreateCmd.MarkFlagRequired("username")
|
||||
userCreateCmd.Flags().StringVarP(&userCreateFlagEmail, "email", "e", "", "The email address of the new user.")
|
||||
_ = userCreateCmd.MarkFlagRequired("email")
|
||||
userCreateCmd.Flags().StringVarP(&userCreateFlagPassword, "password", "p", "", "The password of the new user. You will be asked to enter it if not provided through the flag.")
|
||||
|
||||
userCmd.AddCommand(userListCmd, userCreateCmd, userDeleteCmd, userUpdateCmd, userResetPasswordCmd, userChangeEnabledCmd)
|
||||
rootCmd.AddCommand(userCmd)
|
||||
}
|
||||
|
@ -83,6 +100,34 @@ var userCreateCmd = &cobra.Command{
|
|||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if userCreateFlagPassword == "" {
|
||||
fmt.Print("Enter Password: ")
|
||||
bytePW, err := terminal.ReadPassword(syscall.Stdin)
|
||||
if err != nil {
|
||||
log.Criticalf("Error reading password: %s", err)
|
||||
}
|
||||
fmt.Printf("\nConfirm Password: ")
|
||||
byteConfirmPW, err := terminal.ReadPassword(syscall.Stdin)
|
||||
if err != nil {
|
||||
log.Criticalf("Error reading password: %s", err)
|
||||
}
|
||||
if string(bytePW) != string(byteConfirmPW) {
|
||||
log.Critical("Passwords don't match!")
|
||||
}
|
||||
userCreateFlagPassword = strings.TrimSpace(string(bytePW))
|
||||
}
|
||||
|
||||
u := &user.User{
|
||||
Username: userCreateFlagUsername,
|
||||
Email: userCreateFlagEmail,
|
||||
Password: userCreateFlagPassword,
|
||||
}
|
||||
_, err := user.CreateUser(u)
|
||||
if err != nil {
|
||||
log.Criticalf("Error creating new user: %s", err)
|
||||
}
|
||||
|
||||
fmt.Printf("\nUser was created successfully.\n")
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue