github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/access.go (about) 1 package main 2 3 import ( 4 "log" 5 "os" 6 "sort" 7 "text/tabwriter" 8 9 "github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go" 10 ) 11 12 var cmdAccess = &Command{ 13 Run: runAccess, 14 Usage: "access", 15 NeedsApp: true, 16 Category: "access", 17 Short: "list access permissions" + extra, 18 Long: ` 19 List access permissions for an app. The owner is shown first, and 20 collaborators are then listed alphabetically. 21 22 Examples: 23 24 $ hk access 25 b@heroku.com owner 26 max@heroku.com collaborator 27 `, 28 } 29 30 func runAccess(cmd *Command, args []string) { 31 w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0) 32 defer w.Flush() 33 34 if len(args) != 0 { 35 cmd.PrintUsage() 36 os.Exit(2) 37 } 38 39 // Org collaborators works for all apps and gives us exactly the data we need. 40 orgCollaborators, err := client.OrganizationAppCollaboratorList(mustApp(), nil) 41 must(err) 42 43 sort.Sort(accessByRoleAndEmail(orgCollaborators)) 44 for _, oc := range orgCollaborators { 45 listRec(w, 46 oc.User.Email, 47 oc.Role, 48 prettyTime{oc.UpdatedAt}, 49 ) 50 } 51 } 52 53 type accessByRoleAndEmail []heroku.OrganizationAppCollaborator 54 55 func (a accessByRoleAndEmail) Len() int { return len(a) } 56 func (a accessByRoleAndEmail) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 57 func (a accessByRoleAndEmail) Less(i, j int) bool { 58 return a[i].Role == "owner" || a[i].User.Email < a[j].User.Email 59 } 60 61 var cmdAccessAdd = &Command{ 62 Run: runAccessAdd, 63 Usage: "access-add [-s] <email>", 64 NeedsApp: true, 65 Category: "access", 66 Short: "give a user access to an app" + extra, 67 Long: ` 68 Give another Heroku user access to an app. 69 70 Options: 71 72 -s add user silently with no email notification 73 74 Examples: 75 76 $ hk access-add user@me.com 77 Granted user@me.com access to myapp. 78 79 $ hk access-add -s anotheruser@me.com 80 Granted anotheruser@me.com access to myapp. 81 `, 82 } 83 84 var flagSilent bool 85 86 func init() { 87 cmdAccessAdd.Flag.BoolVarP(&flagSilent, "silent", "s", false, "add user silently with no email notification") 88 } 89 90 func runAccessAdd(cmd *Command, args []string) { 91 appname := mustApp() 92 if len(args) != 1 { 93 cmd.PrintUsage() 94 os.Exit(2) 95 } 96 user := args[0] 97 98 var err error 99 if isAppInOrg(mustGetOrgApp(appname)) { 100 opts := heroku.OrganizationAppCollaboratorCreateOpts{Silent: &flagSilent} 101 _, err = client.OrganizationAppCollaboratorCreate(appname, user, &opts) 102 } else { 103 opts := heroku.CollaboratorCreateOpts{Silent: &flagSilent} 104 _, err = client.CollaboratorCreate(appname, user, &opts) 105 } 106 must(err) 107 log.Printf("Granted %s access to %s.", user, appname) 108 } 109 110 var cmdAccessRemove = &Command{ 111 Run: runAccessRemove, 112 Usage: "access-remove <email>", 113 NeedsApp: true, 114 Category: "access", 115 Short: "remove a user's access to an app" + extra, 116 Long: ` 117 Remove another Heroku user's access to an app. 118 119 Examples: 120 121 $ hk access-remove user@me.com 122 Removed user@me.com from access to myapp. 123 `, 124 } 125 126 func runAccessRemove(cmd *Command, args []string) { 127 appname := mustApp() 128 if len(args) != 1 { 129 cmd.PrintUsage() 130 os.Exit(2) 131 } 132 user := args[0] 133 134 if isAppInOrg(mustGetOrgApp(appname)) { 135 must(client.OrganizationAppCollaboratorDelete(appname, user)) 136 } else { 137 must(client.CollaboratorDelete(appname, user)) 138 } 139 log.Printf("Removed %s from access to %s.", user, appname) 140 }