GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(platform): P3 Part 0 — C2 ux/platform exposure (#8) · Episkey-G/ucloud-cli@5209add

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • base
    • client.go
    • log.go
    • logtofile_test.go
    • requestlog_test.go
    • sspoll_test.go
    • util.go
  • cmd
    • root.go
  • pkg
    • cli
      • allregions_test.go
      • context.go
      • context_getters_test.go
      • context_log_test.go
      • forward.go
      • progress.go
      • progress_test.go
      • result.go
      • result_test.go
    • command
      • bind.go
      • bindgroup_test.go
  • ux
    • document.go
    • document_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎base/client.go‎

Copy file name to clipboardExpand all lines: base/client.go
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ func attachHandlers(sc sdk.ServiceClient, credConfig *CredentialConfig, ac *AggC
204204
err := req.SetProjectId(PickResourceID(req.GetProjectId()))
205205
return req, err
206206
})
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+
})
207216
sc.AddHttpRequestHandler(newCredHeaderInjector(credConfig))
208217
sc.AddHttpResponseHandler(newOAuthRetryHandler(credConfig, ac))
209218
}
Collapse file

‎base/log.go‎

Copy file name to clipboardExpand all lines: base/log.go
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ func GetLogFilePath() string {
9797
return common.GetHomePath() + fmt.Sprintf("/%s/cli.log", ConfigPath)
9898
}
9999

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+
100118
// LogInfo 记录日志
101119
func LogInfo(logs ...string) {
102120
_, ok := os.LookupEnv("COMP_LINE")
@@ -255,6 +273,15 @@ func ToQueryMap(req request.Common) map[string]string {
255273
return reqMap
256274
}
257275

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+
258285
// Tracer upload log to server if allowed
259286
type Tracer struct {
260287
DasUrl string
Collapse file

‎base/logtofile_test.go‎

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

‎base/requestlog_test.go‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

‎base/sspoll_test.go‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

‎base/util.go‎

Copy file name to clipboardExpand all lines: base/util.go
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ func (p *Poller) Sspoll(resourceID, pollText string, targetStates []string, bloc
342342
pollRetChan <- ret
343343
}()
344344

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+
345357
spin := ux.NewDotSpin(p.Out, pollText)
346358
block.SetSpin(spin)
347359

Collapse file

‎cmd/root.go‎

Copy file name to clipboardExpand all lines: cmd/root.go
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ func buildContext() *cli.Context {
221221
RegionList: getRegionList,
222222
ZoneList: getZoneList,
223223
ProjectList: getProjectList,
224+
AllRegions: getAllRegions,
224225
})
225226
}
226227

Collapse file

‎pkg/cli/allregions_test.go‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

‎pkg/cli/context.go‎

Copy file name to clipboardExpand all lines: pkg/cli/context.go
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type Context struct {
3535
regionList func() []string
3636
zoneList func(region string) []string
3737
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)
3841
}
3942

4043
// Deps carries constructor arguments for NewContext.
@@ -48,6 +51,7 @@ type Deps struct {
4851
RegionList func() []string
4952
ZoneList func(region string) []string
5053
ProjectList func() []string
54+
AllRegions func() ([]string, error)
5155
}
5256

5357
// NewContext constructs a Context from the provided Deps.
@@ -61,6 +65,7 @@ func NewContext(d Deps) *Context {
6165
regionList: d.RegionList,
6266
zoneList: d.ZoneList,
6367
projectList: d.ProjectList,
68+
allRegions: d.AllRegions,
6469
}
6570
}
6671

Collapse file

‎pkg/cli/context_getters_test.go‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.