Repository navigation
Expand file tree
/
Copy pathlist-goproxy-modules.go
More file actions
92 lines (75 loc) · 2.68 KB
/
Copy pathlist-goproxy-modules.go
File metadata and controls
92 lines (75 loc) · 2.68 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
package cmd
import (
"bufio"
"context"
"encoding/csv"
"flag"
"log/slog"
"os"
"time"
"github.com/Thiht/go-command"
"github.com/Thiht/go-stats/goproxy"
"github.com/schollz/progressbar/v3"
)
func ListGoProxyModulesHandler(goProxyClient goproxy.Client) command.Handler {
return func(ctx context.Context, flagSet *flag.FlagSet, _ []string) int {
since, err := time.Parse(time.RFC3339, command.Lookup[string](flagSet, "since"))
if err != nil {
slog.Error("failed to parse \"since\"", slog.String("since", command.Lookup[string](flagSet, "since")), slog.Any("error", err))
return 1
}
until, err := time.Parse(time.RFC3339, command.Lookup[string](flagSet, "until"))
if err != nil {
slog.Error("failed to parse \"until\"", slog.String("until", command.Lookup[string](flagSet, "until")), slog.Any("error", err))
return 1
}
outputFile := command.Lookup[string](flagSet, "output-file")
slog.Debug("opening output file", slog.String("file", outputFile))
outputFileHandler, err := os.Create(outputFile)
if err != nil {
slog.Error("failed to open output file", slog.String("file", outputFile), slog.Any("error", err))
return 1
}
defer outputFileHandler.Close()
bufferedWriter := bufio.NewWriter(outputFileHandler)
defer bufferedWriter.Flush()
csvWriter := csv.NewWriter(bufferedWriter)
defer csvWriter.Flush()
csvWriter.Write([]string{"timestamp", "module", "version"})
nbDays := int64(until.Sub(since).Hours() / 24)
progress := progressbar.Default(nbDays, since.Format("2006-01-02"))
chIndex := make(chan goproxy.Index, goproxy.ListIndexMaxLimit)
go func() {
defer close(chIndex)
for index, err := range goProxyClient.IterIndex(ctx, since) {
if err != nil {
slog.Error("failed to list index", slog.Any("error", err))
return
}
chIndex <- index.Index
progress.Describe("Cursor: " + index.Current.Format("2006-01-02"))
if err := progress.Set64(nbDays - int64(until.Sub(index.Current).Hours()/24)); err != nil {
slog.Error("failed to update progress", slog.Any("error", err))
return
}
if index.Current.After(until) {
slog.Debug("reached until date")
break
}
}
}()
modulesSet := map[string]struct{}{}
for i := range chIndex {
key := i.Path + "@" + i.Version
if _, exists := modulesSet[key]; exists {
continue
}
modulesSet[key] = struct{}{}
if err := csvWriter.Write([]string{i.Timestamp.Format(time.RFC3339Nano), i.Path, i.Version}); err != nil {
slog.Error("failed to write module", slog.String("timestamp", i.Timestamp.Format(time.RFC3339Nano)), slog.String("module", i.Path), slog.String("version", i.Version), slog.Any("error", err))
continue
}
}
return 0
}
}