-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_domain.go
66 lines (54 loc) · 1.47 KB
/
cmd_domain.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
package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/urfave/cli"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
func cmdDomainList(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory Client %v", err)
}
r, err := srv.Domains.List(myAccoutsCustomerID).Do()
if err != nil {
return fmt.Errorf("unable to retrieve domains: %v", err)
}
if optionJSON(c, r.Domains) {
return nil
}
for _, each := range r.Domains {
fmt.Println(each.DomainName)
}
return nil
}
const primaryDomainEnvironmentKey = "GWS_PRIMARY_DOMAIN"
var cachedPrimaryDomain string
func primaryDomain(c *cli.Context) (string, error) {
if len(cachedPrimaryDomain) > 0 {
return cachedPrimaryDomain, nil
}
if p := os.Getenv(primaryDomainEnvironmentKey); len(p) > 0 {
return p, nil
}
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return "", fmt.Errorf("unable to retrieve directory Client %v", err)
}
r, err := srv.Domains.List(myAccoutsCustomerID).Do()
if err != nil {
return "", fmt.Errorf("unable to retrieve domains: %v", err)
}
for _, each := range r.Domains {
if each.IsPrimary {
cachedPrimaryDomain = each.DomainName
return each.DomainName, nil
}
}
return "", errors.New("no primary domain found")
}