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:
73
cmd_batch.go
Normal file
73
cmd_batch.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// handleBatch executes commands from a file
|
||||
func (c *KVSClient) handleBatch(args []string, executor func(string) bool) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println(red("Usage: batch <file> [--continue-on-error]"))
|
||||
fmt.Println(" source <file> [--continue-on-error]")
|
||||
return
|
||||
}
|
||||
|
||||
filename := args[0]
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
fmt.Println(red("Error opening file:"), err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineNum := 0
|
||||
successCount := 0
|
||||
errorCount := 0
|
||||
skippedCount := 0
|
||||
|
||||
for scanner.Scan() {
|
||||
lineNum++
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
// Skip empty lines and comments
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
skippedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
// Execute the command
|
||||
fmt.Printf(cyan("[%d]")+" %s\n", lineNum, line)
|
||||
|
||||
// Block exit/quit commands in batch mode
|
||||
parts := parseCommand(line)
|
||||
if len(parts) > 0 && (parts[0] == "exit" || parts[0] == "quit") {
|
||||
fmt.Println(yellow("Note: exit/quit ignored in batch mode"))
|
||||
skippedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
// Execute command
|
||||
executor(line)
|
||||
successCount++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Println(red("Error reading file:"), err)
|
||||
return
|
||||
}
|
||||
|
||||
// Print summary
|
||||
fmt.Println()
|
||||
fmt.Println(cyan("Batch execution complete:"))
|
||||
fmt.Printf(" Total lines: %d\n", lineNum)
|
||||
fmt.Printf(" Executed: %s\n", green(fmt.Sprintf("%d", successCount)))
|
||||
fmt.Printf(" Skipped: %d (empty/comments)\n", skippedCount)
|
||||
if errorCount > 0 {
|
||||
fmt.Printf(" Errors: %s\n", red(fmt.Sprintf("%d", errorCount)))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user