-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (52 loc) · 1.96 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
)
func main() {
// Flags for the command
repo := flag.String("r", "", "The repo for which to check roles. If blank, the current repo is used.")
org := flag.String("o", "", "The org for which to check roles. If blank, defaults to repo check. If present, repo flag will be ignored.")
team := flag.String("t", "", "The team for which to check roles. Only valid in combination with org flag.")
user := flag.String("u", "", "The user login for which to check roles. If blank, the current user is used.")
host := flag.String("hostname", "", "The host for which to check roles. If blank, defaults to the gh config. Note that you will need to be be authenticated for the host through the gh cli.")
friendly := flag.Bool("f", false, "Prints a friendly message. Otherwise, prints a machine-readable role name.")
// Overrides default help message to inform about args
defaultUsage := flag.Usage
flag.Usage = func() {
defaultUsage()
fmt.Println(" List roles as space-separated arguments after any other flags to check if the current user has one of those roles.")
fmt.Println(" Will exit with a non-zero status if the user does not have one of the specified roles.")
}
flag.Parse()
var roles = flag.Args()
isViewer := *user == ""
if isViewer {
user = _viewerLogin()
}
if *org != "" {
err := Evaluate(*user, NestedEntityName(*org, *team), OrgRole(*org, *team, *user, *host), roles, *friendly)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
// Check repo roles
repository := _getRepo(*repo, *host)
var userRole string
if isViewer {
// Much faster to use viewerPermission check
userRole = RepoRoleForViewer(repository)
} else {
userRole = RepoRoleForUser(repository, *user, *host)
}
err := Evaluate(*user, _repoPath(repository), userRole, roles, *friendly)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
// For more examples of using go-gh, see:
// https://github.com/cli/go-gh/blob/trunk/example_gh_test.go