Skip to content

Commit af23128

Browse files
committed
fix: Fix linting (gocyclo, revive), improve responsiveness
1 parent ab6f7ab commit af23128

File tree

1 file changed

+70
-45
lines changed

1 file changed

+70
-45
lines changed

cli/configssh.go

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"bufio"
55
"bytes"
6+
"context"
67
"errors"
78
"fmt"
89
"io"
@@ -15,6 +16,7 @@ import (
1516
"strings"
1617

1718
"github.com/cli/safeexec"
19+
"github.com/google/uuid"
1820
"github.com/pkg/diff"
1921
"github.com/pkg/diff/write"
2022
"github.com/spf13/cobra"
@@ -97,6 +99,65 @@ func (o sshCoderConfigOptions) asList() (list []string) {
9799
return list
98100
}
99101

102+
type sshWorkspaceConfig struct {
103+
Name string
104+
Hosts []string
105+
}
106+
107+
func sshPrepareWorkspaceConfigs(ctx context.Context, client *codersdk.Client, organizationID uuid.UUID) (receive func() ([]sshWorkspaceConfig, error)) {
108+
wcC := make(chan []sshWorkspaceConfig, 1)
109+
errC := make(chan error, 1)
110+
go func() {
111+
wc, err := func() ([]sshWorkspaceConfig, error) {
112+
workspaces, err := client.WorkspacesByOwner(ctx, organizationID, codersdk.Me)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
var errGroup errgroup.Group
118+
workspaceConfigs := make([]sshWorkspaceConfig, len(workspaces))
119+
for i, workspace := range workspaces {
120+
i := i
121+
workspace := workspace
122+
errGroup.Go(func() error {
123+
resources, err := client.TemplateVersionResources(ctx, workspace.LatestBuild.TemplateVersionID)
124+
if err != nil {
125+
return err
126+
}
127+
128+
wc := sshWorkspaceConfig{Name: workspace.Name}
129+
for _, resource := range resources {
130+
if resource.Transition != codersdk.WorkspaceTransitionStart {
131+
continue
132+
}
133+
for _, agent := range resource.Agents {
134+
hostname := workspace.Name
135+
if len(resource.Agents) > 1 {
136+
hostname += "." + agent.Name
137+
}
138+
wc.Hosts = append(wc.Hosts, hostname)
139+
}
140+
}
141+
workspaceConfigs[i] = wc
142+
143+
return nil
144+
})
145+
}
146+
err = errGroup.Wait()
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
return workspaceConfigs, nil
152+
}()
153+
wcC <- wc
154+
errC <- err
155+
}()
156+
return func() ([]sshWorkspaceConfig, error) {
157+
return <-wcC, <-errC
158+
}
159+
}
160+
100161
func configSSH() *cobra.Command {
101162
var (
102163
coderConfig sshCoderConfigOptions
@@ -136,11 +197,7 @@ func configSSH() *cobra.Command {
136197
return err
137198
}
138199

139-
// Early check for workspaces to ensure API key has not expired.
140-
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
141-
if err != nil {
142-
return err
143-
}
200+
recvWorkspaceConfigs := sshPrepareWorkspaceConfigs(cmd.Context(), client, organization.ID)
144201

145202
out := cmd.OutOrStdout()
146203
if showDiff {
@@ -174,6 +231,7 @@ func configSSH() *cobra.Command {
174231
coderConfigExists := true
175232
coderConfigRaw, err := os.ReadFile(coderConfigFile)
176233
if err != nil {
234+
//nolint: revive // Inverting this if statement doesn't improve readability.
177235
if errors.Is(err, fs.ErrNotExist) {
178236
coderConfigExists = false
179237
} else {
@@ -234,43 +292,6 @@ func configSSH() *cobra.Command {
234292
}
235293

236294
root := createConfig(cmd)
237-
var errGroup errgroup.Group
238-
type workspaceConfig struct {
239-
Name string
240-
Hosts []string
241-
}
242-
workspaceConfigs := make([]workspaceConfig, len(workspaces))
243-
for i, workspace := range workspaces {
244-
i := i
245-
workspace := workspace
246-
errGroup.Go(func() error {
247-
resources, err := client.TemplateVersionResources(cmd.Context(), workspace.LatestBuild.TemplateVersionID)
248-
if err != nil {
249-
return err
250-
}
251-
252-
wc := workspaceConfig{Name: workspace.Name}
253-
for _, resource := range resources {
254-
if resource.Transition != codersdk.WorkspaceTransitionStart {
255-
continue
256-
}
257-
for _, agent := range resource.Agents {
258-
hostname := workspace.Name
259-
if len(resource.Agents) > 1 {
260-
hostname += "." + agent.Name
261-
}
262-
wc.Hosts = append(wc.Hosts, hostname)
263-
}
264-
}
265-
workspaceConfigs[i] = wc
266-
267-
return nil
268-
})
269-
}
270-
err = errGroup.Wait()
271-
if err != nil {
272-
return err
273-
}
274295

275296
buf := &bytes.Buffer{}
276297

@@ -281,8 +302,12 @@ func configSSH() *cobra.Command {
281302
return xerrors.Errorf("write coder config header failed: %w", err)
282303
}
283304

305+
workspaceConfigs, err := recvWorkspaceConfigs()
306+
if err != nil {
307+
return xerrors.Errorf("fetch workspace configs failed: %w", err)
308+
}
284309
// Ensure stable sorting of output.
285-
slices.SortFunc(workspaceConfigs, func(a, b workspaceConfig) bool {
310+
slices.SortFunc(workspaceConfigs, func(a, b sshWorkspaceConfig) bool {
286311
return a.Name < b.Name
287312
})
288313
for _, wc := range workspaceConfigs {
@@ -379,9 +404,9 @@ func configSSH() *cobra.Command {
379404
}
380405
}
381406

382-
if len(workspaces) > 0 {
407+
if len(workspaceConfigs) > 0 {
383408
_, _ = fmt.Fprintln(out, "You should now be able to ssh into your workspace.")
384-
_, _ = fmt.Fprintf(out, "For example, try running:\n\n\t$ ssh coder.%s\n\n", workspaces[0].Name)
409+
_, _ = fmt.Fprintf(out, "For example, try running:\n\n\t$ ssh coder.%s\n\n", workspaceConfigs[0].Name)
385410
} else {
386411
_, _ = fmt.Fprint(out, "You don't have any workspaces yet, try creating one with:\n\n\t$ coder create <workspace>\n\n")
387412
}

0 commit comments

Comments
 (0)