Skip to content

Commit 9d323d2

Browse files
committed
use simple logging
1 parent 3e38248 commit 9d323d2

4 files changed

Lines changed: 49 additions & 22 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist/
2+
main_sandbox_test.go

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/godbus/dbus/v5 v5.1.0 // indirect
2121
github.com/golang/protobuf v1.5.3 // indirect
2222
github.com/google/go-querystring v1.1.0 // indirect
23+
github.com/hashicorp/logutils v1.0.0 // indirect
2324
github.com/pmezard/go-difflib v1.0.0 // indirect
2425
golang.org/x/crypto v0.9.0 // indirect
2526
golang.org/x/net v0.10.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ github.com/google/go-github/v33 v33.0.0/go.mod h1:GMdDnVZY/2TsWgp/lkYnpSAh6TrzhA
2626
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
2727
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
2828
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
29+
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
30+
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
2931
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
3032
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
3133
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

main.go

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6774
func 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

Comments
 (0)