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:
61
client.go
Normal file
61
client.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// KVSClient maintains the connection state and configuration
|
||||
type KVSClient struct {
|
||||
baseURL string
|
||||
currentToken string
|
||||
currentUser string
|
||||
httpClient *http.Client
|
||||
profiles map[string]Profile
|
||||
activeProfile string
|
||||
}
|
||||
|
||||
// NewKVSClient creates a new KVS client instance
|
||||
func NewKVSClient(baseURL string) *KVSClient {
|
||||
return &KVSClient{
|
||||
baseURL: baseURL,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
profiles: make(map[string]Profile),
|
||||
}
|
||||
}
|
||||
|
||||
// doRequest performs an HTTP request with JSON body and auth headers
|
||||
func (c *KVSClient) doRequest(method, path string, body interface{}) ([]byte, int, error) {
|
||||
var reqBody io.Reader
|
||||
if body != nil {
|
||||
jsonData, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
reqBody = bytes.NewBuffer(jsonData)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, c.baseURL+path, reqBody)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if c.currentToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.currentToken)
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
return respBody, resp.StatusCode, err
|
||||
}
|
Reference in New Issue
Block a user