-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_github_repos.go
More file actions
84 lines (73 loc) 路 2.22 KB
/
Copy pathcollect_github_repos.go
File metadata and controls
84 lines (73 loc) 路 2.22 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
package cmd
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/Snider/Borg/pkg/github"
borghttp "github.com/Snider/Borg/pkg/http"
"github.com/spf13/cobra"
)
var (
// GithubClient is the github client used by the command. It can be replaced for testing.
GithubClient = github.NewGithubClient(nil)
)
var collectGithubReposCmd = &cobra.Command{
Use: "repos [user-or-org]",
Short: "Collects all public repositories for a user or organization",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rateLimit, _ := cmd.Flags().GetString("rate-limit")
burst, _ := cmd.Flags().GetInt("burst")
rateConfig, _ := cmd.Flags().GetString("rate-config")
config := &borghttp.Config{
Defaults: borghttp.Rate{
RequestsPerSecond: 1, // GitHub API has strict limits
Burst: 1,
},
Domains: make(map[string]borghttp.Rate),
}
if rateConfig != "" {
var err error
config, err = borghttp.ParseConfig(rateConfig)
if err != nil {
return fmt.Errorf("error parsing rate config: %w", err)
}
}
if rateLimit != "" {
parts := strings.Split(rateLimit, "/")
if len(parts) != 2 || (parts[1] != "s" && parts[1] != "m") {
return fmt.Errorf("invalid rate limit format: %s (e.g., 2/s or 120/m)", rateLimit)
}
rate, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return fmt.Errorf("invalid rate: %w", err)
}
if parts[1] == "m" {
rate = rate / 60
}
config.Defaults.RequestsPerSecond = rate
}
if burst > 0 {
config.Defaults.Burst = burst
}
client := &http.Client{
Transport: borghttp.NewRateLimitingRoundTripper(config, http.DefaultTransport),
}
ghClient := github.NewGithubClient(client)
repos, err := ghClient.GetPublicRepos(cmd.Context(), args[0])
if err != nil {
return err
}
for _, repo := range repos {
fmt.Fprintln(cmd.OutOrStdout(), repo)
}
return nil
},
}
func init() {
collectGithubCmd.AddCommand(collectGithubReposCmd)
collectGithubReposCmd.Flags().String("rate-limit", "", "Requests per second (e.g., 2/s) or minute (e.g., 120/m)")
collectGithubReposCmd.Flags().Int("burst", 0, "Burst allowance")
collectGithubReposCmd.Flags().String("rate-config", "", "Path to a rate limit configuration file")
}