forked from cockroachdb/cockroach
/
context.go
263 lines (226 loc) · 5.44 KB
/
context.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Raphael 'kena' Poss (knz@cockroachlabs.com)
package cli
import (
"fmt"
"strconv"
"strings"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/pkg/errors"
)
// statementsValue is an implementation of pflag.Value that appends any
// argument to a slice.
type statementsValue []string
func (s *statementsValue) String() string {
return strings.Join(*s, ";")
}
func (s *statementsValue) Type() string {
return "statementsValue"
}
func (s *statementsValue) Set(value string) error {
*s = append(*s, value)
return nil
}
type cliContext struct {
// Embed the base context.
*base.Config
// tableDisplayFormat indicates how to format result tables.
tableDisplayFormat tableDisplayFormat
}
type tableDisplayFormat int
const (
tableDisplayTSV tableDisplayFormat = iota
tableDisplayCSV
tableDisplayPretty
tableDisplayRecords
tableDisplaySQL
tableDisplayHTML
)
// Type implements the pflag.Value interface.
func (f *tableDisplayFormat) Type() string { return "string" }
// String implements the pflag.Value interface.
func (f *tableDisplayFormat) String() string {
switch *f {
case tableDisplayTSV:
return "tsv"
case tableDisplayCSV:
return "csv"
case tableDisplayPretty:
return "pretty"
case tableDisplayRecords:
return "records"
case tableDisplaySQL:
return "sql"
case tableDisplayHTML:
return "html"
}
return ""
}
// Set implements the pflag.Value interface.
func (f *tableDisplayFormat) Set(s string) error {
switch s {
case "tsv":
*f = tableDisplayTSV
case "csv":
*f = tableDisplayCSV
case "pretty":
*f = tableDisplayPretty
case "records":
*f = tableDisplayRecords
case "sql":
*f = tableDisplaySQL
case "html":
*f = tableDisplayHTML
default:
return fmt.Errorf("invalid table display format: %s "+
"(possible values: tsv, csv, pretty, records, sql, html)", s)
}
return nil
}
type sqlContext struct {
// Embed the cli context.
*cliContext
// execStmts is a list of statements to execute.
execStmts statementsValue
}
type dumpContext struct {
// Embed the cli context.
*cliContext
// dumpMode determines which part of the database should be dumped.
dumpMode dumpMode
asOf string
}
type dumpMode int
const (
dumpBoth dumpMode = iota
dumpSchemaOnly
dumpDataOnly
)
// Type implements the pflag.Value interface.
func (m *dumpMode) Type() string { return "string" }
// String implements the pflag.Value interface.
func (m *dumpMode) String() string {
switch *m {
case dumpBoth:
return "both"
case dumpSchemaOnly:
return "schema"
case dumpDataOnly:
return "data"
}
return ""
}
// Set implements the pflag.Value interface.
func (m *dumpMode) Set(s string) error {
switch s {
case "both":
*m = dumpBoth
case "schema":
*m = dumpSchemaOnly
case "data":
*m = dumpDataOnly
default:
return fmt.Errorf("invalid value for --dump-mode: %s", s)
}
return nil
}
type keyType int
//go:generate stringer -type=keyType
const (
raw keyType = iota
human
rangeID
)
var _keyTypes []string
func keyTypes() []string {
if _keyTypes == nil {
for i := 0; i+1 < len(_keyType_index); i++ {
_keyTypes = append(_keyTypes, _keyType_name[_keyType_index[i]:_keyType_index[i+1]])
}
}
return _keyTypes
}
func parseKeyType(value string) (keyType, error) {
for i, typ := range keyTypes() {
if strings.EqualFold(value, typ) {
return keyType(i), nil
}
}
return 0, fmt.Errorf("unknown key type '%s'", value)
}
// unquoteArg unquotes the provided argument using Go double-quoted
// string literal rules.
func unquoteArg(arg string) (string, error) {
s, err := strconv.Unquote(`"` + arg + `"`)
if err != nil {
return "", errors.Wrapf(err, "invalid argument %q", arg)
}
return s, nil
}
type mvccKey engine.MVCCKey
func (k *mvccKey) String() string {
return engine.MVCCKey(*k).String()
}
func (k *mvccKey) Set(value string) error {
var typ keyType
var keyStr string
i := strings.IndexByte(value, ':')
if i == -1 {
keyStr = value
} else {
var err error
typ, err = parseKeyType(value[:i])
if err != nil {
return err
}
keyStr = value[i+1:]
}
switch typ {
case raw:
unquoted, err := unquoteArg(keyStr)
if err != nil {
return err
}
*k = mvccKey(engine.MakeMVCCMetadataKey(roachpb.Key(unquoted)))
case human:
key, err := keys.UglyPrint(keyStr)
if err != nil {
return err
}
*k = mvccKey(engine.MakeMVCCMetadataKey(key))
case rangeID:
fromID, err := parseRangeID(keyStr)
if err != nil {
return err
}
*k = mvccKey(engine.MakeMVCCMetadataKey(keys.MakeRangeIDPrefix(fromID)))
default:
return fmt.Errorf("unknown key type %s", typ)
}
return nil
}
func (k *mvccKey) Type() string {
return "engine.MVCCKey"
}
type debugContext struct {
startKey, endKey engine.MVCCKey
values bool
sizes bool
replicated bool
}