Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
192 lines (171 loc) · 5.02 KB
/
Copy pathmain.go
File metadata and controls
192 lines (171 loc) · 5.02 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// prwatch lists your open GitHub pull requests and GitLab merge requests in one table,
// highlighting new comments/reactions since the last run and PRs that are behind upstream.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"github.com/jedib0t/go-pretty/v6/text"
"prwatch/internal/config"
"prwatch/internal/github"
"prwatch/internal/gitlab"
"prwatch/internal/model"
"prwatch/internal/render"
"prwatch/internal/state"
)
const usage = `prwatch – your open PRs/MRs on GitHub and GitLab
Usage:
prwatch list [--all | --github | --gitlab] [--keep] [--urls] [--width N] [--json]
prwatch reset forget what was seen (next run becomes the new baseline)
prwatch state-path print where the seen-state is stored
Environment:
PRWATCH_GITHUB_TOKEN GitHub personal access token
PRWATCH_GITHUB_URL GitHub URL (default: https://github.com; GitHub Enterprise Server works too)
PRWATCH_GITLAB_TOKEN GitLab personal access token (scope: read_api)
PRWATCH_GITLAB_URL GitLab URL (default: https://gitlab.com)
NO_COLOR disable colors
More instances: add the same variables with a common suffix, e.g.
PRWATCH_GITLAB_TOKEN_ARCH + PRWATCH_GITLAB_URL_ARCH=https://gitlab.archlinux.org
`
func main() {
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(2)
}
var err error
switch os.Args[1] {
case "list", "ls":
err = runList(os.Args[2:])
case "reset":
err = state.Reset()
if err == nil {
fmt.Println("State cleared.")
}
case "state-path":
fmt.Println(state.Path())
case "help", "-h", "--help":
fmt.Print(usage)
default:
fmt.Fprintf(os.Stderr, "unknown command %q\n\n%s", os.Args[1], usage)
os.Exit(2)
}
if err != nil {
fmt.Fprintln(os.Stderr, text.FgRed.Sprint("error:"), err)
os.Exit(1)
}
}
type source struct {
inst config.Instance
fetch func(context.Context) ([]*model.PR, error)
}
type result struct {
prs []*model.PR
err error
}
func runList(args []string) error {
fs := flag.NewFlagSet("list", flag.ExitOnError)
all := fs.Bool("all", false, "show GitHub and GitLab (default)")
onlyGH := fs.Bool("github", false, "only GitHub")
onlyGL := fs.Bool("gitlab", false, "only GitLab")
keep := fs.Bool("keep", false, "don't mark activity as seen")
urls := fs.Bool("urls", false, "add a column with full URLs")
width := fs.Int("width", 48, "max title width")
asJSON := fs.Bool("json", false, "print JSON instead of a table (includes branches and fetch errors)")
fs.Parse(args)
plain := *asJSON || os.Getenv("NO_COLOR") != "" || !isTerminal()
if plain {
text.DisableColors()
}
instances, warnings, err := config.FromEnv()
if err != nil {
return err
}
for _, w := range warnings {
fmt.Fprintln(os.Stderr, text.Faint.Sprint(w))
}
explicit := (*onlyGH || *onlyGL) && !*all
wanted := func(p model.Platform) bool {
return !explicit || (p == model.GitHub && *onlyGH) || (p == model.GitLab && *onlyGL)
}
var active []source
for _, in := range instances {
if !wanted(in.Platform) {
continue
}
s := source{inst: in}
if in.Platform == model.GitHub {
s.fetch = github.New(in.URL, in.Token).OpenPRs
} else {
s.fetch = gitlab.New(in.URL, in.Token).OpenMRs
}
active = append(active, s)
}
if len(active) == 0 {
for _, p := range []model.Platform{model.GitHub, model.GitLab} {
if explicit && wanted(p) {
return fmt.Errorf("%s requested but no PRWATCH_%s_TOKEN is set", model.PlatformName(p), strings.ToUpper(string(p)))
}
}
return fmt.Errorf("no tokens configured – set PRWATCH_GITHUB_TOKEN and/or PRWATCH_GITLAB_TOKEN (see .env.example)")
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
results := make([]result, len(active))
var wg sync.WaitGroup
for i, s := range active {
wg.Go(func() {
prs, err := s.fetch(ctx)
results[i] = result{prs, err}
})
}
if isTerminal() {
fmt.Fprint(os.Stderr, text.Faint.Sprint("fetching…"))
}
wg.Wait()
if isTerminal() {
fmt.Fprint(os.Stderr, "\r\033[K")
}
st, err := state.Load()
if err != nil {
return fmt.Errorf("loading state: %w", err)
}
var prs []*model.PR
var fetchErrs []render.JSONError
for i, r := range results {
if r.err != nil {
fetchErrs = append(fetchErrs, render.JSONError{Instance: active[i].inst.Name(), Error: r.err.Error()})
fmt.Fprintf(os.Stderr, "%s %s: %v\n", text.FgRed.Sprint("✘"), active[i].inst.Name(), r.err)
continue
}
st.Annotate(r.prs)
prs = append(prs, r.prs...)
if !*keep {
st.Record(active[i].inst.Platform, active[i].inst.Host(), r.prs)
}
}
if len(fetchErrs) == len(active) {
return fmt.Errorf("all sources failed")
}
render.Sort(prs)
if *asJSON {
if err := render.JSON(os.Stdout, prs, fetchErrs); err != nil {
return err
}
} else {
render.Table(os.Stdout, prs, render.Options{ShowURLs: *urls, TitleWidth: *width, Plain: plain})
}
if !*keep {
if err := st.Save(); err != nil {
return fmt.Errorf("saving state: %w", err)
}
}
return nil
}
func isTerminal() bool {
fi, err := os.Stdout.Stat()
return err == nil && fi.Mode()&os.ModeCharDevice != 0
}