-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmds.go
More file actions
206 lines (166 loc) · 4.52 KB
/
cmds.go
File metadata and controls
206 lines (166 loc) · 4.52 KB
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
package main
import (
"fmt"
"io"
"strings"
)
type SystemCmd interface {
Handle(args []string, rawInput string, resultWriter io.Writer) error
Name() string
Description() string
Usage() string
}
var (
RegisteredSystemCmds = []SystemCmd{
HelpCmd{},
VerCmd{},
RefreshCmd{},
ConnectCmd{},
OutputFormatCmd{},
AskCmd{},
LuaCmd{},
LuaRunFileCmd{},
}
)
func SystemCmdNames() []string {
names := make([]string, len(RegisteredSystemCmds))
for i, cmd := range RegisteredSystemCmds {
names[i] = cmd.Name()
}
return names
}
func handleCmd(line string, resultWriter io.Writer) error {
line = strings.TrimSpace(line)
cmdName := strings.Split(line, " ")[0]
params := strings.Split(line, " ")[1:]
for _, cmd := range RegisteredSystemCmds {
if cmd.Name() == cmdName {
return cmd.Handle(params, line, resultWriter)
}
}
resultWriter.Write([]byte("Unknown command: " + cmdName + ", use .help for help\n"))
return nil
}
type HelpCmd struct{}
func (cmd HelpCmd) Name() string {
return ".help"
}
func (cmd HelpCmd) Description() string {
return "Display help information for all available commands"
}
func (cmd HelpCmd) Usage() string {
return ".help"
}
func (cmd HelpCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
for _, cmd := range RegisteredSystemCmds {
resultWriter.Write([]byte(cmd.Name() + " - " + cmd.Description() + "- Usage: " + cmd.Usage() + "\n"))
}
return nil
}
type VerCmd struct{}
func (cmd VerCmd) Name() string {
return ".ver"
}
func (cmd VerCmd) Description() string {
return "Display the current version of tip"
}
func (cmd VerCmd) Usage() string {
return ".ver"
}
func (cmd VerCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
resultWriter.Write([]byte("tip version: " + Version + "\n"))
return nil
}
type RefreshCmd struct{}
func (cmd RefreshCmd) Name() string {
return ".refresh_completion"
}
func (cmd RefreshCmd) Description() string {
return "Refresh completion (not implemented yet)"
}
func (cmd RefreshCmd) Usage() string {
return ".refresh_completion"
}
func (cmd RefreshCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
resultWriter.Write([]byte("not impl yet\n"))
return nil
}
type ConnectCmd struct{}
func (cmd ConnectCmd) Name() string {
return ".connect"
}
func (cmd ConnectCmd) Description() string {
return "Connect to a TiDB database"
}
func (cmd ConnectCmd) Usage() string {
return ".connect <host> <port> <user> <password> [database]"
}
func (cmd ConnectCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
if len(args) < 4 {
return fmt.Errorf("usage: .connect <host> <port> <user> <password> [database]")
}
host := args[0]
port := args[1]
user := args[2]
pass := args[3]
dbName := ""
if len(args) > 4 {
dbName = args[4]
} else if GetLastUsedDB() != "" {
// If no database is specified and we have a last used database, use it
dbName = GetLastUsedDB()
} else {
dbName = "test" // Default database if no last used database
}
connInfo := ConnInfo{
Host: host,
Port: port,
User: user,
Password: pass,
Database: dbName,
}
err := connectToDatabase(connInfo)
if err != nil {
return fmt.Errorf("failed to connect to TiDB: %v", err)
}
resultWriter.Write([]byte("Connected successfully.\n"))
return nil
}
type OutputFormatCmd struct{}
func (cmd OutputFormatCmd) Name() string {
return ".output_format"
}
func (cmd OutputFormatCmd) Description() string {
return "Set or display the current output format"
}
func (cmd OutputFormatCmd) Usage() string {
return ".output_format [format]"
}
func (cmd OutputFormatCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
if len(args) == 0 {
// If no arguments, print the current output format and available options
current := *globalOutputFormat
options := []string{"json", "table", "plain", "csv"}
formattedOptions := make([]string, len(options))
for i, opt := range options {
if opt == current.String() {
formattedOptions[i] = "[" + opt + "]"
} else {
formattedOptions[i] = opt
}
}
resultWriter.Write([]byte(fmt.Sprintf("%s\n", strings.Join(formattedOptions, " "))))
return nil
}
if len(args) != 1 {
return fmt.Errorf("usage: .output_format <format>")
}
format := parseOutputFormat(args[0])
if format == Plain && args[0] != "plain" {
return fmt.Errorf("invalid format: %s", args[0])
}
// Update the global outputFormat variable
*globalOutputFormat = format
resultWriter.Write([]byte(fmt.Sprintf("Output format set to: %s\n", format)))
return nil
}