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
|
/**
* CQL Minus - cqlminus
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Paolo Lulli <kevwe.com>
* @copyright Paolo Lulli 2026
*/
package main
import (
"cqlminus/internal/config"
"cqlminus/internal/db"
"cqlminus/internal/render"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/chzyer/readline"
"github.com/gocql/gocql"
)
func main() {
configPath := flag.String("config", os.Getenv("HOME")+"/.config/cqlminus/config.json", "config file")
historyPath := flag.String("history", defaultHistoryPath(), "history file")
flag.Parse()
cfg, err := config.LoadConfig(*configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
session, err := db.Connect(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
defer session.Close()
fmt.Printf("connected to %s keyspace=%s\n", cfg.Endpoint, cfg.Keyspace)
fmt.Println("enter CQL queries, terminate with ; then enter. type exit or quit to leave.")
fmt.Println("use up/down arrows to browse command history.")
runLoop(session, *historyPath)
}
func defaultHistoryPath() string {
home, err := os.UserHomeDir()
if err != nil {
return ".cqlminus_history"
}
return filepath.Join(home, ".cqlminus_history")
}
func runLoop(session *gocql.Session, historyPath string) {
rl, err := readline.NewEx(&readline.Config{
Prompt: "cqlminus> ",
HistoryFile: historyPath,
HistoryLimit: 1000,
DisableAutoSaveHistory: true,
})
if err != nil {
render.PrintError(fmt.Errorf("initializing readline: %w", err))
return
}
defer rl.Close()
var buffer strings.Builder
for {
if buffer.Len() == 0 {
rl.SetPrompt("cqlminus> ")
} else {
rl.SetPrompt("...> ")
}
line, err := rl.Readline()
if err == readline.ErrInterrupt {
buffer.Reset()
continue
}
if err == io.EOF {
return
}
if err != nil {
return
}
trimmed := strings.TrimSpace(line)
if buffer.Len() == 0 {
lower := strings.ToLower(trimmed)
if lower == "exit" || lower == "quit" {
return
}
if trimmed == "" {
continue
}
}
buffer.WriteString(line)
buffer.WriteString(" ")
if !strings.HasSuffix(trimmed, ";") {
continue
}
query := strings.TrimSpace(buffer.String())
query = strings.TrimSuffix(query, ";")
buffer.Reset()
if query == "" {
continue
}
rl.SaveHistory(query)
result, err := db.RunQuery(session, query)
if err != nil {
render.PrintError(err)
continue
}
render.PrintTable(result)
}
}
|