2021-08-25 22:12:19 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/spf13/cobra"
|
2021-08-27 21:14:20 +02:00
|
|
|
"os"
|
2021-08-25 22:12:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-08-28 13:44:41 +02:00
|
|
|
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) {
|
2021-08-27 21:14:20 +02:00
|
|
|
|
|
|
|
ok := true
|
2021-08-28 13:44:41 +02:00
|
|
|
if targetUserArgs.name == "" {
|
|
|
|
fmt.Println("Argument --name required")
|
2021-08-27 21:14:20 +02:00
|
|
|
ok = false
|
|
|
|
}
|
2021-08-28 13:44:41 +02:00
|
|
|
if targetUserArgs.login == "" {
|
|
|
|
fmt.Println("Argument --login required")
|
2021-08-27 21:14:20 +02:00
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
|
2021-08-28 13:44:41 +02:00
|
|
|
if targetUserArgs.email == "" {
|
|
|
|
fmt.Println("Argument --email required")
|
2021-08-27 21:14:20 +02:00
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
|
2021-08-28 13:44:41 +02:00
|
|
|
if targetUserArgs.password == "" {
|
|
|
|
fmt.Println("Argument --password required")
|
2021-08-27 21:14:20 +02:00
|
|
|
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-28 13:44:41 +02:00
|
|
|
Pwd: targetUserArgs.password,
|
2021-08-25 22:12:19 +02:00
|
|
|
User: db.User{
|
2021-08-28 13:44:41 +02:00
|
|
|
Name: targetUserArgs.name,
|
|
|
|
Username: targetUserArgs.login,
|
|
|
|
Email: targetUserArgs.email,
|
|
|
|
Admin: targetUserArgs.admin,
|
2021-08-25 22:12:19 +02:00
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-08-28 13:44:41 +02:00
|
|
|
fmt.Printf("User %s <%s> added!\n", targetUserArgs.login, targetUserArgs.email)
|
2021-08-25 22:12:19 +02:00
|
|
|
},
|
|
|
|
}
|