-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroot.go
71 lines (62 loc) · 1.77 KB
/
root.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
package cmd
import (
"encoding/json"
"fmt"
"log"
"os"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/ejcx/cf/lib"
"github.com/spf13/cobra"
)
var cfgFile string
type Credentials struct {
Email string
}
var RootCmd = &cobra.Command{
Use: "cf",
Short: "A CLI for interacting with Cloudflare's V4 API",
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
// Main is where the high level program execution takes place.
// The first thing that happens is the credentials are attempted
// to be loaded from a file or your environment. If the creds are
// loaded from a file then they are set as env vars to be used
// by cloudflare-go.
// Next, `root` is called, which is where API calls are made to
// the cloudflare v4 API by using the `cloudflare-go` library.
// Finally, the results are outputted to stdout (or stderr if
// something disasterous happens).
func Main(cmd *cobra.Command, args []string, name string) {
err := lib.DefaultCredentialProvider.ConfigureEnvironment()
if err != nil {
log.Fatalf("No set of credentials to use: %s", err)
}
var (
opts []cloudflare.Option
)
if OrganizationId != "" {
opts = append(opts, cloudflare.UsingOrganization(OrganizationId))
}
api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"), opts...)
if err != nil {
log.Fatalf("Could not initialize api object: %s", err)
}
if serviceKey, ok := os.LookupEnv("CF_USER_SERVICE_KEY"); ok {
api.APIUserServiceKey = serviceKey
}
r, err := Run(cmd, args, name, api)
if err != nil {
log.Fatalf("Could not make cloudflare request: %s", err)
}
buf, err := json.MarshalIndent(r, " ", " ")
if err != nil {
log.Fatalf("Could not make print resp: %s", err)
}
if string(buf) != "null" {
fmt.Println(string(buf))
}
}