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

Commitfec0729

Browse files
committed
Fix all erorr handling to use errors
1 parenta64797a commitfec0729

File tree

14 files changed

+220
-125
lines changed

14 files changed

+220
-125
lines changed

‎ci/integration/users_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestUsers(t *testing.T) {
1717

1818
c,err:=tcli.NewContainerRunner(ctx,&tcli.ContainerConfig{
1919
Image:"codercom/enterprise-dev",
20-
Name:"coder-cli-tests",
20+
Name:"users-cli-tests",
2121
BindMounts:map[string]string{
2222
binpath:"/bin/coder",
2323
},

‎cmd/coder/auth.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55

66
"cdr.dev/coder-cli/internal/config"
77
"cdr.dev/coder-cli/internal/entclient"
8+
9+
"go.coder.com/flog"
810
)
911

12+
// requireAuth exits the process with a nonzero exit code if the user is not authenticated to make requests
1013
funcrequireAuth()*entclient.Client {
1114
sessionToken,err:=config.Session.Read()
1215
requireSuccess(err,"read session: %v (did you run coder login?)",err)
@@ -22,3 +25,10 @@ func requireAuth() *entclient.Client {
2225
Token:sessionToken,
2326
}
2427
}
28+
29+
// requireSuccess prints the given message and format args as a fatal error if err != nil
30+
funcrequireSuccess(errerror,msgstring,args...interface{}) {
31+
iferr!=nil {
32+
flog.Fatal(msg,args...)
33+
}
34+
}

‎cmd/coder/ceapi.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"golang.org/x/xerrors"
5+
46
"go.coder.com/flog"
57

68
"cdr.dev/coder-cli/internal/entclient"
@@ -25,43 +27,50 @@ outer:
2527
}
2628

2729
// getEnvs returns all environments for the user.
28-
funcgetEnvs(client*entclient.Client) []entclient.Environment {
30+
funcgetEnvs(client*entclient.Client)([]entclient.Environment,error) {
2931
me,err:=client.Me()
30-
requireSuccess(err,"get self: %+v",err)
32+
iferr!=nil {
33+
returnnil,xerrors.Errorf("get self: %+v",err)
34+
}
3135

3236
orgs,err:=client.Orgs()
33-
requireSuccess(err,"get orgs: %+v",err)
37+
iferr!=nil {
38+
returnnil,xerrors.Errorf("get orgs: %+v",err)
39+
}
3440

3541
orgs=userOrgs(me,orgs)
3642

3743
varallEnvs []entclient.Environment
3844

3945
for_,org:=rangeorgs {
4046
envs,err:=client.Envs(me,org)
41-
requireSuccess(err,"get envs for %v: %+v",org.Name,err)
47+
iferr!=nil {
48+
returnnil,xerrors.Errorf("get envs for %v: %+v",org.Name,err)
49+
}
4250

4351
for_,env:=rangeenvs {
4452
allEnvs=append(allEnvs,env)
4553
}
4654
}
47-
48-
returnallEnvs
55+
returnallEnvs,nil
4956
}
5057

5158
// findEnv returns a single environment by name (if it exists.)
52-
funcfindEnv(client*entclient.Client,namestring) entclient.Environment {
53-
envs:=getEnvs(client)
59+
funcfindEnv(client*entclient.Client,namestring) (*entclient.Environment,error) {
60+
envs,err:=getEnvs(client)
61+
iferr!=nil {
62+
returnnil,xerrors.Errorf("failed to get environments: %w",err)
63+
}
5464

5565
varfound []string
5666

5767
for_,env:=rangeenvs {
5868
found=append(found,env.Name)
5969
ifenv.Name==name {
60-
returnenv
70+
return&env,nil
6171
}
6272
}
63-
64-
flog.Info("found %q",found)
65-
flog.Fatal("environment %q not found",name)
66-
panic("unreachable")
73+
flog.Error("found %q",found)
74+
flog.Error("%q not found",name)
75+
returnnil,xerrors.New("environment not found")
6776
}

‎cmd/coder/configssh.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
"cdr.dev/coder-cli/internal/config"
1515
"cdr.dev/coder-cli/internal/entclient"
1616
"github.com/urfave/cli"
17-
18-
"go.coder.com/flog"
17+
"golang.org/x/xerrors"
1918
)
2019

2120
funcmakeConfigSSHCmd() cli.Command {
@@ -46,7 +45,7 @@ func makeConfigSSHCmd() cli.Command {
4645
}
4746
}
4847

49-
funcconfigSSH(filepath*string,remove*bool)func(c*cli.Context) {
48+
funcconfigSSH(filepath*string,remove*bool)func(c*cli.Context)error{
5049
startToken:="# ------------START-CODER-ENTERPRISE-----------"
5150
startMessage:=`# The following has been auto-generated by "coder config-ssh"
5251
# to make accessing your Coder Enterprise environments easier.
@@ -58,7 +57,7 @@ func configSSH(filepath *string, remove *bool) func(c *cli.Context) {
5857
# You should not hand-edit this section, unless you are deleting it.`
5958
endToken:="# ------------END-CODER-ENTERPRISE------------"
6059

61-
returnfunc(c*cli.Context) {
60+
returnfunc(c*cli.Context)error{
6261
ctx,cancel:=context.WithCancel(context.Background())
6362
defercancel()
6463

@@ -67,45 +66,48 @@ func configSSH(filepath *string, remove *bool) func(c *cli.Context) {
6766
// SSH configs are not always already there.
6867
currentConfig=""
6968
}elseiferr!=nil {
70-
flog.Fatal("failed to read ssh config file %q: %v",filepath,err)
69+
returnxerrors.Errorf("failed to read ssh config file %q: %w",filepath,err)
7170
}
7271

7372
startIndex:=strings.Index(currentConfig,startToken)
7473
endIndex:=strings.Index(currentConfig,endToken)
7574

7675
if*remove {
7776
ifstartIndex==-1||endIndex==-1 {
78-
flog.Fatal("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist")
77+
returnxerrors.Errorf("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist")
7978
}
8079
currentConfig=currentConfig[:startIndex-1]+currentConfig[endIndex+len(endToken)+1:]
8180

8281
err=writeStr(*filepath,currentConfig)
8382
iferr!=nil {
84-
flog.Fatal("failed to write to ssh config file %q: %v",*filepath,err)
83+
returnxerrors.Errorf("failed to write to ssh config file %q: %v",*filepath,err)
8584
}
8685

87-
return
86+
returnnil
8887
}
8988

9089
entClient:=requireAuth()
9190

9291
sshAvailable:=isSSHAvailable(ctx)
9392
if!sshAvailable {
94-
flog.Fatal("SSH is disabled or not available for your Coder Enterprise deployment.")
93+
returnxerrors.New("SSH is disabled or not available for your Coder Enterprise deployment.")
9594
}
9695

9796
me,err:=entClient.Me()
9897
iferr!=nil {
99-
flog.Fatal("failed to fetch username: %v",err)
98+
returnxerrors.Errorf("failed to fetch username: %w",err)
10099
}
101100

102-
envs:=getEnvs(entClient)
101+
envs,err:=getEnvs(entClient)
102+
iferr!=nil {
103+
returnerr
104+
}
103105
iflen(envs)<1 {
104-
flog.Fatal("no environments found")
106+
returnxerrors.New("no environments found")
105107
}
106108
newConfig,err:=makeNewConfigs(me.Username,envs,startToken,startMessage,endToken)
107109
iferr!=nil {
108-
flog.Fatal("failed to make new ssh configurations: %v",err)
110+
returnxerrors.Errorf("failed to make new ssh configurations: %w",err)
109111
}
110112

111113
// if we find the old config, remove those chars from the string
@@ -115,17 +117,18 @@ func configSSH(filepath *string, remove *bool) func(c *cli.Context) {
115117

116118
err=writeStr(*filepath,currentConfig+newConfig)
117119
iferr!=nil {
118-
flog.Fatal("failed to write new configurations to ssh config file %q: %v",filepath,err)
120+
returnxerrors.Errorf("failed to write new configurations to ssh config file %q: %w",filepath,err)
119121
}
120122
err=writeSSHKey(ctx,entClient)
121123
iferr!=nil {
122-
flog.Fatal("failed to fetch and write ssh key: %v",err)
124+
returnxerrors.Errorf("failed to fetch and write ssh key: %w",err)
123125
}
124126

125127
fmt.Printf("An auto-generated ssh config was written to %q\n",*filepath)
126128
fmt.Printf("Your private ssh key was written to %q\n",privateKeyFilepath)
127129
fmt.Println("You should now be able to ssh into your environment")
128130
fmt.Printf("For example, try running\n\n\t$ ssh coder.%s\n\n",envs[0].Name)
131+
returnnil
129132
}
130133
}
131134

‎cmd/coder/envs.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66

77
"cdr.dev/coder-cli/internal/x/xtabwriter"
88
"github.com/urfave/cli"
9-
10-
"go.coder.com/flog"
9+
"golang.org/x/xerrors"
1110
)
1211

1312
funcmakeEnvsCommand() cli.Command {
@@ -23,22 +22,30 @@ func makeEnvsCommand() cli.Command {
2322
Usage:"list all environments owned by the active user",
2423
Description:"List all Coder environments owned by the active user.",
2524
ArgsUsage:"[...flags]>",
26-
Action:func(c*cli.Context) {
25+
Action:func(c*cli.Context)error{
2726
entClient:=requireAuth()
28-
envs:=getEnvs(entClient)
27+
envs,err:=getEnvs(entClient)
28+
iferr!=nil {
29+
returnerr
30+
}
2931

3032
switchoutputFmt {
3133
case"human":
3234
err:=xtabwriter.WriteTable(len(envs),func(iint)interface{} {
3335
returnenvs[i]
3436
})
35-
requireSuccess(err,"failed to write table: %v",err)
37+
iferr!=nil {
38+
returnxerrors.Errorf("failed to write table: %w",err)
39+
}
3640
case"json":
3741
err:=json.NewEncoder(os.Stdout).Encode(envs)
38-
requireSuccess(err,"failed to write json: %v",err)
42+
iferr!=nil {
43+
returnxerrors.Errorf("failed to write environments as JSON: %w",err)
44+
}
3945
default:
40-
flog.Fatal("unknown --output value %q",outputFmt)
46+
returnxerrors.Errorf("unknown --output value %q",outputFmt)
4147
}
48+
returnnil
4249
},
4350
Flags: []cli.Flag{
4451
cli.StringFlag{

‎cmd/coder/exit.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎cmd/coder/login.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"cdr.dev/coder-cli/internal/loginsrv"
1212
"github.com/pkg/browser"
1313
"github.com/urfave/cli"
14+
"golang.org/x/xerrors"
1415

1516
"go.coder.com/flog"
1617
)
@@ -24,20 +25,20 @@ func makeLoginCmd() cli.Command {
2425
}
2526
}
2627

27-
funclogin(c*cli.Context) {
28+
funclogin(c*cli.Context)error{
2829
rawURL:=c.Args().First()
2930
ifrawURL==""||!strings.HasPrefix(rawURL,"http") {
30-
flog.Fatal("invalid URL")
31+
returnxerrors.Errorf("invalid URL")
3132
}
3233

3334
u,err:=url.Parse(rawURL)
3435
iferr!=nil {
35-
flog.Fatal("parse url: %v",err)
36+
returnxerrors.Errorf("parse url: %v",err)
3637
}
3738

3839
listener,err:=net.Listen("tcp","127.0.0.1:0")
3940
iferr!=nil {
40-
flog.Fatal("create login server: %+v",err)
41+
returnxerrors.Errorf("create login server: %+v",err)
4142
}
4243
deferlistener.Close()
4344

@@ -54,7 +55,7 @@ func login(c *cli.Context) {
5455
(&url.URL{Scheme:u.Scheme,Host:u.Host}).String(),
5556
)
5657
iferr!=nil {
57-
flog.Fatal("write url: %v",err)
58+
returnxerrors.Errorf("write url: %v",err)
5859
}
5960

6061
authURL:= url.URL{
@@ -74,7 +75,8 @@ func login(c *cli.Context) {
7475
err=config.Session.Write(srv.Token)
7576
srv.TokenCond.L.Unlock()
7677
iferr!=nil {
77-
flog.Fatal("set session: %v",err)
78+
returnxerrors.Errorf("set session: %v",err)
7879
}
7980
flog.Success("logged in")
81+
returnnil
8082
}

‎cmd/coder/logout.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"cdr.dev/coder-cli/internal/config"
77
"github.com/urfave/cli"
8+
"golang.org/x/xerrors"
89

910
"go.coder.com/flog"
1011
)
@@ -17,14 +18,15 @@ func makeLogoutCmd() cli.Command {
1718
}
1819
}
1920

20-
funclogout(c*cli.Context) {
21+
funclogout(_*cli.Context)error {
2122
err:=config.Session.Delete()
2223
iferr!=nil {
2324
ifos.IsNotExist(err) {
2425
flog.Info("no active session")
25-
return
26+
returnnil
2627
}
27-
flog.Fatal("delete session: %v",err)
28+
returnxerrors.Errorf("delete session: %w",err)
2829
}
2930
flog.Success("logged out")
31+
returnnil
3032
}

‎cmd/coder/main.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ func main() {
5959
}
6060
}
6161

62-
// requireSuccess prints the given message and format args as a fatal error if err != nil
63-
funcrequireSuccess(errerror,msgstring,args...interface{}) {
64-
iferr!=nil {
65-
flog.Fatal(msg,args...)
66-
}
67-
}
68-
6962
funcexitHelp(c*cli.Context) {
7063
cli.ShowCommandHelpAndExit(c,c.Command.FullName(),1)
7164
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp