/
add.go
174 lines (153 loc) · 4.11 KB
/
add.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
167
168
169
170
171
172
173
174
package cmd
import (
"log"
"os"
"strings"
"github.com/spf13/cobra"
"vitess.io/vitess/go/cmd/rulesctl/common"
"vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder"
vtrules "vitess.io/vitess/go/vt/vttablet/tabletserver/rules"
)
var (
addOptDryrun bool
addOptName string
addOptDescription string
addOptAction string
addOptPlans []string
addOptTables []string
addOptQueryRE string
addOptLeadingCommentRE string
addOptTrailingCommentRE string
// TODO: other stuff, bind vars etc
)
func runAdd(cmd *cobra.Command, args []string) {
rulePlans := mkPlanSlice()
ruleAction := mkAction()
rule := vtrules.NewQueryRule(addOptDescription, addOptName, ruleAction)
for _, pt := range rulePlans {
rule.AddPlanCond(pt)
}
for _, t := range addOptTables {
rule.AddTableCond(t)
}
if addOptQueryRE != "" {
if err := rule.SetQueryCond(addOptQueryRE); err != nil {
log.Fatalf("Query condition invalid '%v': %v", addOptQueryRE, err)
}
}
if addOptLeadingCommentRE != "" {
if err := rule.SetLeadingCommentCond(addOptLeadingCommentRE); err != nil {
log.Fatalf("Leading comment condition invalid '%v': %v", addOptLeadingCommentRE, err)
}
}
if addOptTrailingCommentRE != "" {
if err := rule.SetTrailingCommentCond(addOptTrailingCommentRE); err != nil {
log.Fatalf("Trailing comment condition invalid '%v': %v", addOptTrailingCommentRE, err)
}
}
var rules *vtrules.Rules
_, err := os.Stat(configFile)
if os.IsNotExist(err) {
rules = vtrules.New()
} else {
rules = common.GetRules(configFile)
}
existingRule := rules.Find(rule.Name)
if existingRule != nil {
log.Fatalf("Rule by name %q already exists", rule.Name)
}
rules.Add(rule)
if addOptDryrun {
common.MustPrintJSON(rules)
} else {
common.MustWriteJSON(rules, configFile)
}
}
func mkPlanSlice() []planbuilder.PlanType {
if len(addOptPlans) == 0 {
return nil
}
plans := []planbuilder.PlanType{}
badPlans := []string{}
for _, p := range addOptPlans {
if pbn, ok := planbuilder.PlanByNameIC(p); ok {
plans = append(plans, pbn)
} else {
badPlans = append(badPlans, p)
}
}
if len(badPlans) != 0 {
log.Fatalf("Unknown PlanType(s) %q", badPlans)
}
return plans
}
func mkAction() vtrules.Action {
switch strings.ToLower(addOptAction) {
case "fail":
return vtrules.QRFail
case "fail_retry":
return vtrules.QRFailRetry
case "continue":
return vtrules.QRContinue
default:
log.Fatalf("Unknown action '%v'", addOptAction)
}
panic("Nope")
}
func Add() *cobra.Command {
addCmd := &cobra.Command{
Use: "add-rule",
Short: "Adds a rule to the config file",
Args: cobra.NoArgs,
Run: runAdd,
}
addCmd.Flags().BoolVarP(
&addOptDryrun,
"dry-run", "d",
false,
"Instead of writing the config file back print the result to stdout")
addCmd.Flags().StringVarP(
&addOptName,
"name", "n",
"",
"The name of the rule to add (required)")
addCmd.Flags().StringVarP(
&addOptDescription,
"description", "e",
"",
"The purpose/description of the rule being added")
addCmd.Flags().StringVarP(
&addOptAction,
"action", "a",
"",
"What action should be taken when this rule is matched {continue, fail, fail-retry} (required)")
addCmd.Flags().StringSliceVarP(
&addOptPlans,
"plan", "p",
nil,
"Which query plan types does this rule match; see \"explain query-plans\" for details; may be specified multiple times")
addCmd.Flags().StringSliceVarP(
&addOptTables,
"table", "t",
nil,
"Queries will only match if running against these tables; may be specified multiple times")
addCmd.Flags().StringVarP(
&addOptQueryRE,
"query", "q",
"",
"A regexp that will be applied to a query in order to determine if it matches")
addCmd.Flags().StringVarP(
&addOptLeadingCommentRE,
"leading-comment", "l",
"",
"A regexp that will be applied to comments prefacing a SQL statement")
addCmd.Flags().StringVarP(
&addOptTrailingCommentRE,
"trailing-comment", "r",
"",
"A regexp that will be applied to comments after a SQL statement")
for _, f := range []string{"name", "action"} {
addCmd.MarkFlagRequired(f)
}
return addCmd
}