Merge branch 'feat/verbose-debug' into master

This commit is contained in:
2026-07-30 01:04:48 +08:00
9 changed files with 446 additions and 32 deletions
+8 -2
View File
@@ -26,7 +26,7 @@ var (
flagProvider string
flagOutput string
flagRaw bool
flagDebug bool
flagVerbose bool
)
var rootCmd = &cobra.Command{
@@ -67,6 +67,7 @@ Use "ry [command] --help" for more information about a command.`,
AccessKeyID: pc.AccessKeyID,
AccessKeySecret: pc.AccessKeySecret,
Endpoint: pc.Endpoint,
Debug: flagVerbose,
})
if err != nil {
return err
@@ -80,6 +81,11 @@ Use "ry [command] --help" for more information about a command.`,
outputFormat = "raw"
}
out = output.New(outputFormat, os.Stdout)
if flagVerbose {
fmt.Fprintf(os.Stderr, "[debug] provider: %s\n", providerName)
fmt.Fprintf(os.Stderr, "[debug] config: %s\n", cfgPath)
fmt.Fprintf(os.Stderr, "[debug] output: %s\n", outputFormat)
}
return nil
},
}
@@ -89,7 +95,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&flagProvider, "provider", "p", "", "Cloud provider to use")
rootCmd.PersistentFlags().StringVarP(&flagOutput, "output", "o", "", "Output format: table, json, yaml, raw")
rootCmd.PersistentFlags().BoolVar(&flagRaw, "raw", false, "Output raw API response")
rootCmd.PersistentFlags().BoolVar(&flagDebug, "debug", false, "Enable debug output")
rootCmd.PersistentFlags().BoolVarP(&flagVerbose, "verbose", "v", false, "Enable verbose output")
completionCmd := &cobra.Command{
Use: "completion [bash|zsh|fish]",
+316
View File
@@ -0,0 +1,316 @@
# Verbose/Debug Mode Implementation Plan
> **For agentic workers:** Use superpowers:subagent-driven-development or superpowers:executing-plans to implement this plan task-by-task.
**Goal:** Wire up `--debug`/`--verbose` flags to print HTTP request details and runtime info to stderr.
**Architecture:** Add `Debug bool` to `ProviderConfig`, pass it through to the HTTP client. Client's `do()` method logs request/response when debug is on. Root command prints runtime info in `PersistentPreRunE`.
**Tech Stack:** Go, cobra, net/http
## Global Constraints
- All debug output goes to `os.Stderr`
- `--verbose` is an alias for `--debug` (both set `flagDebug`)
- Debug prefix: `[debug]`
- Existing tests must pass after each task
---
### Task 1: Add Debug field to ProviderConfig
**Files:**
- Modify: `internal/provider/provider.go`
**Interfaces:**
- Produces: `ProviderConfig.Debug bool` — consumed by Task 3 and Task 4
- [ ] **Step 1: Add Debug field**
```go
// internal/provider/provider.go — add to ProviderConfig
type ProviderConfig struct {
APIKey string
AccessKeyID string
AccessKeySecret string
Endpoint string
Debug bool
}
```
- [ ] **Step 2: Verify build**
Run: `go build ./...`
Expected: PASS (field addition is backward-compatible)
- [ ] **Step 3: Commit**
```bash
git add internal/provider/provider.go
git commit -m "feat: add Debug field to ProviderConfig"
```
---
### Task 2: Update client to accept and use debug flag
**Files:**
- Modify: `internal/provider/rainyun/client.go`
- Modify: `internal/provider/rainyun/client_test.go` — update `newClient` calls
- Modify: `internal/provider/rainyun/server_test.go` — update `newClient` calls
**Interfaces:**
- Consumes: `debug bool` parameter
- Produces: `newClient(endpoint, apiKey string, debug bool) *client`
- Produces: stderr debug output with `[debug]` prefix
- [ ] **Step 1: Update client struct and newClient**
```go
// internal/provider/rainyun/client.go
type client struct {
httpClient *http.Client
baseURL string
apiKey string
debug bool
}
func newClient(endpoint, apiKey string, debug bool) *client {
if endpoint == "" {
endpoint = defaultEndpoint
}
return &client{
httpClient: &http.Client{Timeout: 30 * time.Second},
baseURL: endpoint,
apiKey: apiKey,
debug: debug,
}
}
```
- [ ] **Step 2: Add debug logging to do()**
Add to top of `client.go` imports: `"os"`, `"time"` (time already imported).
In `do()`, after building the URL and before creating the request, add:
```go
if c.debug {
fmt.Fprintf(os.Stderr, "[debug] > %s %s\n", method, u)
if body != nil {
fmt.Fprintf(os.Stderr, "[debug] %s\n", bodyReader.(*bytes.Reader). // wrong
}
}
```
Actually, the body bytes are already in `b` at that point. Better approach — log after marshaling:
```go
func (c *client) do(ctx context.Context, method, path string, body, result any, query map[string]string) error {
u := c.baseURL + path
if len(query) > 0 {
params := url.Values{}
for k, v := range query {
params.Set(k, v)
}
u += "?" + params.Encode()
}
var bodyReader io.Reader
var bodyBytes []byte
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshal request: %w", err)
}
bodyBytes = b
bodyReader = bytes.NewReader(b)
}
if c.debug {
fmt.Fprintf(os.Stderr, "[debug] > %s %s\n", method, u)
if bodyBytes != nil {
fmt.Fprintf(os.Stderr, "[debug] %s\n", bodyBytes)
}
}
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
if err != nil {
return fmt.Errorf("create request: %w", err)
}
req.Header.Set("X-Api-Key", c.apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
start := time.Now()
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read response: %w", err)
}
if c.debug {
fmt.Fprintf(os.Stderr, "[debug] < %d %s %s\n", resp.StatusCode, http.StatusText(resp.StatusCode), time.Since(start).Round(time.Millisecond))
fmt.Fprintf(os.Stderr, "[debug] %s\n", respBody)
}
// ... rest unchanged
```
- [ ] **Step 3: Update test calls to newClient**
In `client_test.go`, change all `newClient(srv.URL, "sk-test")` to `newClient(srv.URL, "sk-test", false)`.
In `server_test.go`, change all `newClient(srv.URL, "sk-test")` to `newClient(srv.URL, "sk-test", false)`.
- [ ] **Step 4: Run tests**
Run: `go test ./internal/provider/rainyun/...`
Expected: PASS
- [ ] **Step 5: Commit**
```bash
git add internal/provider/rainyun/
git commit -m "feat: HTTP debug logging in client.do()"
```
---
### Task 3: Pass debug flag from provider to client
**Files:**
- Modify: `internal/provider/rainyun/rainyun.go`
**Interfaces:**
- Consumes: `ProviderConfig.Debug` (from Task 1)
- Consumes: `newClient(endpoint, apiKey string, debug bool)` (from Task 2)
- [ ] **Step 1: Update New()**
```go
func New(cfg provider.ProviderConfig) (provider.Provider, error) {
c := newClient(cfg.Endpoint, cfg.APIKey, cfg.Debug)
// ... rest unchanged
}
```
- [ ] **Step 2: Run tests**
Run: `go test ./internal/provider/rainyun/...`
Expected: PASS
- [ ] **Step 3: Commit**
```bash
git add internal/provider/rainyun/rainyun.go
git commit -m "feat: pass Debug flag from config to client"
```
---
### Task 4: Wire --verbose alias and runtime info in root
**Files:**
- Modify: `cmd/cli/root.go`
**Interfaces:**
- Consumes: `flagDebug` (existing, now wired)
- Consumes: `ProviderConfig.Debug` (from Task 1)
- [ ] **Step 1: Add --verbose alias**
In `init()`, add after the `--debug` flag registration:
```go
rootCmd.PersistentFlags().BoolVarP(&flagDebug, "verbose", "v", false, "Enable verbose/debug output")
```
Note: cobra doesn't support two flags binding to the same var directly. Instead, use `MarkHidden` or just register `--verbose` as the primary and `--debug` as hidden alias. Simplest approach:
```go
rootCmd.PersistentFlags().BoolVarP(&flagDebug, "verbose", "v", false, "Enable verbose/debug output (same as --debug)")
rootCmd.PersistentFlags().BoolVar(&flagDebug, "debug", false, "Enable debug output (same as --verbose)")
```
- [ ] **Step 2: Wire flagDebug into ProviderConfig**
In `PersistentPreRunE`, after creating `pc`:
```go
prov, err = provider.New(providerName, provider.ProviderConfig{
APIKey: pc.APIKey,
AccessKeyID: pc.AccessKeyID,
AccessKeySecret: pc.AccessKeySecret,
Endpoint: pc.Endpoint,
Debug: flagDebug,
})
```
- [ ] **Step 3: Print runtime info when debug**
At the end of `PersistentPreRunE`, after creating `out`:
```go
if flagDebug {
fmt.Fprintf(os.Stderr, "[debug] provider: %s\n", providerName)
fmt.Fprintf(os.Stderr, "[debug] config: %s\n", cfgPath)
fmt.Fprintf(os.Stderr, "[debug] output: %s\n", outputFormat)
}
```
- [ ] **Step 4: Run full test suite**
Run: `go test ./...`
Expected: PASS
- [ ] **Step 5: Commit**
```bash
git add cmd/cli/root.go
git commit -m "feat: wire --verbose/--debug flags with runtime info"
```
---
### Task 5: Manual verification
- [ ] **Step 1: Build and test verbose output**
```bash
go build -o ry ./cmd/cli/
./ry --verbose server list 2>&1 | head -20
```
Expected stderr output:
```
[debug] provider: rainyun
[debug] config: /path/to/config.json
[debug] output: table
[debug] > GET /product/rcs/?Page=1&PageSize=999&PerPage=999
[debug] < 200 OK 123ms
[debug] {"code":200,"data":{...}}
```
- [ ] **Step 2: Verify --debug works too**
```bash
./ry --debug server list 2>&1 | head -5
```
Expected: same `[debug]` output
- [ ] **Step 3: Verify no debug output without flag**
```bash
./ry server list 2>/dev/null
```
Expected: clean table output, no debug lines
@@ -0,0 +1,70 @@
# Verbose/Debug Mode Design
**Date:** 2026-07-28
**Status:** Approved
## Goal
Wire up the existing `--debug` flag (currently unused) and add `--verbose` as an alias. When enabled, print detailed runtime and HTTP request information to stderr.
## Scope
4 files modified, 0 new files.
## Changes
### 1. `internal/provider/provider.go` — ProviderConfig
Add `Debug bool` field to `ProviderConfig`:
```go
type ProviderConfig struct {
APIKey string
AccessKeyID string
AccessKeySecret string
Endpoint string
Debug bool
}
```
### 2. `cmd/cli/root.go` — Flag + Runtime Info
- Register `--verbose` as alias for `--debug` (both set `flagDebug`)
- In `PersistentPreRunE`, when debug is on, print to stderr:
- Provider name
- Config file path
- Output format
### 3. `internal/provider/rainyun/client.go` — HTTP Logging
- Add `debug bool` field to `client` struct
- Update `newClient(endpoint, apiKey string, debug bool)` signature
- In `do()`, when `c.debug` is true, print to `os.Stderr`:
- Request: `> METHOD /path` + request body (if any)
- Response: `< StatusCode StatusText Duration` + response body
### 4. `internal/provider/rainyun/rainyun.go` — Pass Debug
- `New()` passes `cfg.Debug` to `newClient()`
## Output Format
All debug output goes to **stderr** to avoid polluting stdout data pipeline.
```
[debug] provider: rainyun
[debug] config: /Users/user/.config/ry/config.json
[debug] output: table
[debug] > GET /api/v2/servers
[debug] < 200 OK 123ms
[debug] {"code":200,"data":[...]}
```
## Files
| File | Change |
|------|--------|
| `internal/provider/provider.go` | Add `Debug` to `ProviderConfig` |
| `cmd/cli/root.go` | Wire `--verbose`, print runtime info |
| `internal/provider/rainyun/client.go` | HTTP request/response logging |
| `internal/provider/rainyun/rainyun.go` | Pass `cfg.Debug` to client |
+1
View File
@@ -11,6 +11,7 @@ type ProviderConfig struct {
AccessKeyID string
AccessKeySecret string
Endpoint string
Debug bool
}
type Provider interface {
+19 -1
View File
@@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"time"
)
@@ -17,9 +18,10 @@ type client struct {
httpClient *http.Client
baseURL string
apiKey string
debug bool
}
func newClient(endpoint, apiKey string) *client {
func newClient(endpoint, apiKey string, debug bool) *client {
if endpoint == "" {
endpoint = defaultEndpoint
}
@@ -27,6 +29,7 @@ func newClient(endpoint, apiKey string) *client {
httpClient: &http.Client{Timeout: 30 * time.Second},
baseURL: endpoint,
apiKey: apiKey,
debug: debug,
}
}
@@ -85,14 +88,23 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
}
var bodyReader io.Reader
var bodyBytes []byte
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshal request: %w", err)
}
bodyBytes = b
bodyReader = bytes.NewReader(b)
}
if c.debug {
fmt.Fprintf(os.Stderr, "[debug] > %s %s\n", method, u)
if bodyBytes != nil {
fmt.Fprintf(os.Stderr, "[debug] %s\n", bodyBytes)
}
}
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
if err != nil {
return fmt.Errorf("create request: %w", err)
@@ -101,6 +113,7 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
start := time.Now()
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
@@ -112,6 +125,11 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
return fmt.Errorf("read response: %w", err)
}
if c.debug {
fmt.Fprintf(os.Stderr, "[debug] < %d %s %s\n", resp.StatusCode, http.StatusText(resp.StatusCode), time.Since(start).Round(time.Millisecond))
fmt.Fprintf(os.Stderr, "[debug] %s\n", respBody)
}
var apiResp apiResponse
if err := json.Unmarshal(respBody, &apiResp); err == nil {
// Rainyun uses code:200 and code:0 both as success indicators
+2 -2
View File
@@ -16,7 +16,7 @@ func TestClientAuthHeader(t *testing.T) {
}))
defer srv.Close()
c := newClient(srv.URL, "sk-test")
c := newClient(srv.URL, "sk-test", false)
var result struct {
ID int `json:"id"`
Name string `json:"name"`
@@ -40,7 +40,7 @@ func TestClientAPIError(t *testing.T) {
}))
defer srv.Close()
c := newClient(srv.URL, "sk-test")
c := newClient(srv.URL, "sk-test", false)
var result any
err := c.get(context.Background(), "/error", &result)
if err == nil {
+1 -1
View File
@@ -21,7 +21,7 @@ type Rainyun struct {
// New creates a Rainyun provider from config.
func New(cfg provider.ProviderConfig) (provider.Provider, error) {
c := newClient(cfg.Endpoint, cfg.APIKey)
c := newClient(cfg.Endpoint, cfg.APIKey, cfg.Debug)
r := &Rainyun{client: c}
r.server = &ServerService{client: c}
r.domain = &DomainService{client: c}
+16 -14
View File
@@ -16,15 +16,15 @@ type ServerService struct {
// rainyunServer mirrors the Rainyun RCS API JSON fields (PascalCase).
type rainyunServer struct {
ID int `json:"ID"`
Name string `json:"name"`
HostName string `json:"HostName"`
Status string `json:"status"`
IP string `json:"ip"`
VCPU int `json:"vcpu"`
MainIPv4 string `json:"MainIPv4"`
CPU int `json:"CPU"`
Memory int `json:"memory"`
Disk int `json:"disk"`
OS string `json:"os"`
Region string `json:"zone_name"`
ExpireDate int64 `json:"ExpDate"` // Unix timestamp
OsName string `json:"OsName"`
Zone string `json:"Zone"`
ExpireDate int64 `json:"ExpDate"`
}
func (s *ServerService) List(ctx context.Context) ([]model.Server, error) {
@@ -40,12 +40,14 @@ func (s *ServerService) List(ctx context.Context) ([]model.Server, error) {
}
func (s *ServerService) Get(ctx context.Context, id string) (*model.Server, error) {
var raw rainyunServer
var resp struct {
Data rainyunServer `json:"Data"`
}
path := fmt.Sprintf("/product/rcs/%s/", id)
if err := s.client.get(ctx, path, &raw); err != nil {
if err := s.client.get(ctx, path, &resp); err != nil {
return nil, err
}
sv := toServer(raw)
sv := toServer(resp.Data)
return &sv, nil
}
@@ -90,14 +92,14 @@ func toServer(r rainyunServer) model.Server {
expire := time.Unix(r.ExpireDate, 0)
return model.Server{
ID: fmt.Sprintf("%d", r.ID),
Name: r.Name,
Name: r.HostName,
Status: r.Status,
IP: r.IP,
CPU: r.VCPU,
IP: r.MainIPv4,
CPU: r.CPU,
Memory: r.Memory,
Disk: r.Disk,
OS: r.OS,
Region: r.Region,
OS: r.OsName,
Region: r.Zone,
ExpireAt: expire,
Raw: r,
}
+13 -12
View File
@@ -18,15 +18,16 @@ func TestServerList(t *testing.T) {
"data": map[string]any{
"Records": []map[string]any{
{
"ID": 12001,
"name": "web-server",
"status": "running",
"ip": "1.2.3.4",
"vcpu": 2,
"memory": 4096,
"disk": 50,
"os": "Ubuntu 22.04",
"ExpDate": 1756656000,
"ID": 12001,
"HostName": "web-server",
"status": "running",
"MainIPv4": "1.2.3.4",
"CPU": 2,
"memory": 4096,
"disk": 50,
"OsName": "Ubuntu 22.04",
"Zone": "cn-sy1",
"ExpDate": 1756656000,
},
},
"TotalRecords": 1,
@@ -36,7 +37,7 @@ func TestServerList(t *testing.T) {
}))
defer srv.Close()
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
svc := rain.Server()
servers, err := svc.List(context.Background())
if err != nil {
@@ -66,7 +67,7 @@ func TestServerStart(t *testing.T) {
}))
defer srv.Close()
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
err := rain.Server().Start(context.Background(), "12001")
if err != nil {
t.Fatalf("Start() error: %v", err)
@@ -83,7 +84,7 @@ func TestServerVNC(t *testing.T) {
}))
defer srv.Close()
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
url, err := rain.Server().GetVNC(context.Background(), "12001")
if err != nil {
t.Fatalf("GetVNC() error: %v", err)