GeezerVison is showing github.com — feat(platform): P3 Part 0 — C2 ux/platform exposure (#8) · Episkey-G/ucloud-cli@5209add
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: base/client.go +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -204,6 +204,15 @@ func attachHandlers(sc sdk.ServiceClient, credConfig *CredentialConfig, ac *AggC | 204 | 204 | err := req.SetProjectId(PickResourceID(req.GetProjectId())) | 205 | 205 | return req, err | 206 | 206 | }) | | 207 | + // Platform request logging: every API request is logged uniformly at the SDK | | 208 | + // layer (replaces per-command hand-rolled logging; products no longer build | | 209 | + // "api:..." lines with ToQueryMap). logToFile writes to local cli.log only | | 210 | + // (NO DAS upload) and skips completion (COMP_LINE) — see batch-1 plan Part 0 | | 211 | + // Task 0.2 (decision A: keep request logs local, don't inflate telemetry). | | 212 | + sc.AddRequestHandler(func(c *sdk.Client, req request.Common) (request.Common, error) { | | 213 | + logToFile(requestLogLine(req)) | | 214 | + return req, nil | | 215 | + }) | 207 | 216 | sc.AddHttpRequestHandler(newCredHeaderInjector(credConfig)) | 208 | 217 | sc.AddHttpResponseHandler(newOAuthRetryHandler(credConfig, ac)) | 209 | 218 | } | |
Collapse file Copy file name to clipboardExpand all lines: base/log.go +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,6 +97,24 @@ func GetLogFilePath() string { | 97 | 97 | return common.GetHomePath() + fmt.Sprintf("/%s/cli.log", ConfigPath) | 98 | 98 | } | 99 | 99 |
| | 100 | +// logToFile writes lines to the local cli.log only — NO DAS telemetry upload — | | 101 | +// with the same redaction and COMP_LINE skip as LogInfo. Used by the platform | | 102 | +// request-logging handler so logging every API request does not inflate | | 103 | +// telemetry traffic for users who opted into log upload (see batch-1 plan | | 104 | +// Part 0 Task 0.2, decision A). | | 105 | +func logToFile(logs ...string) { | | 106 | + if _, ok := os.LookupEnv("COMP_LINE"); ok { | | 107 | + return | | 108 | + } | | 109 | + logs = redactLogLines(logs) | | 110 | + mu.Lock() | | 111 | + defer mu.Unlock() | | 112 | + goID := curGoroutineID() | | 113 | + for _, line := range logs { | | 114 | + logger.WithField("goroutine_id", goID).Info(line) | | 115 | + } | | 116 | +} | | 117 | + | 100 | 118 | // LogInfo 记录日志 | 101 | 119 | func LogInfo(logs ...string) { | 102 | 120 | _, ok := os.LookupEnv("COMP_LINE") | @@ -255,6 +273,15 @@ func ToQueryMap(req request.Common) map[string]string { | 255 | 273 | return reqMap | 256 | 274 | } | 257 | 275 |
| | 276 | +// requestLogLine formats an API request for the platform request-logging | | 277 | +// handler: "api: <Action>, request: <query map>" (Password already redacted by | | 278 | +// ToQueryMap). This replaces the per-command hand-rolled request logging that | | 279 | +// products used to build with ToQueryMap — every request is now logged | | 280 | +// uniformly at the SDK handler layer (see batch-1 plan Part 0 Task 0.2). | | 281 | +func requestLogLine(req request.Common) string { | | 282 | + return fmt.Sprintf("api: %s, request: %v", req.GetAction(), ToQueryMap(req)) | | 283 | +} | | 284 | + | 258 | 285 | // Tracer upload log to server if allowed | 259 | 286 | type Tracer struct { | 260 | 287 | DasUrl string | |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package base | | 2 | + | | 3 | +import ( | | 4 | + "bytes" | | 5 | + "os" | | 6 | + "strings" | | 7 | + "testing" | | 8 | + | | 9 | + log "github.com/sirupsen/logrus" | | 10 | +) | | 11 | + | | 12 | +// logToFile writes to cli.log only — never to DAS telemetry — so the per-request | | 13 | +// logging handler does not upload every API request to the server. | | 14 | +func TestLogToFileWritesToLoggerOnly(t *testing.T) { | | 15 | + // logToFile skips when COMP_LINE is present (completion); ensure it's absent. | | 16 | + if v, ok := os.LookupEnv("COMP_LINE"); ok { | | 17 | + os.Unsetenv("COMP_LINE") | | 18 | + defer os.Setenv("COMP_LINE", v) | | 19 | + } | | 20 | + | | 21 | + var buf bytes.Buffer | | 22 | + old := logger | | 23 | + logger = log.New() | | 24 | + logger.SetOutput(&buf) | | 25 | + defer func() { logger = old }() | | 26 | + | | 27 | + logToFile("api: DescribeUHostInstance, request: map[Region:cn-bj2]") | | 28 | + | | 29 | + if !strings.Contains(buf.String(), "DescribeUHostInstance") { | | 30 | + t.Fatalf("logToFile did not write to the cli.log logger: %q", buf.String()) | | 31 | + } | | 32 | +} |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package base | | 2 | + | | 3 | +import ( | | 4 | + "strings" | | 5 | + "testing" | | 6 | + | | 7 | + "github.com/ucloud/ucloud-sdk-go/ucloud/request" | | 8 | +) | | 9 | + | | 10 | +func TestRequestLogLine(t *testing.T) { | | 11 | + req := &request.CommonBase{} | | 12 | + req.SetAction("DescribeUHostInstance") | | 13 | + req.SetRegion("cn-bj2") | | 14 | + | | 15 | + line := requestLogLine(req) | | 16 | + | | 17 | + if !strings.HasPrefix(line, "api: DescribeUHostInstance") { | | 18 | + t.Fatalf("want 'api: DescribeUHostInstance...' prefix, got %q", line) | | 19 | + } | | 20 | + if !strings.Contains(line, "request:") { | | 21 | + t.Fatalf("want 'request:' marker in %q", line) | | 22 | + } | | 23 | + if !strings.Contains(line, "cn-bj2") { | | 24 | + t.Fatalf("want region in serialized request, got %q", line) | | 25 | + } | | 26 | +} |
Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package base | | 2 | + | | 3 | +import ( | | 4 | + "bytes" | | 5 | + "strings" | | 6 | + "testing" | | 7 | + "time" | | 8 | + | | 9 | + "github.com/ucloud/ucloud-sdk-go/ucloud/request" | | 10 | + | | 11 | + "github.com/ucloud/ucloud-cli/ux" | | 12 | +) | | 13 | + | | 14 | +// Sspoll (concurrent poller) must suppress spinner animation on a non-TTY | | 15 | +// writer, exactly like Spoll, emitting a single terminal-state line instead. | | 16 | +func TestSspollNonTTY_Done(t *testing.T) { | | 17 | + describeFunc := func(resourceID string, _ *request.CommonBase) (interface{}, error) { | | 18 | + return &instDone{State: "DONE"}, nil | | 19 | + } | | 20 | + | | 21 | + buf := &bytes.Buffer{} | | 22 | + p := NewSpoller(describeFunc, buf) | | 23 | + p.Timeout = 30 * time.Second | | 24 | + | | 25 | + ret := p.Sspoll("res-001", "creating", []string{"DONE"}, ux.NewBlock(), &request.CommonBase{}) | | 26 | + | | 27 | + if ret == nil || !ret.Done { | | 28 | + t.Fatalf("Sspoll non-TTY: want Done=true, got %+v", ret) | | 29 | + } | | 30 | + out := buf.String() | | 31 | + if !strings.Contains(out, "creating...done\n") { | | 32 | + t.Errorf("expected 'creating...done\\n' in output, got: %q", out) | | 33 | + } | | 34 | + for _, r := range []rune{'⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'} { | | 35 | + if strings.ContainsRune(out, r) { | | 36 | + t.Errorf("spinner frame %q leaked into non-TTY Sspoll output: %q", r, out) | | 37 | + } | | 38 | + } | | 39 | +} |
Collapse file Copy file name to clipboardExpand all lines: base/util.go +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -342,6 +342,18 @@ func (p *Poller) Sspoll(resourceID, pollText string, targetStates []string, bloc | 342 | 342 | pollRetChan <- ret | 343 | 343 | }() | 344 | 344 |
| | 345 | + if !ui.IsTTY(p.Out) { | | 346 | + // non-TTY (piped/redirected): no spinner animation, single terminal-state | | 347 | + // line — mirrors Spoll's suppression so machine output stays clean. | | 348 | + ret := <-pollRetChan | | 349 | + if ret.Timeout { | | 350 | + fmt.Fprintf(p.Out, "%s...timeout\n", pollText) | | 351 | + } else { | | 352 | + fmt.Fprintf(p.Out, "%s...done\n", pollText) | | 353 | + } | | 354 | + return &ret | | 355 | + } | | 356 | + | 345 | 357 | spin := ux.NewDotSpin(p.Out, pollText) | 346 | 358 | block.SetSpin(spin) | 347 | 359 |
| |
Collapse file Copy file name to clipboardExpand all lines: cmd/root.go +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -221,6 +221,7 @@ func buildContext() *cli.Context { | 221 | 221 | RegionList: getRegionList, | 222 | 222 | ZoneList: getZoneList, | 223 | 223 | ProjectList: getProjectList, | | 224 | + AllRegions: getAllRegions, | 224 | 225 | }) | 225 | 226 | } | 226 | 227 |
| |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package cli_test | | 2 | + | | 3 | +import ( | | 4 | + "errors" | | 5 | + "testing" | | 6 | + | | 7 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 8 | +) | | 9 | + | | 10 | +func TestAllRegionsForwardsProviderAndError(t *testing.T) { | | 11 | + ctx := cli.NewContext(cli.Deps{ | | 12 | + AllRegions: func() ([]string, error) { return []string{"cn-bj2", "us-ca"}, nil }, | | 13 | + }) | | 14 | + regions, err := ctx.AllRegions() | | 15 | + if err != nil || len(regions) != 2 { | | 16 | + t.Fatalf("AllRegions = %v, %v; want 2 regions, nil err", regions, err) | | 17 | + } | | 18 | + | | 19 | + // error must propagate (so --all-region reports a region-fetch failure | | 20 | + // instead of silently listing nothing). | | 21 | + wantErr := errors.New("fetch region failed") | | 22 | + ctxErr := cli.NewContext(cli.Deps{ | | 23 | + AllRegions: func() ([]string, error) { return nil, wantErr }, | | 24 | + }) | | 25 | + if _, err := ctxErr.AllRegions(); !errors.Is(err, wantErr) { | | 26 | + t.Fatalf("AllRegions error = %v, want %v", err, wantErr) | | 27 | + } | | 28 | + | | 29 | + // nil-safe when no provider injected. | | 30 | + empty := cli.NewContext(cli.Deps{}) | | 31 | + if r, err := empty.AllRegions(); r != nil || err != nil { | | 32 | + t.Fatalf("nil provider: want (nil,nil), got (%v,%v)", r, err) | | 33 | + } | | 34 | +} |
Collapse file Copy file name to clipboardExpand all lines: pkg/cli/context.go +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,9 @@ type Context struct { | 35 | 35 | regionList func() []string | 36 | 36 | zoneList func(region string) []string | 37 | 37 | projectList func() []string | | 38 | + // allRegions is the runtime all-region lister (returns an error, unlike the | | 39 | + // completion providers) for non-standard flags like uhost --all-region. | | 40 | + allRegions func() ([]string, error) | 38 | 41 | } | 39 | 42 |
| 40 | 43 | // Deps carries constructor arguments for NewContext. | @@ -48,6 +51,7 @@ type Deps struct { | 48 | 51 | RegionList func() []string | 49 | 52 | ZoneList func(region string) []string | 50 | 53 | ProjectList func() []string | | 54 | + AllRegions func() ([]string, error) | 51 | 55 | } | 52 | 56 |
| 53 | 57 | // NewContext constructs a Context from the provided Deps. | @@ -61,6 +65,7 @@ func NewContext(d Deps) *Context { | 61 | 65 | regionList: d.RegionList, | 62 | 66 | zoneList: d.ZoneList, | 63 | 67 | projectList: d.ProjectList, | | 68 | + allRegions: d.AllRegions, | 64 | 69 | } | 65 | 70 | } | 66 | 71 |
| |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package cli_test | | 2 | + | | 3 | +import ( | | 4 | + "testing" | | 5 | + | | 6 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 7 | +) | | 8 | + | | 9 | +func TestRegionZoneProjectListGetters(t *testing.T) { | | 10 | + ctx := cli.NewContext(cli.Deps{ | | 11 | + RegionList: func() []string { return []string{"cn-bj2"} }, | | 12 | + ZoneList: func(r string) []string { return []string{r + "-01"} }, | | 13 | + ProjectList: func() []string { return []string{"org-x"} }, | | 14 | + }) | | 15 | + | | 16 | + if got := ctx.RegionList(); len(got) != 1 || got[0] != "cn-bj2" { | | 17 | + t.Fatalf("RegionList = %v", got) | | 18 | + } | | 19 | + if got := ctx.ZoneList("cn-bj2"); len(got) != 1 || got[0] != "cn-bj2-01" { | | 20 | + t.Fatalf("ZoneList = %v", got) | | 21 | + } | | 22 | + if got := ctx.ProjectList(); len(got) != 1 || got[0] != "org-x" { | | 23 | + t.Fatalf("ProjectList = %v", got) | | 24 | + } | | 25 | + | | 26 | + // nil-safe when providers absent (non-standard-flag getters must not panic). | | 27 | + empty := cli.NewContext(cli.Deps{}) | | 28 | + if empty.RegionList() != nil || empty.ZoneList("x") != nil || empty.ProjectList() != nil { | | 29 | + t.Fatal("getters must be nil-safe when providers absent") | | 30 | + } | | 31 | +} |
|