-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (50 loc) · 1.3 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/northwood-labs/aws-account-list/accountlist"
"github.com/northwood-labs/awsutils"
"github.com/northwood-labs/golang-utils/exiterrorf"
)
const (
// The number of results to request per page.
orgResultCount = 20
)
var (
// Global flags.
retries *int
verbose *bool
// Context.
ctx = context.Background()
orgRole = os.Getenv("AWS_ORG_ROLE")
)
func main() {
// Flags
retries = flag.Int("retries", 5, "The number of times to retry failed requests to AWS.")
verbose = flag.Bool("verbose", false, "Output internal data to stdout.")
flag.Parse()
config, err := awsutils.GetAWSConfig(ctx, "", "", *retries, *verbose)
if err != nil {
exiterrorf.ExitErrorf(err)
}
var orgClient *organizations.Client
if orgRole == "" {
orgClient = organizations.NewFromConfig(config)
} else {
orgClient = accountlist.GetSTSEnabledOrgClient(&config, orgRole)
}
paginator := accountlist.GetOrgListAccountsPaginator(orgClient, orgResultCount)
accountTags, err := accountlist.CollectAccountTags(ctx, orgClient, paginator)
if err != nil {
exiterrorf.ExitErrorf(err)
}
b, err := json.Marshal(accountTags)
if err != nil {
exiterrorf.ExitErrorf(err)
}
fmt.Println(string(b))
}