forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.go
166 lines (144 loc) · 3.64 KB
/
misc.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"os"
"strings"
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
func initializeAPI(c *cli.Context) error {
apiKey := os.Getenv("CF_API_KEY")
if apiKey == "" {
err := errors.New("No CF_API_KEY environment set")
fmt.Fprintln(os.Stderr, err)
return err
}
apiEmail := os.Getenv("CF_API_EMAIL")
if apiEmail == "" {
err := errors.New("No CF_API_EMAIL environment set")
fmt.Fprintln(os.Stderr, err)
return err
}
// Be aware the following code sets the global package `api` variable
var err error
api, err = cloudflare.New(apiKey, apiEmail)
if err != nil {
fmt.Fprintf(os.Stderr, "cloudflare api: %s", err)
return err
}
if c.IsSet("accountid") {
cloudflare.UsingOrganization(c.String("accountid"))(api)
}
return nil
}
// writeTable outputs tabular data to stdout.
func writeTable(data [][]string, cols ...string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(cols)
table.SetBorder(false)
table.AppendBulk(data)
table.Render()
}
// Utility function to check if CLI flags were given.
func checkFlags(c *cli.Context, flags ...string) error {
for _, flag := range flags {
if c.String(flag) == "" {
cli.ShowSubcommandHelp(c)
err := errors.Errorf("error: the required flag %q was empty or not provided", flag)
fmt.Fprintln(os.Stderr, err)
return err
}
}
return nil
}
func ips(c *cli.Context) {
if c.String("ip-type") == "all" {
_getIps("ipv4", c.Bool("ip-only"))
_getIps("ipv6", c.Bool("ip-only"))
} else {
_getIps(c.String("ip-type"), c.Bool("ip-only"))
}
}
//
// gets type of IPs to retrieve and returns results
//
func _getIps(ipType string, showMsgType bool) {
ips, _ := cloudflare.IPs()
switch ipType {
case "ipv4":
if showMsgType != true {
fmt.Println("IPv4 ranges:")
}
for _, r := range ips.IPv4CIDRs {
fmt.Println(" ", r)
}
case "ipv6":
if showMsgType != true {
fmt.Println("IPv6 ranges:")
}
for _, r := range ips.IPv6CIDRs {
fmt.Println(" ", r)
}
}
}
func userInfo(*cli.Context) {
user, err := api.UserDetails()
if err != nil {
fmt.Println(err)
return
}
var output [][]string
output = append(output, []string{
user.ID,
user.Email,
user.Username,
user.FirstName + " " + user.LastName,
fmt.Sprintf("%t", user.TwoFA),
})
writeTable(output, "ID", "Email", "Username", "Name", "2FA")
}
func userUpdate(*cli.Context) {
}
func pageRules(c *cli.Context) {
if err := checkFlags(c, "zone"); err != nil {
return
}
zone := c.String("zone")
zoneID, err := api.ZoneIDByName(zone)
if err != nil {
fmt.Println(err)
return
}
rules, err := api.ListPageRules(zoneID)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%3s %-32s %-8s %s\n", "Pri", "ID", "Status", "URL")
for _, r := range rules {
var settings []string
fmt.Printf("%3d %s %-8s %s\n", r.Priority, r.ID, r.Status, r.Targets[0].Constraint.Value)
for _, a := range r.Actions {
var s string
switch v := a.Value.(type) {
case int:
s = fmt.Sprintf("%s: %d", cloudflare.PageRuleActions[a.ID], v)
case float64:
s = fmt.Sprintf("%s: %.f", cloudflare.PageRuleActions[a.ID], v)
case map[string]interface{}:
s = fmt.Sprintf("%s: %.f - %s", cloudflare.PageRuleActions[a.ID], v["status_code"], v["url"])
case nil:
s = fmt.Sprintf("%s", cloudflare.PageRuleActions[a.ID])
default:
vs := fmt.Sprintf("%s", v)
s = fmt.Sprintf("%s: %s", cloudflare.PageRuleActions[a.ID], strings.Title(strings.Replace(vs, "_", " ", -1)))
}
settings = append(settings, s)
}
fmt.Println(" ", strings.Join(settings, ", "))
}
}
func railgun(*cli.Context) {
}