forked from ehazlett/shipyard
/
accounts.go
110 lines (102 loc) · 2.21 KB
/
accounts.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/codegangsta/cli"
"github.com/shipyard/shipyard"
"github.com/shipyard/shipyard/client"
)
var accountsCommand = cli.Command{
Name: "accounts",
Usage: "show accounts",
Action: accountsAction,
}
func accountsAction(c *cli.Context) {
cfg, err := loadConfig()
if err != nil {
logger.Fatal(err)
}
m := client.NewManager(cfg)
accounts, err := m.Accounts()
if err != nil {
logger.Fatalf("error getting accounts: %s", err)
}
if len(accounts) == 0 {
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "Username\tRole")
for _, u := range accounts {
fmt.Fprintf(w, "%s\t%s\n", u.Username, u.Role.Name)
}
w.Flush()
}
var addAccountCommand = cli.Command{
Name: "add-account",
Usage: "add account",
Action: addAccountAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "username, u",
Usage: "account username",
},
cli.StringFlag{
Name: "password, p",
Usage: "account password",
},
cli.StringFlag{
Name: "role, r",
Usage: "account role (admin, user)",
},
},
}
func addAccountAction(c *cli.Context) {
cfg, err := loadConfig()
if err != nil {
logger.Fatal(err)
}
m := client.NewManager(cfg)
user := c.String("username")
pass := c.String("password")
if user == "" || pass == "" {
logger.Fatalf("you must specify a username and password")
}
role, err := m.Role(c.String("role"))
if err != nil {
logger.Fatal(err)
}
account := &shipyard.Account{
Username: user,
Password: pass,
Role: role,
}
if err := m.AddAccount(account); err != nil {
logger.Fatalf("error adding account: %s", err)
}
}
var deleteAccountCommand = cli.Command{
Name: "delete-account",
Usage: "delete account",
Description: "delete-account <username> [<username>]",
Action: deleteAccountAction,
}
func deleteAccountAction(c *cli.Context) {
cfg, err := loadConfig()
if err != nil {
logger.Fatal(err)
}
m := client.NewManager(cfg)
accounts := c.Args()
if len(accounts) == 0 {
return
}
for _, acct := range accounts {
account := &shipyard.Account{
Username: acct,
}
if err := m.DeleteAccount(account); err != nil {
logger.Fatalf("error deleting account: %s", err)
}
}
}