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>
This commit is contained in:
2025-10-05 23:54:47 +03:00
parent 33201227ca
commit 2f77c0a825
4 changed files with 142 additions and 52 deletions

114
main.go
View File

@@ -8,6 +8,67 @@ import (
"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")
@@ -47,59 +108,8 @@ func main() {
continue
}
parts := parseCommand(line)
if len(parts) == 0 {
continue
}
cmd := parts[0]
args := parts[1:]
switch cmd {
case "exit", "quit":
fmt.Println("Goodbye!")
if !client.executeCommand(line) {
return
case "clear":
print("\033[H\033[2J")
case "help":
client.handleHelp(args)
case "connect":
client.handleConnect(args)
case "auth":
client.handleAuth(args)
case "profile":
client.handleProfile(args)
case "get":
client.handleGet(args)
case "put":
client.handlePut(args)
case "delete":
client.handleDelete(args)
case "meta":
client.handleMeta(args)
case "members":
client.handleMembers(args)
case "health":
client.handleHealth(args)
case "user":
if len(args) > 0 {
switch args[0] {
case "get":
client.handleUserGet(args[1:])
case "create":
client.handleUserCreate(args[1:])
default:
fmt.Println(red("Unknown user command:"), args[0])
fmt.Println("Type 'help' for available commands")
}
} else {
client.handleUserList(args)
}
case "group":
client.handleGroup(args)
default:
fmt.Println(red("Unknown command:"), cmd)
fmt.Println("Type 'help' for available commands")
}
}
}