-
Notifications
You must be signed in to change notification settings - Fork 787
/
get_limits.go
164 lines (132 loc) · 3.46 KB
/
get_limits.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
package cmd
import (
"io"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"encoding/json"
"fmt"
"net/http"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"strconv"
"time"
"github.com/jenkins-x/jx/pkg/log"
)
type RateLimits struct {
Resources RateResources `json:"resources"`
}
type RateResources struct {
Core Rate `json:"core"`
Search Rate `json:"search"`
GraphQL Rate `json:"graphql"`
}
type Rate struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
}
// GetAddonOptions the command line options
type GetLimitsOptions struct {
GetOptions
}
var (
get_limits_long = templates.LongDesc(`
Display the github limits for users
`)
get_limits_example = templates.Examples(`
# List all git users with limits
jx get limits
`)
)
// NewCmdGetLimits creates the command
func NewCmdGetLimits(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetLimitsOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "limits [flags]",
Short: "Displays the git user limits",
Long: get_limits_long,
Example: get_limits_example,
Aliases: []string{"limit"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
return cmd
}
// Run implements this command
func (o *GetLimitsOptions) Run() error {
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
config := authConfigSvc.Config()
table := o.createTable()
table.AddRow("Name", "URL", "Username", "Limit", "Remaining", "Reset")
for _, s := range config.Servers {
kind := s.Kind
if kind == "" {
kind = "github"
}
if kind == "github" {
for _, u := range s.Users {
r, err := o.GetLimits(s.URL, u.Username, u.ApiToken)
if err != nil {
return err
}
resetLabel := ""
if 0 != r.Resources.Core.Reset {
secondsUntilReset := int64(r.Resources.Core.Reset) - time.Now().Unix()
d := time.Duration(1000 * 1000 * 1000 * secondsUntilReset)
resetLabel = d.String()
}
table.AddRow(s.Name, s.URL, u.Username, strconv.Itoa(r.Resources.Core.Limit), strconv.Itoa(r.Resources.Core.Remaining), resetLabel)
}
}
}
table.Render()
return nil
}
func (o *GetLimitsOptions) GetLimits(server string, username string, apitoken string) (RateLimits, error) {
url := fmt.Sprintf("https://%s:%s@api.github.com/rate_limit", username, apitoken)
// Build the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Errorf("NewRequest: %s", err)
return RateLimits{}, err
}
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
log.Errorf("Do: %s", err)
return RateLimits{}, err
}
// Callers should close resp.Body
// when done reading from it
// Defer the closing of the body
defer resp.Body.Close()
// Fill the record with the data from the JSON
var limits RateLimits
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&limits); err != nil {
log.Errorf("Decode: %s", err)
}
return limits, nil
}