Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit99a751f

Browse files
committed
Add new clog.ErrGroup
1 parentd32d196 commit99a751f

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

‎internal/clog/doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// package clog provides rich error types and logging helpers for coder-cli.
2+
//
3+
// To preserve the modularity of the code, clog encourages returning error types rather than
4+
// logging them and failing with os.Exit as they happen.
5+
// Error, Fatal, and Warn allow downstream functions to return errors with rich formatting information
6+
// while preserving the original, single-line error chain.
7+
package clog

‎internal/clog/errgroup.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package clog
2+
3+
import (
4+
"fmt"
5+
"sync/atomic"
6+
7+
"golang.org/x/sync/errgroup"
8+
"golang.org/x/xerrors"
9+
)
10+
11+
// ErrGroup wraps the /x/sync/errgroup.(Group) and adds clog logging and rich error propagation.
12+
//
13+
// Take for example, a case in which we are concurrently stopping a slice of environments.
14+
// In this case, we want to log errors as they happen, not pass them through the callstack as errors.
15+
// When the operations complete, we want to log how many, if any, failed.
16+
typeErrGroupinterface {
17+
Go(ffunc()error)
18+
Wait()error
19+
}
20+
21+
typegroupstruct {
22+
egroup errgroup.Group
23+
failuresint32
24+
}
25+
26+
// NewErrGroup gives an error group with logging and error propagation handled automatically.
27+
funcNewErrGroup()ErrGroup {
28+
return&group{
29+
egroup: errgroup.Group{},
30+
failures:0,
31+
}
32+
}
33+
34+
func (g*group)Go(ffunc()error) {
35+
g.egroup.Go(func()error {
36+
iferr:=f();err!=nil {
37+
atomic.AddInt32(&g.failures,1)
38+
Log(err)
39+
40+
// this error does not matter because we discard it in Wait.
41+
returnxerrors.New("")
42+
}
43+
returnnil
44+
})
45+
}
46+
47+
func (g*group)Wait()error {
48+
_=g.egroup.Wait()// ignore this error because we are already tracking failures manually
49+
ifg.failures==0 {
50+
returnnil
51+
}
52+
failureWord:="failure"
53+
ifg.failures>1 {
54+
failureWord+="s"
55+
}
56+
returnFatal(fmt.Sprintf("%d %s emitted",g.failures,failureWord))
57+
}

‎internal/cmd/envs.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"encoding/json"
55
"fmt"
66
"os"
7-
"sync/atomic"
87

98
"cdr.dev/coder-cli/coder-sdk"
109
"cdr.dev/coder-cli/internal/clog"
1110
"cdr.dev/coder-cli/internal/x/xtabwriter"
1211
"github.com/spf13/cobra"
13-
"golang.org/x/sync/errgroup"
1412
"golang.org/x/xerrors"
1513
)
1614

@@ -102,34 +100,28 @@ coder envs --user charlie@coder.com ls -o json \
102100
returnxerrors.Errorf("new client: %w",err)
103101
}
104102

105-
varegroup errgroup.Group
106-
varfailsint32
103+
egroup:=clog.NewErrGroup()
107104
for_,envName:=rangeargs {
108105
envName:=envName
109106
egroup.Go(func()error {
110107
env,err:=findEnv(cmd.Context(),client,envName,*user)
111108
iferr!=nil {
112-
atomic.AddInt32(&fails,1)
113-
clog.Log(err)
114-
returnxerrors.Errorf("find env by name: %w",err)
109+
returnerr
115110
}
116111

117112
iferr=client.StopEnvironment(cmd.Context(),env.ID);err!=nil {
118-
atomic.AddInt32(&fails,1)
119-
err=clog.Fatal(fmt.Sprintf("stop environment %q",env.Name),
113+
returnclog.Fatal(fmt.Sprintf("stop environment %q",env.Name),
120114
clog.Causef(err.Error()),clog.BlankLine,
121115
clog.Hintf("current environment status is %q",env.LatestStat.ContainerStatus),
122116
)
123-
clog.Log(err)
124-
returnerr
125117
}
126118
clog.LogSuccess(fmt.Sprintf("successfully stopped environment %q",envName))
127119
returnnil
128120
})
129121
}
130122

131-
iferr=egroup.Wait();err!=nil {
132-
returnclog.Fatal(fmt.Sprintf("%d failure(s) emitted",fails))
123+
iferr:=egroup.Wait();err!=nil {
124+
returnerr
133125
}
134126
returnnil
135127
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp