forked from ardanlabs/gotraining
/
create.go
80 lines (63 loc) · 1.75 KB
/
create.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cmduser
import (
"errors"
"github.com/spf13/cobra"
)
var createLong = `Use create to add a new user to the system. The user email
must be unique for every user.
Example:
./shelf user create -n "Bill Kennedy" -e "bill@ardanlabs.com" -p "yefc*7fdf92"
`
// create contains the state for this command.
var create struct {
name string
pass string
email string
}
// addCreate handles the creation of users.
func addCreate() {
cmd := &cobra.Command{
Use: "create",
Short: "Add a new user to the system.",
Long: createLong,
Run: runCreate,
}
cmd.Flags().StringVarP(&create.name, "name", "n", "", "Full name for the user.")
cmd.Flags().StringVarP(&create.email, "email", "e", "", "Email for the user.")
cmd.Flags().StringVarP(&create.pass, "pass", "p", "", "Password for the user.")
userCmd.AddCommand(cmd)
}
// runCreate is the code that implements the create command.
func runCreate(cmd *cobra.Command, args []string) {
cmd.Printf("Creating User : Name[%s] Email[%s] Pass[%s]\n", create.name, create.email, create.pass)
if create.name == "" && create.email == "" && create.pass == "" {
cmd.Help()
return
}
u := User{
Status: 1,
Name: "Bill",
Email: "bill@ardanlabs.com",
Password: "my passoword",
}
if err := createUser(&u); err != nil {
cmd.Println("Creating User : ", err)
return
}
cmd.Println("Creating User : Created")
}
//==============================================================================
// User represents a sample user model.
type User struct {
Status int
Name string
Email string
Password string
}
// createUser is a sample function to simulate a user creation.
func createUser(u *User) error {
if u.Status == 0 {
return errors.New("Invalid user value")
}
return nil
}