-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
185 lines (169 loc) · 4.45 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
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
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/manifoldco/promptui"
"github.com/mfridman/bestgo/gen/go/api"
"github.com/mfridman/bestgo/gen/go/datapb"
"github.com/mfridman/cli"
"github.com/ryanuber/columnize"
)
const (
apiURL = "https://api.bestofgo.dev"
)
var (
re = regexp.MustCompile(`(?m)\[(.*?)\s*\]`)
)
func convertToTimeInterval(interval string) datapb.TimeIntervalType {
switch interval {
case "day":
return datapb.TimeIntervalType_TIME_INTERVAL_TYPE_DAY
case "month":
return datapb.TimeIntervalType_TIME_INTERVAL_TYPE_MONTH
case "quarter":
return datapb.TimeIntervalType_TIME_INTERVAL_TYPE_QUARTER
default:
return datapb.TimeIntervalType_TIME_INTERVAL_TYPE_YEAR
}
}
func main() {
root := &cli.Command{
Name: "bestgo",
Usage: "bestgo [flags]",
ShortHelp: "bestgo is a CLI tool to get the star history of a Go repository",
Flags: cli.FlagsFunc(func(f *flag.FlagSet) {
f.String("repo", "", "Full repository name. e.g., bufbuild/buf")
f.String("i", "year", "The grouping interval [day,month,quarter,year]")
}),
Exec: func(ctx context.Context, s *cli.State) error {
var (
repoName = cli.GetFlag[string](s, "repo")
interval = cli.GetFlag[string](s, "i")
client = api.NewClient(apiURL)
)
metrics, err := fetchRepoMetricsWithRetryPrompt(
client,
strings.TrimSpace(repoName),
convertToTimeInterval(interval),
)
if err != nil {
return fmt.Errorf("failed to get repo metrics from api: %w", err)
}
var points []point
for _, m := range metrics.GetData() {
points = append(points, point{date: m.X, count: m.Y})
}
fmt.Fprintln(s.Stdout, plot(points))
fmt.Fprintf(s.Stdout, "Repository: %v has %d ⭐️ stars total\n",
lipgloss.NewStyle().Foreground(lipgloss.Color("68")).Render(metrics.GetRepoName()),
metrics.GetTotalStars(),
)
return nil
},
}
if err := cli.Parse(root, os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
fmt.Fprintf(os.Stdout, "%s\n", cli.DefaultUsage(root))
return
}
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := cli.Run(context.Background(), root, nil); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
type point struct {
date string
count int32
}
// could also be done directly in sql.
// https://tapoueh.org/blog/2014/02/postgresql-aggregates-and-histograms/?utm_source=pocket_mylist
func plot(points []point) string {
var max int32
for _, p := range points {
if p.count > max {
max = p.count
}
}
var b strings.Builder
for i := 0; i < len(points); i++ {
var l int32
if max > 0 {
l = (points[i].count*30 + max/2) / max
}
b.WriteString(fmt.Sprintf("%s [%v]%s|%v\n",
points[i].date,
points[i].count,
strings.Repeat(" ", addPadding(points[i].count, max)),
strings.Repeat(`■`, int(l))))
}
return b.String()
}
func addPadding(count, max int32) int {
m := len(strconv.Itoa(int(max)))
c := len(strconv.Itoa(int(count)))
if c >= m {
return 0
}
return m - c
}
func fetchRepoMetricsWithRetryPrompt(
client *api.Client,
repoName string,
timeInterval datapb.TimeIntervalType,
) (*api.GetRepoMetricsResponse, error) {
resp, err := client.APIService.GetRepoMetrics(context.Background(), &api.GetRepoMetricsRequest{
RepoName: repoName,
TimeInterval: timeInterval,
})
if err != nil {
return nil, err
}
if len(resp.GetSuggestions()) > 0 {
name := repoName
if name == "" {
name = "unspecified"
}
fmt.Printf("Could not match repository: %s\n\n", name)
prompt := promptui.Select{
Label: "Was that a typo? Did you mean one of these",
}
var items []string
for i, repo := range resp.GetSuggestions() {
i++
items = append(items, fmt.Sprintf("%d.| [|%s|] with %d|stars",
i,
repo.GetRepoFullName(),
repo.GetRepoStargazers()),
)
}
prompt.Items = strings.Split(columnize.SimpleFormat(items), "\n")
_, result, err := prompt.Run()
if err != nil {
return nil, fmt.Errorf("failed on ui prompt: %w", err)
}
out := re.FindStringSubmatch(result)
if len(out) == 2 {
repoName = strings.TrimSpace(out[1])
resp, err := client.APIService.GetRepoMetrics(context.Background(), &api.GetRepoMetricsRequest{
RepoName: repoName,
TimeInterval: timeInterval,
})
if err != nil {
return nil, err
}
return resp, nil
}
return nil, fmt.Errorf("failed to match repository: %v", repoName)
}
return resp, nil
}