-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
repl.go
140 lines (117 loc) · 2.96 KB
/
repl.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
package repl
import (
"context"
"fmt"
"strings"
"sync"
"github.com/c-bata/go-prompt"
"github.com/pterm/pterm"
session "github.com/shortlink-org/shortlink/boundaries/shortdb/shortdb/domain/session/v1"
"github.com/shortlink-org/shortlink/boundaries/shortdb/shortdb/engine"
"github.com/shortlink-org/shortlink/boundaries/shortdb/shortdb/engine/file"
parser "github.com/shortlink-org/shortlink/boundaries/shortdb/shortdb/parser/v1"
)
type Repl struct {
engine engine.Engine
session *session.Session
mu sync.Mutex
}
func New(ctx context.Context, s *session.Session) (*Repl, error) {
// set engine
store, err := engine.New(ctx, "file", file.SetName(s.GetCurrentDatabase()), file.SetPath("/tmp/shortdb_repl"))
if err != nil {
return nil, err
}
return &Repl{
session: s,
engine: store,
}, nil
}
func (r *Repl) Run() { //nolint:gocyclo,gocognit // ignore
// load history
if err := r.init(); err != nil {
pterm.FgRed.Println(err)
}
// Show help snippet
r.help()
for {
t := prompt.Input("> ", completer,
prompt.OptionTitle("shortdb"),
prompt.OptionHistory(r.session.GetHistory()),
prompt.OptionPrefixTextColor(prompt.Yellow),
prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
prompt.OptionSuggestionBGColor(prompt.DarkGray),
)
if t == "" {
continue
}
// if this next line
if t[len(t)-1] == ';' || t[0] == '.' {
t = fmt.Sprintf("%s %s", r.session.GetRaw(), t)
r.session.Raw = ""
r.session.Exec = true
// set in history
t = strings.TrimSpace(t)
r.session.History = append(r.session.GetHistory(), t)
} else {
r.session.Raw += fmt.Sprintf("%s ", t)
r.session.Exec = false
}
t = strings.TrimSpace(t)
switch t[0] {
case '.': // if this command
s := strings.Split(t, " ")
switch s[0] {
case ".close":
if err := r.close(); err != nil {
pterm.FgRed.Println(err)
}
pterm.FgYellow.Println("Good buy!")
return
case ".open":
if err := r.open(t); err != nil {
pterm.FgRed.Println(err)
}
case ".help":
r.help()
case ".save":
if err := r.save(); err != nil {
pterm.FgRed.Println(err)
continue
}
pterm.FgGreen.Println("Saved!")
default:
pterm.FgRed.Println("incorrect command")
}
default: // if this not command then this SQL-expression
// if this multiline then skip
if !r.session.GetExec() {
continue
}
p, err := parser.New(t)
if err != nil {
pterm.FgRed.Println(err)
continue
}
// exec query
response, err := r.engine.Exec(p.GetQuery())
if err != nil && err.Error() != "" {
pterm.FgRed.Println(err)
continue
}
if response != nil {
pterm.FgGreen.Println(response)
} else {
pterm.FgGreen.Println(`Executed`)
}
}
}
}
func completer(in prompt.Document) []prompt.Suggest {
w := in.GetWordBeforeCursor()
if w == "" {
return []prompt.Suggest{}
}
return prompt.FilterHasPrefix(suggestions, w, true)
}