/
user.go
54 lines (44 loc) · 1.15 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package gen
import (
"fmt"
"github.com/Aptomi/aptomi/pkg/config"
"github.com/Aptomi/aptomi/pkg/lang"
"github.com/Aptomi/aptomi/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
func newUserCommand(cfg *config.Client) *cobra.Command {
var username, password string
cmd := &cobra.Command{
Use: "user",
Short: "gen user",
Long: "gen user long",
Run: func(cmd *cobra.Command, args []string) {
if len(username) <= 0 {
log.Fatalf("username should be specified")
}
if len(password) <= 0 {
log.Fatalf("password should be specified")
}
user := &lang.User{
Name: username,
PasswordHash: util.HashAndSalt(password),
}
data, err := yaml.Marshal(user)
if err != nil {
panic(fmt.Sprintf("error while marshalling user: %s", err))
}
fmt.Println(string(data))
},
}
cmd.Flags().StringVarP(&username, "username", "u", "", "Username")
if err := cmd.MarkFlagRequired("username"); err != nil {
panic(err)
}
cmd.Flags().StringVarP(&password, "password", "p", "", "Password")
if err := cmd.MarkFlagRequired("password"); err != nil {
panic(err)
}
return cmd
}