forked from vmware-archive/atc
/
client.go
62 lines (49 loc) · 1.21 KB
/
client.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
package gitlab
import (
"fmt"
"net/http"
"net/url"
gogitlab "github.com/xanzy/go-gitlab"
)
//go:generate counterfeiter . Client
type Client interface {
Groups(*http.Client) ([]string, error)
}
type client struct {
baseURL string
}
func NewClient(baseURL string) Client {
return &client{baseURL: baseURL}
}
func (c *client) gitlabClient(httpClient *http.Client) (*gogitlab.Client, error) {
client := gogitlab.NewClient(httpClient, "token")
if c.baseURL != "" {
var err error
gitlabBaseURL, err := url.Parse(c.baseURL)
if err != nil {
return nil, fmt.Errorf("invalid gitlab auth API URL '%s'", c.baseURL)
}
client.SetBaseURL(gitlabBaseURL.String())
}
return client, nil
}
func (c *client) Groups(httpClient *http.Client) ([]string, error) {
client, err := c.gitlabClient(httpClient)
if err != nil {
return nil, err
}
nextPage := 1
usersGroups := []string{}
for nextPage != 0 {
opts := &gogitlab.ListGroupsOptions{gogitlab.ListOptions{Page: nextPage}, nil, nil}
groups, resp, err := client.Groups.ListGroups(opts)
if err != nil {
return nil, err
}
for _, g := range groups {
usersGroups = append(usersGroups, g.Name)
}
nextPage = resp.NextPage
}
return usersGroups, nil
}