wroofauth/cmd/addUser.go

60 lines
1.1 KiB
Go

package cmd
import (
"bufio"
"context"
"fmt"
"os"
"git.1in9.net/raider/wroofauth/internal/database"
"git.1in9.net/raider/wroofauth/internal/entities"
"git.1in9.net/raider/wroofauth/internal/entities/user"
"github.com/spf13/cobra"
)
// addUserCmd represents the generateKeys command
var addUserCmd = &cobra.Command{
Use: "add-user [username]",
Short: "Creates a user",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Usage()
return
}
ctx := context.Background()
database.MongoConnect(ctx)
database.RedisConnect(ctx)
entities.SetupEntityDatabases()
newUser := user.New()
newUser.Username = &args[0]
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Password for %s: ", args[0])
password, _, err := reader.ReadLine()
if err != nil {
fmt.Println(err)
return
}
passwordString := string(password)
newUser.SetPassword(passwordString)
newUser.Persist(context.Background())
fmt.Printf("User \"%s\" created. ID: %s\n", args[0], newUser.ID)
database.MongoDisconnect(ctx)
database.RedisDisconnect()
},
}
func init() {
rootCmd.AddCommand(addUserCmd)
}