forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_agent.go
146 lines (117 loc) · 3.08 KB
/
user_agent.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
package main
import (
"fmt"
"os"
"strconv"
"github.com/cloudflare/cloudflare-go"
"github.com/urfave/cli/v2"
)
func formatUserAgentRule(rule cloudflare.UserAgentRule) []string {
return []string{
rule.ID,
rule.Description,
rule.Mode,
rule.Configuration.Value,
strconv.FormatBool(rule.Paused),
}
}
func userAgentCreate(c *cli.Context) error {
if err := checkFlags(c, "zone", "mode", "value"); err != nil {
fmt.Println(err)
return err
}
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Println(err)
return err
}
userAgentRule := cloudflare.UserAgentRule{
Description: c.String("description"),
Mode: c.String("mode"),
Paused: c.Bool("paused"),
Configuration: cloudflare.UserAgentRuleConfig{
Target: "ua",
Value: c.String("value"),
},
}
resp, err := api.CreateUserAgentRule(zoneID, userAgentRule)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating User-Agent block rule: ", err)
return err
}
output := [][]string{
formatUserAgentRule(resp.Result),
}
writeTable(c, output, "ID", "Description", "Mode", "Value", "Paused")
return nil
}
func userAgentUpdate(c *cli.Context) error {
if err := checkFlags(c, "zone", "id", "mode", "value"); err != nil {
return err
}
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
userAgentRule := cloudflare.UserAgentRule{
Description: c.String("description"),
Mode: c.String("mode"),
Paused: c.Bool("paused"),
Configuration: cloudflare.UserAgentRuleConfig{
Target: "ua",
Value: c.String("value"),
},
}
resp, err := api.UpdateUserAgentRule(zoneID, c.String("id"), userAgentRule)
if err != nil {
fmt.Fprintln(os.Stderr, "Error updating User-Agent block rule: ", err)
return err
}
output := [][]string{
formatUserAgentRule(resp.Result),
}
writeTable(c, output, "ID", "Description", "Mode", "Value", "Paused")
return nil
}
func userAgentDelete(c *cli.Context) error {
if err := checkFlags(c, "zone", "id"); err != nil {
return err
}
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
resp, err := api.DeleteUserAgentRule(zoneID, c.String("id"))
if err != nil {
fmt.Fprintln(os.Stderr, "Error deleting User-Agent block rule: ", err)
return err
}
output := [][]string{
formatUserAgentRule(resp.Result),
}
writeTable(c, output, "ID", "Description", "Mode", "Value", "Paused")
return nil
}
func userAgentList(c *cli.Context) error {
if err := checkFlags(c, "zone", "page"); err != nil {
return err
}
zoneID, err := api.ZoneIDByName(c.String("zone"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
resp, err := api.ListUserAgentRules(zoneID, c.Int("page"))
if err != nil {
fmt.Fprintln(os.Stderr, "Error listing User-Agent block rules: ", err)
return err
}
output := make([][]string, 0, len(resp.Result))
for _, rule := range resp.Result {
output = append(output, formatUserAgentRule(rule))
}
writeTable(c, output, "ID", "Description", "Mode", "Value", "Paused")
return nil
}