package main import ( "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", "path to json config file") historyPath := flag.String("history", defaultHistoryPath(), "path to history file") flag.Parse() cfg, err := LoadConfig(*configPath) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } session, err := Connect(cfg) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } defer session.Close() fmt.Printf("connected to %s:%d keyspace=%s\n", cfg.Server, cfg.Port, 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 ".cqltui_history" } return filepath.Join(home, ".cqltui_history") } func runLoop(session *gocql.Session, historyPath string) { rl, err := readline.NewEx(&readline.Config{ Prompt: "cql> ", HistoryFile: historyPath, HistoryLimit: 1000, DisableAutoSaveHistory: true, }) if err != nil { PrintError(fmt.Errorf("initializing readline: %w", err)) return } defer rl.Close() var buffer strings.Builder for { if buffer.Len() == 0 { rl.SetPrompt("cql> ") } 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 := RunQuery(session, query) if err != nil { PrintError(err) continue } PrintTable(result) } }