Refactored 800+ line main.go into clean, domain-separated files: - main.go (96 lines) - Entry point and command router only - types.go - Type definitions and data structures - client.go - HTTP client and request handling - cmd_kv.go - Key-value operations (get/put/delete) - cmd_meta.go - Resource metadata commands - cmd_user.go - User management commands - cmd_profile.go - Profile management - cmd_cluster.go - Cluster operations (members/health) - cmd_system.go - System commands (connect/auth/help) - utils.go - Shared utilities (parsing, colors, completion) No functional changes, pure reorganization for maintainability. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
126 lines
2.6 KiB
Go
126 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (c *KVSClient) handleGet(args []string) {
|
|
if len(args) < 1 {
|
|
fmt.Println(red("Usage: get <key>"))
|
|
return
|
|
}
|
|
|
|
respBody, status, err := c.doRequest("GET", "/kv/"+args[0], nil)
|
|
if err != nil {
|
|
fmt.Println(red("Error:"), err)
|
|
return
|
|
}
|
|
|
|
if status == 404 {
|
|
fmt.Println(yellow("Key not found"))
|
|
return
|
|
}
|
|
|
|
if status != 200 {
|
|
fmt.Println(red("Error:"), string(respBody))
|
|
return
|
|
}
|
|
|
|
var stored StoredValue
|
|
if err := json.Unmarshal(respBody, &stored); err != nil {
|
|
fmt.Println(red("Error parsing response:"), err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(cyan("UUID: "), stored.UUID)
|
|
fmt.Println(cyan("Timestamp:"), time.UnixMilli(stored.Timestamp).Format(time.RFC3339))
|
|
fmt.Println(cyan("Data:"))
|
|
|
|
var pretty bytes.Buffer
|
|
json.Indent(&pretty, stored.Data, "", " ")
|
|
fmt.Println(pretty.String())
|
|
}
|
|
|
|
func (c *KVSClient) handlePut(args []string) {
|
|
if len(args) < 1 {
|
|
fmt.Println(red("Usage: put <key> <json-data>"))
|
|
fmt.Println("Or: put <key> (then paste JSON on next line)")
|
|
return
|
|
}
|
|
|
|
var jsonStr string
|
|
if len(args) == 1 {
|
|
// Multiline mode - read from next input
|
|
fmt.Println(yellow("Enter JSON (Ctrl+D when done):"))
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
var lines []string
|
|
for scanner.Scan() {
|
|
lines = append(lines, scanner.Text())
|
|
}
|
|
jsonStr = strings.Join(lines, "\n")
|
|
} else {
|
|
// Single line mode
|
|
jsonStr = strings.Join(args[1:], " ")
|
|
}
|
|
|
|
// Parse JSON data
|
|
var data json.RawMessage
|
|
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
|
|
printJSONError(jsonStr, err)
|
|
return
|
|
}
|
|
|
|
respBody, status, err := c.doRequest("PUT", "/kv/"+args[0], data)
|
|
if err != nil {
|
|
fmt.Println(red("Error:"), err)
|
|
return
|
|
}
|
|
|
|
if status != 200 && status != 201 {
|
|
fmt.Println(red("Error:"), string(respBody))
|
|
return
|
|
}
|
|
|
|
type PutResponse struct {
|
|
UUID string `json:"uuid"`
|
|
Timestamp int64 `json:"timestamp"`
|
|
}
|
|
|
|
var resp PutResponse
|
|
if err := json.Unmarshal(respBody, &resp); err == nil {
|
|
fmt.Println(green("Stored successfully"))
|
|
fmt.Println(cyan("UUID: "), resp.UUID)
|
|
fmt.Println(cyan("Timestamp:"), time.UnixMilli(resp.Timestamp).Format(time.RFC3339))
|
|
}
|
|
}
|
|
|
|
func (c *KVSClient) handleDelete(args []string) {
|
|
if len(args) < 1 {
|
|
fmt.Println(red("Usage: delete <key>"))
|
|
return
|
|
}
|
|
|
|
_, status, err := c.doRequest("DELETE", "/kv/"+args[0], nil)
|
|
if err != nil {
|
|
fmt.Println(red("Error:"), err)
|
|
return
|
|
}
|
|
|
|
if status == 404 {
|
|
fmt.Println(yellow("Key not found"))
|
|
return
|
|
}
|
|
|
|
if status == 204 {
|
|
fmt.Println(green("Deleted successfully"))
|
|
} else {
|
|
fmt.Println(red("Unexpected status:"), status)
|
|
}
|
|
}
|