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>
138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// handleMeta dispatches metadata sub-commands
|
|
func (c *KVSClient) handleMeta(args []string) {
|
|
if len(args) == 0 {
|
|
fmt.Println(red("Usage: meta <get|set> [options]"))
|
|
return
|
|
}
|
|
|
|
subCmd := args[0]
|
|
switch subCmd {
|
|
case "get":
|
|
c.handleMetaGet(args[1:])
|
|
case "set":
|
|
c.handleMetaSet(args[1:])
|
|
default:
|
|
fmt.Println(red("Unknown meta command:"), subCmd)
|
|
fmt.Println("Available commands: get, set")
|
|
}
|
|
}
|
|
|
|
// handleMetaGet fetches and displays metadata for a key
|
|
func (c *KVSClient) handleMetaGet(args []string) {
|
|
if len(args) < 1 {
|
|
fmt.Println(red("Usage: meta get <key>"))
|
|
return
|
|
}
|
|
key := args[0]
|
|
|
|
respBody, status, err := c.doRequest("GET", "/kv/"+key+"/metadata", nil)
|
|
if err != nil {
|
|
fmt.Println(red("Error:"), err)
|
|
return
|
|
}
|
|
|
|
if status == http.StatusNotFound {
|
|
fmt.Println(yellow("No metadata found for key:"), key)
|
|
return
|
|
}
|
|
|
|
if status != http.StatusOK {
|
|
fmt.Printf(red("Error getting metadata (status %d):\n"), status)
|
|
fmt.Println(string(respBody))
|
|
return
|
|
}
|
|
|
|
// Struct to parse the metadata response
|
|
type MetaResponse struct {
|
|
OwnerUUID string `json:"owner_uuid"`
|
|
GroupUUID string `json:"group_uuid"`
|
|
Permissions int `json:"permissions"`
|
|
CreatedAt int64 `json:"created_at"`
|
|
UpdatedAt int64 `json:"updated_at"`
|
|
}
|
|
|
|
var meta MetaResponse
|
|
if err := json.Unmarshal(respBody, &meta); err != nil {
|
|
fmt.Println(red("Error parsing metadata response:"), err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(cyan("Metadata for key:"), key)
|
|
fmt.Printf(" Owner UUID: %s\n", meta.OwnerUUID)
|
|
fmt.Printf(" Group UUID: %s\n", meta.GroupUUID)
|
|
fmt.Printf(" Permissions: %d\n", meta.Permissions)
|
|
fmt.Printf(" Created At: %s\n", time.Unix(meta.CreatedAt, 0).Format(time.RFC3339))
|
|
fmt.Printf(" Updated At: %s\n", time.Unix(meta.UpdatedAt, 0).Format(time.RFC3339))
|
|
}
|
|
|
|
// handleMetaSet updates metadata for a key using flags
|
|
func (c *KVSClient) handleMetaSet(args []string) {
|
|
if len(args) < 3 {
|
|
fmt.Println(red("Usage: meta set <key> --owner <uuid> | --group <uuid> | --permissions <number>"))
|
|
return
|
|
}
|
|
key := args[0]
|
|
|
|
// Use a map to build the JSON payload for partial updates
|
|
payload := make(map[string]interface{})
|
|
|
|
// Parse command-line flags
|
|
for i := 1; i < len(args); i++ {
|
|
switch args[i] {
|
|
case "--owner":
|
|
if i+1 < len(args) {
|
|
payload["owner_uuid"] = args[i+1]
|
|
i++ // Skip the value
|
|
}
|
|
case "--group":
|
|
if i+1 < len(args) {
|
|
payload["group_uuid"] = args[i+1]
|
|
i++ // Skip the value
|
|
}
|
|
case "--permissions":
|
|
if i+1 < len(args) {
|
|
perms, err := strconv.Atoi(args[i+1])
|
|
if err != nil {
|
|
fmt.Println(red("Invalid permissions value:"), args[i+1])
|
|
return
|
|
}
|
|
payload["permissions"] = perms
|
|
i++ // Skip the value
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(payload) == 0 {
|
|
fmt.Println(red("No valid metadata fields provided to set."))
|
|
return
|
|
}
|
|
|
|
respBody, status, err := c.doRequest("PUT", "/kv/"+key+"/metadata", payload)
|
|
if err != nil {
|
|
fmt.Println(red("Error:"), err)
|
|
return
|
|
}
|
|
|
|
if status != http.StatusOK {
|
|
fmt.Printf(red("Error setting metadata (status %d):\n"), status)
|
|
fmt.Println(string(respBody))
|
|
return
|
|
}
|
|
|
|
fmt.Println(green("Metadata updated successfully for key:"), key)
|
|
var pretty bytes.Buffer
|
|
json.Indent(&pretty, respBody, "", " ")
|
|
fmt.Println(pretty.String())
|
|
}
|