Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit bf9a6ae

Browse files
authored
Merge pull request #123 from cdr/fix-fatal
Remove fatal calls from sdk client creation
2 parents 2edd14e + 2446fbf commit bf9a6ae

File tree

8 files changed

+73
-49
lines changed

8 files changed

+73
-49
lines changed

Diff for: internal/cmd/auth.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,24 @@ import (
77

88
"cdr.dev/coder-cli/coder-sdk"
99
"cdr.dev/coder-cli/internal/config"
10-
11-
"go.coder.com/flog"
1210
)
1311

14-
// requireAuth exits the process with a nonzero exit code if the user is not authenticated to make requests.
15-
func requireAuth() *coder.Client {
16-
client, err := newClient()
17-
if err != nil {
18-
flog.Fatal("%s", err)
19-
}
20-
return client
21-
}
12+
var errNeedLogin = xerrors.New("failed to read session credentials: did you run \"coder login\"?")
2213

2314
func newClient() (*coder.Client, error) {
2415
sessionToken, err := config.Session.Read()
2516
if err != nil {
26-
return nil, xerrors.Errorf("read session: %w (did you run coder login?)", err)
17+
return nil, errNeedLogin
2718
}
2819

2920
rawURL, err := config.URL.Read()
3021
if err != nil {
31-
return nil, xerrors.Errorf("read url: %w (did you run coder login?)", err)
22+
return nil, errNeedLogin
3223
}
3324

3425
u, err := url.Parse(rawURL)
3526
if err != nil {
36-
return nil, xerrors.Errorf("url misformatted: %w (try runing coder login)", err)
27+
return nil, xerrors.Errorf("url misformatted: %w try runing \"coder login\" with a valid URL", err)
3728
}
3829

3930
return &coder.Client{

Diff for: internal/cmd/configssh.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
8888
return nil
8989
}
9090

91-
client := requireAuth()
91+
client, err := newClient()
92+
if err != nil {
93+
return err
94+
}
9295

9396
sshAvailable := isSSHAvailable(ctx)
9497
if !sshAvailable {

Diff for: internal/cmd/envs.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func makeEnvsCommand() *cobra.Command {
2727
Short: "list all environments owned by the active user",
2828
Long: "List all Coder environments owned by the active user.",
2929
RunE: func(cmd *cobra.Command, args []string) error {
30-
entClient := requireAuth()
31-
envs, err := getEnvs(cmd.Context(), entClient, user)
30+
client, err := newClient()
31+
if err != nil {
32+
return err
33+
}
34+
envs, err := getEnvs(cmd.Context(), client, user)
3235
if err != nil {
3336
return err
3437
}

Diff for: internal/cmd/secrets.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,14 @@ coder secrets create aws-credentials --from-file ./credentials.json`,
8080
},
8181
RunE: func(cmd *cobra.Command, args []string) error {
8282
var (
83-
client = requireAuth()
84-
name = args[0]
85-
value string
86-
err error
83+
name = args[0]
84+
value string
85+
err error
8786
)
87+
client, err := newClient()
88+
if err != nil {
89+
return err
90+
}
8891
if fromLiteral != "" {
8992
value = fromLiteral
9093
} else if fromFile != "" {
@@ -136,7 +139,10 @@ coder secrets create aws-credentials --from-file ./credentials.json`,
136139

137140
func listSecrets(userEmail *string) func(cmd *cobra.Command, _ []string) error {
138141
return func(cmd *cobra.Command, _ []string) error {
139-
client := requireAuth()
142+
client, err := newClient()
143+
if err != nil {
144+
return err
145+
}
140146
user, err := client.UserByEmail(cmd.Context(), *userEmail)
141147
if err != nil {
142148
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)
@@ -167,9 +173,12 @@ func listSecrets(userEmail *string) func(cmd *cobra.Command, _ []string) error {
167173
func makeViewSecret(userEmail *string) func(cmd *cobra.Command, args []string) error {
168174
return func(cmd *cobra.Command, args []string) error {
169175
var (
170-
client = requireAuth()
171-
name = args[0]
176+
name = args[0]
172177
)
178+
client, err := newClient()
179+
if err != nil {
180+
return err
181+
}
173182
user, err := client.UserByEmail(cmd.Context(), *userEmail)
174183
if err != nil {
175184
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)
@@ -190,9 +199,10 @@ func makeViewSecret(userEmail *string) func(cmd *cobra.Command, args []string) e
190199

191200
func makeRemoveSecrets(userEmail *string) func(c *cobra.Command, args []string) error {
192201
return func(cmd *cobra.Command, args []string) error {
193-
var (
194-
client = requireAuth()
195-
)
202+
client, err := newClient()
203+
if err != nil {
204+
return err
205+
}
196206
user, err := client.UserByEmail(cmd.Context(), *userEmail)
197207
if err != nil {
198208
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)

Diff for: internal/cmd/shell.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ func sendResizeEvents(ctx context.Context, termFD uintptr, process wsep.Process)
9797
}
9898

9999
func runCommand(ctx context.Context, envName, command string, args []string) error {
100-
entClient := requireAuth()
101-
102-
env, err := findEnv(ctx, entClient, envName, coder.Me)
100+
client, err := newClient()
101+
if err != nil {
102+
return err
103+
}
104+
env, err := findEnv(ctx, client, envName, coder.Me)
103105
if err != nil {
104106
return xerrors.Errorf("find environment: %w", err)
105107
}
@@ -125,7 +127,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
125127
ctx, cancel := context.WithCancel(ctx)
126128
defer cancel()
127129

128-
conn, err := entClient.DialWsep(ctx, env)
130+
conn, err := client.DialWsep(ctx, env)
129131
if err != nil {
130132
return xerrors.Errorf("dial websocket: %w", err)
131133
}
@@ -165,7 +167,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
165167
stdin := process.Stdin()
166168
defer func() { _ = stdin.Close() }() // Best effort.
167169

168-
ap := activity.NewPusher(entClient, env.ID, sshActivityName)
170+
ap := activity.NewPusher(client, env.ID, sshActivityName)
169171
wr := ap.Writer(stdin)
170172
if _, err := io.Copy(wr, os.Stdin); err != nil {
171173
cancel()

Diff for: internal/cmd/sync.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
5252
remote = args[1]
5353
)
5454

55-
entClient := requireAuth()
55+
client, err := newClient()
56+
if err != nil {
57+
return err
58+
}
5659

5760
info, err := os.Stat(local)
5861
if err != nil {
@@ -64,29 +67,29 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
6467

6568
remoteTokens := strings.SplitN(remote, ":", 2)
6669
if len(remoteTokens) != 2 {
67-
flog.Fatal("remote misformatted")
70+
return xerrors.New("remote misformatted")
6871
}
6972
var (
7073
envName = remoteTokens[0]
7174
remoteDir = remoteTokens[1]
7275
)
7376

74-
env, err := findEnv(cmd.Context(), entClient, envName, coder.Me)
77+
env, err := findEnv(cmd.Context(), client, envName, coder.Me)
7578
if err != nil {
7679
return err
7780
}
7881

7982
absLocal, err := filepath.Abs(local)
8083
if err != nil {
81-
flog.Fatal("make abs path out of %v: %v", local, absLocal)
84+
return xerrors.Errorf("make abs path out of %s, %s: %w", local, absLocal, err)
8285
}
8386

8487
s := sync.Sync{
8588
Init: *init,
8689
Env: *env,
8790
RemoteDir: remoteDir,
8891
LocalDir: absLocal,
89-
Client: entClient,
92+
Client: client,
9093
}
9194

9295
localVersion := rsyncVersion()
@@ -95,7 +98,7 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
9598
if rsyncErr != nil {
9699
flog.Info("Unable to determine remote rsync version. Proceeding cautiously.")
97100
} else if localVersion != remoteVersion {
98-
flog.Fatal("rsync protocol mismatch: local = %v, remote = %v", localVersion, rsyncErr)
101+
return xerrors.Errorf("rsync protocol mismatch: local = %s, remote = %s", localVersion, remoteVersion)
99102
}
100103

101104
for err == nil || err == sync.ErrRestartSync {

Diff for: internal/cmd/urls.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,12 @@ func makeCreateDevURL() *cobra.Command {
152152
if urlname != "" && !devURLNameValidRx.MatchString(urlname) {
153153
return xerrors.New("update devurl: name must be < 64 chars in length, begin with a letter and only contain letters or digits.")
154154
}
155-
entClient := requireAuth()
155+
client, err := newClient()
156+
if err != nil {
157+
return err
158+
}
156159

157-
env, err := findEnv(cmd.Context(), entClient, envName, coder.Me)
160+
env, err := findEnv(cmd.Context(), client, envName, coder.Me)
158161
if err != nil {
159162
return err
160163
}
@@ -167,13 +170,13 @@ func makeCreateDevURL() *cobra.Command {
167170
urlID, found := devURLID(portNum, urls)
168171
if found {
169172
flog.Info("Updating devurl for port %v", port)
170-
err := entClient.UpdateDevURL(cmd.Context(), env.ID, urlID, portNum, urlname, access)
173+
err := client.UpdateDevURL(cmd.Context(), env.ID, urlID, portNum, urlname, access)
171174
if err != nil {
172175
return xerrors.Errorf("update DevURL: %w", err)
173176
}
174177
} else {
175178
flog.Info("Adding devurl for port %v", port)
176-
err := entClient.InsertDevURL(cmd.Context(), env.ID, portNum, urlname, access)
179+
err := client.InsertDevURL(cmd.Context(), env.ID, portNum, urlname, access)
177180
if err != nil {
178181
return xerrors.Errorf("insert DevURL: %w", err)
179182
}
@@ -218,8 +221,11 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
218221
return xerrors.Errorf("validate port: %w", err)
219222
}
220223

221-
entClient := requireAuth()
222-
env, err := findEnv(cmd.Context(), entClient, envName, coder.Me)
224+
client, err := newClient()
225+
if err != nil {
226+
return err
227+
}
228+
env, err := findEnv(cmd.Context(), client, envName, coder.Me)
223229
if err != nil {
224230
return err
225231
}
@@ -236,22 +242,25 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
236242
return xerrors.Errorf("No devurl found for port %v", port)
237243
}
238244

239-
if err := entClient.DelDevURL(cmd.Context(), env.ID, urlID); err != nil {
245+
if err := client.DelDevURL(cmd.Context(), env.ID, urlID); err != nil {
240246
return xerrors.Errorf("delete DevURL: %w", err)
241247
}
242248
return nil
243249
}
244250

245251
// urlList returns the list of active devURLs from the cemanager.
246252
func urlList(ctx context.Context, envName string) ([]DevURL, error) {
247-
entClient := requireAuth()
248-
env, err := findEnv(ctx, entClient, envName, coder.Me)
253+
client, err := newClient()
254+
if err != nil {
255+
return nil, err
256+
}
257+
env, err := findEnv(ctx, client, envName, coder.Me)
249258
if err != nil {
250259
return nil, err
251260
}
252261

253262
reqString := "%s/api/environments/%s/devurls?session_token=%s"
254-
reqURL := fmt.Sprintf(reqString, entClient.BaseURL, env.ID, entClient.Token)
263+
reqURL := fmt.Sprintf(reqString, client.BaseURL, env.ID, client.Token)
255264

256265
resp, err := http.Get(reqURL)
257266
if err != nil {

Diff for: internal/cmd/users.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ coder users ls -o json | jq .[] | jq -r .email`,
3232

3333
func listUsers(outputFmt *string) func(cmd *cobra.Command, args []string) error {
3434
return func(cmd *cobra.Command, args []string) error {
35-
entClient := requireAuth()
35+
client, err := newClient()
36+
if err != nil {
37+
return err
38+
}
3639

37-
users, err := entClient.Users(cmd.Context())
40+
users, err := client.Users(cmd.Context())
3841
if err != nil {
3942
return xerrors.Errorf("get users: %w", err)
4043
}

0 commit comments

Comments
 (0)