refactor: split monolithic main.go into modular structure
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>
This commit is contained in:
86
cmd_user.go
Normal file
86
cmd_user.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *KVSClient) handleUserList(args []string) {
|
||||
// Note: Backend doesn't have a list-all endpoint, this would need to be added
|
||||
fmt.Println(yellow("User listing not implemented in backend API"))
|
||||
}
|
||||
|
||||
func (c *KVSClient) handleUserGet(args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println(red("Usage: user get <uuid>"))
|
||||
return
|
||||
}
|
||||
|
||||
respBody, status, err := c.doRequest("GET", "/api/users/"+args[0], nil)
|
||||
if err != nil {
|
||||
fmt.Println(red("Error:"), err)
|
||||
return
|
||||
}
|
||||
|
||||
if status == 404 {
|
||||
fmt.Println(yellow("User not found"))
|
||||
return
|
||||
}
|
||||
|
||||
if status != 200 {
|
||||
fmt.Println(red("Error:"), string(respBody))
|
||||
return
|
||||
}
|
||||
|
||||
var user User
|
||||
if err := json.Unmarshal(respBody, &user); err != nil {
|
||||
fmt.Println(red("Error parsing response:"), err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(cyan("User Details:"))
|
||||
fmt.Printf(" UUID: %s\n", user.UUID)
|
||||
fmt.Printf(" Nickname Hash: %s\n", user.NicknameHash)
|
||||
fmt.Printf(" Groups: %v\n", user.Groups)
|
||||
fmt.Printf(" Created: %s\n", time.Unix(user.CreatedAt, 0).Format(time.RFC3339))
|
||||
fmt.Printf(" Updated: %s\n", time.Unix(user.UpdatedAt, 0).Format(time.RFC3339))
|
||||
}
|
||||
|
||||
func (c *KVSClient) handleUserCreate(args []string) {
|
||||
if len(args) < 1 {
|
||||
fmt.Println(red("Usage: user create <nickname>"))
|
||||
return
|
||||
}
|
||||
|
||||
// The request body requires a JSON object like {"nickname": "..."}
|
||||
requestPayload := map[string]string{
|
||||
"nickname": args[0],
|
||||
}
|
||||
|
||||
respBody, status, err := c.doRequest("POST", "/api/users", requestPayload)
|
||||
if err != nil {
|
||||
fmt.Println(red("Error:"), err)
|
||||
return
|
||||
}
|
||||
|
||||
// The backend returns 200 OK on success
|
||||
if status != http.StatusOK {
|
||||
fmt.Printf(red("Error creating user (status %d):\n"), status)
|
||||
fmt.Println(string(respBody))
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the successful response to get the new user's UUID
|
||||
var response struct {
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &response); err != nil {
|
||||
fmt.Println(red("Error parsing successful response:"), err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(green("User created successfully"))
|
||||
fmt.Println(cyan("UUID:"), response.UUID)
|
||||
}
|
Reference in New Issue
Block a user