This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
access.go
140 lines (114 loc) · 3.17 KB
/
access.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"log"
"os"
"sort"
"text/tabwriter"
"github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go"
)
var cmdAccess = &Command{
Run: runAccess,
Usage: "access",
NeedsApp: true,
Category: "access",
Short: "list access permissions" + extra,
Long: `
List access permissions for an app. The owner is shown first, and
collaborators are then listed alphabetically.
Examples:
$ hk access
b@heroku.com owner
max@heroku.com collaborator
`,
}
func runAccess(cmd *Command, args []string) {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
// Org collaborators works for all apps and gives us exactly the data we need.
orgCollaborators, err := client.OrganizationAppCollaboratorList(mustApp(), nil)
must(err)
sort.Sort(accessByRoleAndEmail(orgCollaborators))
for _, oc := range orgCollaborators {
listRec(w,
oc.User.Email,
oc.Role,
prettyTime{oc.UpdatedAt},
)
}
}
type accessByRoleAndEmail []heroku.OrganizationAppCollaborator
func (a accessByRoleAndEmail) Len() int { return len(a) }
func (a accessByRoleAndEmail) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a accessByRoleAndEmail) Less(i, j int) bool {
return a[i].Role == "owner" || a[i].User.Email < a[j].User.Email
}
var cmdAccessAdd = &Command{
Run: runAccessAdd,
Usage: "access-add [-s] <email>",
NeedsApp: true,
Category: "access",
Short: "give a user access to an app" + extra,
Long: `
Give another Heroku user access to an app.
Options:
-s add user silently with no email notification
Examples:
$ hk access-add user@me.com
Granted user@me.com access to myapp.
$ hk access-add -s anotheruser@me.com
Granted anotheruser@me.com access to myapp.
`,
}
var flagSilent bool
func init() {
cmdAccessAdd.Flag.BoolVarP(&flagSilent, "silent", "s", false, "add user silently with no email notification")
}
func runAccessAdd(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
user := args[0]
var err error
if isAppInOrg(mustGetOrgApp(appname)) {
opts := heroku.OrganizationAppCollaboratorCreateOpts{Silent: &flagSilent}
_, err = client.OrganizationAppCollaboratorCreate(appname, user, &opts)
} else {
opts := heroku.CollaboratorCreateOpts{Silent: &flagSilent}
_, err = client.CollaboratorCreate(appname, user, &opts)
}
must(err)
log.Printf("Granted %s access to %s.", user, appname)
}
var cmdAccessRemove = &Command{
Run: runAccessRemove,
Usage: "access-remove <email>",
NeedsApp: true,
Category: "access",
Short: "remove a user's access to an app" + extra,
Long: `
Remove another Heroku user's access to an app.
Examples:
$ hk access-remove user@me.com
Removed user@me.com from access to myapp.
`,
}
func runAccessRemove(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
user := args[0]
if isAppInOrg(mustGetOrgApp(appname)) {
must(client.OrganizationAppCollaboratorDelete(appname, user))
} else {
must(client.CollaboratorDelete(appname, user))
}
log.Printf("Removed %s from access to %s.", user, appname)
}