Files
kvs-sh/main.go
ryyst 2f77c0a825 feat: add batch command execution from files
Implemented roadmap #3: Batch operations from file.

Features:
- batch <file> or source <file> - Execute commands from script file
- Skips empty lines and comments (lines starting with #)
- Shows line numbers during execution for easy debugging
- Blocks exit/quit commands in batch mode (won't exit shell)
- Displays execution summary with counts

Implementation:
- Refactored main loop to extract executeCommand() method
- Created cmd_batch.go with batch file processor
- Reads file line by line, processes each command
- Summary shows total lines, executed count, and skipped count

Example batch file:
  # Connect and setup
  connect http://localhost:8080
  auth <token>

  # Batch insert
  put key1 '{"data":"value1"}'
  put key2 '{"data":"value2"}'

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-05 23:54:47 +03:00

116 lines
2.1 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"github.com/chzyer/readline"
)
// executeCommand executes a single command line
func (c *KVSClient) executeCommand(line string) bool {
parts := parseCommand(line)
if len(parts) == 0 {
return true
}
cmd := parts[0]
args := parts[1:]
switch cmd {
case "exit", "quit":
fmt.Println("Goodbye!")
return false
case "clear":
print("\033[H\033[2J")
case "help":
c.handleHelp(args)
case "connect":
c.handleConnect(args)
case "auth":
c.handleAuth(args)
case "profile":
c.handleProfile(args)
case "get":
c.handleGet(args)
case "put":
c.handlePut(args)
case "delete":
c.handleDelete(args)
case "meta":
c.handleMeta(args)
case "members":
c.handleMembers(args)
case "health":
c.handleHealth(args)
case "user":
if len(args) > 0 {
switch args[0] {
case "get":
c.handleUserGet(args[1:])
case "create":
c.handleUserCreate(args[1:])
default:
fmt.Println(red("Unknown user command:"), args[0])
fmt.Println("Type 'help' for available commands")
}
} else {
c.handleUserList(args)
}
case "group":
c.handleGroup(args)
case "batch", "source":
c.handleBatch(args, c.executeCommand)
default:
fmt.Println(red("Unknown command:"), cmd)
fmt.Println("Type 'help' for available commands")
}
return true
}
func main() {
client := NewKVSClient("http://localhost:8090")
// Load saved profiles
if config, err := LoadConfig(); err == nil {
client.syncConfigToClient(config)
}
// Setup readline
rl, err := readline.NewEx(&readline.Config{
Prompt: cyan("kvs> "),
HistoryFile: os.Getenv("HOME") + "/.kvs_history",
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
})
if err != nil {
panic(err)
}
defer rl.Close()
fmt.Println(magenta("KVS Interactive Shell"))
fmt.Println("Type 'help' for available commands")
if client.activeProfile != "" {
fmt.Println(green("Active profile:"), client.activeProfile)
}
fmt.Println()
for {
line, err := rl.Readline()
if err != nil {
break
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
if !client.executeCommand(line) {
return
}
}
}