/
license.go
72 lines (59 loc) · 1.52 KB
/
license.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
package api
import graphql "github.com/cli/shurcooL-graphql"
type Licenses struct {
client *Client
}
type License interface {
ExpiresAt() string
IssuedAt() string
}
type OnPremLicense struct {
ID string
ExpiresAtVal string
IssuedAtVal string
IssuedTo string
NumberOfSeats int
}
func (l OnPremLicense) IssuedAt() string {
return l.IssuedAtVal
}
func (l OnPremLicense) ExpiresAt() string {
return l.ExpiresAtVal
}
func (c *Client) Licenses() *Licenses { return &Licenses{client: c} }
func (l *Licenses) Install(license string) error {
var mutation struct {
UpdateLicenseKey struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"updateLicenseKey(license: $license)"`
}
variables := map[string]interface{}{
"license": graphql.String(license),
}
return l.client.Mutate(&mutation, variables)
}
func (l *Licenses) Get() (License, error) {
var query struct {
InstalledLicense struct {
ExpiresAt string
IssuedAt string
OnPrem struct {
ID string `graphql:"uid"`
Owner string
MaxUsers int
} `graphql:"... on OnPremLicense"`
}
}
err := l.client.Query(&query, nil)
if err != nil {
return nil, err
}
return OnPremLicense{
ID: query.InstalledLicense.OnPrem.ID,
ExpiresAtVal: query.InstalledLicense.ExpiresAt,
IssuedAtVal: query.InstalledLicense.IssuedAt,
IssuedTo: query.InstalledLicense.OnPrem.Owner,
NumberOfSeats: query.InstalledLicense.OnPrem.MaxUsers,
}, nil
}