forked from cockroachdb/cockroach
/
context.go
148 lines (124 loc) · 3.19 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
// 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"
"strings"
"github.com/cockroachdb/cockroach/base"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/storage/engine"
)
// 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.Context
// prettyFmt indicates whether tables should be pretty-formatted in
// the output during non-interactive execution.
prettyFmt bool
}
func (ctx *cliContext) InitCLIDefaults() {
ctx.prettyFmt = false
}
type sqlContext struct {
// Embed the cli context.
*cliContext
// execStmts is a list of statements to execute.
execStmts statementsValue
}
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)
}
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:
*k = mvccKey(engine.MakeMVCCMetadataKey(roachpb.Key(unquoteArg(keyStr, false))))
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
}