Semaphore/cli/cmd/user_add.go

67 lines
1.5 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
)
2021-08-27 10:50:26 +02:00
var newUserArgs userArgs
2021-08-25 22:12:19 +02:00
func init() {
2021-08-27 10:50:26 +02:00
userAddCmd.PersistentFlags().StringVar(&newUserArgs.login, "login", "", "New user login")
userAddCmd.PersistentFlags().StringVar(&newUserArgs.name, "name", "", "New user name")
userAddCmd.PersistentFlags().StringVar(&newUserArgs.email, "email", "", "New user email")
userAddCmd.PersistentFlags().StringVar(&newUserArgs.password, "password", "", "New user password")
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 newUserArgs.name == "" {
fmt.Println("Argument --name requied")
ok = false
}
if newUserArgs.login == "" {
fmt.Println("Argument --login requied")
ok = false
}
if newUserArgs.email == "" {
fmt.Println("Argument --email requied")
ok = false
}
if newUserArgs.password == "" {
fmt.Println("Argument --password requied")
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{
2021-08-27 10:50:26 +02:00
Pwd: newUserArgs.password,
2021-08-25 22:12:19 +02:00
User: db.User{
2021-08-27 10:50:26 +02:00
Name: newUserArgs.name,
Username: newUserArgs.login,
Email: newUserArgs.email,
2021-08-27 20:54:48 +02:00
Admin: true,
2021-08-25 22:12:19 +02:00
},
}); err != nil {
panic(err)
}
2021-08-27 10:50:26 +02:00
fmt.Printf("User %s <%s> added!", newUserArgs.login, newUserArgs.email)
2021-08-25 22:12:19 +02:00
},
}