@@ -17,6 +17,7 @@ import (
1717
1818 "github.com/cuotos/outstanding-prs/filter"
1919 "github.com/google/go-github/v33/github"
20+ "github.com/hashicorp/logutils"
2021 "github.com/kelseyhightower/envconfig"
2122 "github.com/zalando/go-keyring"
2223 "golang.org/x/oauth2"
@@ -59,28 +60,47 @@ type PullRequest struct {
5960 Draft bool
6061}
6162
62- func init () {
63- log .SetOutput (os .Stdout )
64- log .SetFlags (log .LstdFlags | log .Lshortfile )
63+ type flags struct {
64+ version * bool
65+ jsonOutput * bool
66+ all * bool
67+ incApproved * bool
68+ searchWholeTeam * bool
69+ incDrafts * bool
70+ reauth * bool
71+ logLevel * string
6572}
6673
6774func main () {
68- if err := run (); err != nil {
69- log .Fatal (err )
75+ flags := flags {}
76+ flags .version = flag .Bool ("v" , false , "prints version" )
77+ flags .jsonOutput = flag .Bool ("json" , false , "print output in JSON format" )
78+ flags .all = flag .Bool ("all" , false , "include PRs ready to merge. DEPRECATED: use -approved" )
79+ flags .incApproved = flag .Bool ("approved" , false , "include PRs ready to merge" )
80+ flags .searchWholeTeam = flag .Bool ("team" , false , "should look up all members of the team. defaults to just calling user" )
81+ flags .incDrafts = flag .Bool ("drafts" , false , "include PRs that are in draft" )
82+ flags .reauth = flag .Bool ("reauth" , false , "clears tokens and authorise against Github.com" )
83+ flags .logLevel = flag .String ("log-level" , "INFO" , "set log level DEBUG, INFO, WARN, or ERROR" )
84+
85+ flag .Parse ()
86+
87+ log .SetOutput (os .Stdout )
88+ log .SetFlags (log .LstdFlags | log .Lshortfile )
89+ logFilter := & logutils.LevelFilter {
90+ Levels : []logutils.LogLevel {"DEBUG" , "INFO" , "WARN" , "ERROR" },
91+ MinLevel : logutils .LogLevel (strings .ToUpper (* flags .logLevel )),
92+ Writer : os .Stderr ,
93+ }
94+ log .SetOutput (logFilter )
95+
96+ if err := run (flags ); err != nil {
97+ log .Printf ("[ERROR] %s" , err )
7098 }
7199}
72100
73- func run () error {
74- v := flag .Bool ("v" , false , "prints version" )
75- jsonOutput := flag .Bool ("json" , false , "print output in JSON format" )
76- all := flag .Bool ("all" , false , "include PRs ready to merge. DEPRECATED: use -approved" )
77- incApproved := flag .Bool ("approved" , false , "include PRs ready to merge" )
78- searchWholeTeam := flag .Bool ("team" , false , "should look up all members of the team. defaults to just calling user" )
79- incDrafts := flag .Bool ("drafts" , false , "include PRs that are in draft" )
80- reauth := flag .Bool ("reauth" , false , "clears tokens and authorise against Github.com" )
101+ func run (flags flags ) error {
81102
82- flag .Parse ()
83- if * v {
103+ if * flags .version {
84104 fmt .Printf ("%s-%s" , version , commit )
85105 os .Exit (0 )
86106 }
@@ -91,15 +111,15 @@ func run() error {
91111 return err
92112 }
93113
94- accessToken , err := getGithubOAuthAccessToken (* reauth )
114+ accessToken , err := getGithubOAuthAccessToken (* flags . reauth )
95115 if err != nil {
96116 return err
97117 }
98118
99119 client := getGithubClient (accessToken )
100120
101121 members := []* github.User {}
102- if * searchWholeTeam {
122+ if * flags . searchWholeTeam {
103123 members , err = getOrgTeamMembers (client , conf .GithubOrg , conf .GithubTeam )
104124 if err != nil {
105125 return err
@@ -112,22 +132,22 @@ func run() error {
112132 members = append (members , user )
113133 }
114134
115- queryString , err := generateQueryString (conf .GithubOrg , members , filter .WithIncludeApproved (* all || * incApproved ), filter .WithIncludeDraft (* incDrafts ))
135+ queryString , err := generateQueryString (conf .GithubOrg , members , filter .WithIncludeApproved (* flags . all || * flags . incApproved ), filter .WithIncludeDraft (* flags . incDrafts ))
116136 if err != nil {
117137 return err
118138 }
119139
120- log .Printf (`Looking for PRs with the following query: "%s"` , queryString )
140+ log .Printf (`[DEBUG] Looking for PRs with the following query: "%s"` , queryString )
121141
122142 prs , err := getPullRequests (client , queryString )
123143 if err != nil {
124144 return err
125145 }
126146
127- if * jsonOutput {
147+ if * flags . jsonOutput {
128148 printOutputJSON (prs )
129149 } else {
130- printOutput (prs , conf .GithubOrg , conf .GithubTeam , * incDrafts )
150+ printOutput (prs , conf .GithubOrg , conf .GithubTeam , * flags . incDrafts )
131151 }
132152
133153 return nil
@@ -319,7 +339,7 @@ func getGithubOAuthAccessToken(reauth bool) (string, error) {
319339 if err == keyring .ErrNotFound || reauth {
320340 // Token file does not exists
321341
322- log .Println ("token not found, begin auth flow" )
342+ log .Println ("[WARN] token not found, begin auth flow" )
323343
324344 flow := & oauth.Flow {
325345 Host : oauth .GitHubHost ("https://github.com" ),
@@ -329,6 +349,9 @@ func getGithubOAuthAccessToken(reauth bool) (string, error) {
329349
330350 fmt .Println (flow .Host .DeviceCodeURL )
331351 ghToken , err := flow .DeviceFlow ()
352+ if err != nil {
353+ return token , err
354+ }
332355 token = ghToken .Token
333356
334357 if errors .Is (err , device .ErrUnsupported ) {
0 commit comments