Semaphore/cli/cmd/user_add.go

66 lines
1.7 KiB
Go
Raw Normal View History

2021-08-25 22:12:19 +02:00
package cmd
import (
"fmt"
"github.com/ansible-semaphore/semaphore/db"
"github.com/spf13/cobra"
"os"
2021-08-25 22:12:19 +02:00
)
func init() {
userAddCmd.PersistentFlags().StringVar(&targetUserArgs.login, "login", "", "New user login")
userAddCmd.PersistentFlags().StringVar(&targetUserArgs.name, "name", "", "New user name")
userAddCmd.PersistentFlags().StringVar(&targetUserArgs.email, "email", "", "New user email")
userAddCmd.PersistentFlags().StringVar(&targetUserArgs.password, "password", "", "New user password")
userAddCmd.PersistentFlags().BoolVar(&targetUserArgs.admin, "admin", false, "Mark new user as admin")
2021-08-25 22:12:19 +02:00
userCmd.AddCommand(userAddCmd)
}
var userAddCmd = &cobra.Command{
Use: "add",
2021-08-27 10:50:26 +02:00
Short: "Add new user",
2021-08-25 22:12:19 +02:00
Run: func(cmd *cobra.Command, args []string) {
ok := true
if targetUserArgs.name == "" {
fmt.Println("Argument --name required")
ok = false
}
if targetUserArgs.login == "" {
fmt.Println("Argument --login required")
ok = false
}
if targetUserArgs.email == "" {
fmt.Println("Argument --email required")
ok = false
}
if targetUserArgs.password == "" {
fmt.Println("Argument --password required")
ok = false
}
if !ok {
fmt.Println("Use command `semaphore user add --help` for details.")
os.Exit(1)
}
2021-08-25 22:12:19 +02:00
store := createStore()
defer store.Close()
if _, err := store.CreateUser(db.UserWithPwd{
Pwd: targetUserArgs.password,
2021-08-25 22:12:19 +02:00
User: db.User{
Name: targetUserArgs.name,
Username: targetUserArgs.login,
Email: targetUserArgs.email,
Admin: targetUserArgs.admin,
2021-08-25 22:12:19 +02:00
},
}); err != nil {
panic(err)
}
fmt.Printf("User %s <%s> added!\n", targetUserArgs.login, targetUserArgs.email)
2021-08-25 22:12:19 +02:00
},
}