Skip to content

Commit bea0ead

Browse files
authored
refactor(workspace): replace function parameters with structured options (#589)
Signed-off-by: Samuel K <skevetter@pm.me>
1 parent 67d7ec0 commit bea0ead

13 files changed

Lines changed: 409 additions & 283 deletions

File tree

cmd/delete.go

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/sirupsen/logrus"
87
"github.com/skevetter/devpod/cmd/completion"
98
"github.com/skevetter/devpod/cmd/flags"
109
client2 "github.com/skevetter/devpod/pkg/client"
@@ -31,27 +30,8 @@ func NewDeleteCmd(flags *flags.GlobalFlags) *cobra.Command {
3130
Short: "Deletes an existing workspace",
3231
Long: `Deletes an existing workspace. You can specify the workspace by its path or name.
3332
If the workspace is not found, you can use the --ignore-not-found flag to treat it as a successful delete.`,
34-
RunE: func(_ *cobra.Command, args []string) error {
35-
_, err := clientimplementation.DecodeOptionsFromEnv(
36-
clientimplementation.DevPodFlagsDelete,
37-
&cmd.DeleteOptions,
38-
)
39-
if err != nil {
40-
return fmt.Errorf("decode up options: %w", err)
41-
}
42-
43-
ctx := context.Background()
44-
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
45-
if err != nil {
46-
return err
47-
}
48-
49-
err = clientimplementation.DecodePlatformOptionsFromEnv(&cmd.Platform)
50-
if err != nil {
51-
return fmt.Errorf("decode platform options: %w", err)
52-
}
53-
54-
return cmd.Run(ctx, devPodConfig, args)
33+
RunE: func(cobraCmd *cobra.Command, args []string) error {
34+
return cmd.Run(cobraCmd, args)
5535
},
5636
ValidArgsFunction: func(
5737
rootCmd *cobra.Command, args []string, toComplete string,
@@ -78,50 +58,91 @@ If the workspace is not found, you can use the --ignore-not-found flag to treat
7858
}
7959

8060
// Run runs the command logic.
81-
func (cmd *DeleteCmd) Run(ctx context.Context, devPodConfig *config.Config, args []string) error {
82-
if len(args) == 0 {
83-
workspaceName, err := workspace.Delete(
84-
ctx,
85-
devPodConfig,
86-
args,
87-
cmd.IgnoreNotFound,
88-
cmd.Force,
89-
cmd.DeleteOptions,
90-
cmd.Owner,
91-
log.Default,
92-
)
93-
if err != nil {
94-
return err
95-
}
96-
log.WithFields(logrus.Fields{
97-
"workspace": workspaceName,
98-
})
99-
log.Default.Donef("deleted workspace")
100-
return nil
61+
func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error {
62+
devPodConfig, err := cmd.loadConfig()
63+
if err != nil {
64+
return err
10165
}
10266

67+
ctx := cobraCmd.Context()
68+
if len(args) <= 1 {
69+
return cmd.deleteSingle(ctx, devPodConfig, args)
70+
}
71+
72+
return cmd.deleteMultiple(ctx, devPodConfig, args)
73+
}
74+
75+
func (cmd *DeleteCmd) loadConfig() (*config.Config, error) {
76+
_, err := clientimplementation.DecodeOptionsFromEnv(
77+
clientimplementation.DevPodFlagsDelete,
78+
&cmd.DeleteOptions,
79+
)
80+
if err != nil {
81+
return nil, fmt.Errorf("decode delete options: %w", err)
82+
}
83+
84+
if err := clientimplementation.DecodePlatformOptionsFromEnv(&cmd.Platform); err != nil {
85+
return nil, fmt.Errorf("decode platform options: %w", err)
86+
}
87+
88+
return config.LoadConfig(cmd.Context, cmd.Provider)
89+
}
90+
91+
func (cmd *DeleteCmd) deleteSingle(
92+
ctx context.Context,
93+
devPodConfig *config.Config,
94+
args []string,
95+
) error {
96+
name, err := cmd.deleteWorkspace(ctx, devPodConfig, args)
97+
if err != nil {
98+
return err
99+
}
100+
101+
log.Default.Donef("deleted workspace %s", name)
102+
103+
return nil
104+
}
105+
106+
func (cmd *DeleteCmd) deleteMultiple(
107+
ctx context.Context,
108+
devPodConfig *config.Config,
109+
args []string,
110+
) error {
111+
var errs []error
103112
for _, arg := range args {
104-
workspaceName, err := workspace.Delete(
105-
ctx,
106-
devPodConfig,
107-
[]string{arg},
108-
cmd.IgnoreNotFound,
109-
cmd.Force,
110-
cmd.DeleteOptions,
111-
cmd.Owner,
112-
log.Default,
113-
)
113+
name, err := cmd.deleteWorkspace(ctx, devPodConfig, []string{arg})
114114
if err != nil {
115-
log.WithFields(logrus.Fields{
116-
"workspace": arg,
117-
"err": err,
118-
}).Error("failed to delete workspace")
115+
errs = append(errs, fmt.Errorf("failed to delete workspace %s: %w", arg, err))
116+
119117
continue
120118
}
121-
log.WithFields(logrus.Fields{
122-
"workspace": workspaceName,
123-
})
124-
log.Default.Donef("deleted workspace")
119+
120+
log.Default.Donef("deleted workspace %s", name)
121+
}
122+
123+
if len(errs) > 0 {
124+
return fmt.Errorf(
125+
"%d workspace(s) failed to delete: %v",
126+
len(errs),
127+
errs,
128+
)
125129
}
130+
126131
return nil
127132
}
133+
134+
func (cmd *DeleteCmd) deleteWorkspace(
135+
ctx context.Context,
136+
devPodConfig *config.Config,
137+
args []string,
138+
) (string, error) {
139+
return workspace.Delete(ctx, workspace.DeleteOptions{
140+
DevPodConfig: devPodConfig,
141+
Args: args,
142+
IgnoreNotFound: cmd.IgnoreNotFound,
143+
Force: cmd.Force,
144+
ClientDelete: cmd.DeleteOptions,
145+
Owner: cmd.Owner,
146+
Log: log.Default,
147+
})
148+
}

cmd/export.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ func NewExportCmd(flags *flags.GlobalFlags) *cobra.Command {
5959
func (cmd *ExportCmd) Run(ctx context.Context, devPodConfig *config.Config, args []string) error {
6060
// try to load workspace
6161
logger := log.Default.ErrorStreamOnly()
62-
client, err := workspace2.Get(ctx, devPodConfig, args, false, cmd.Owner, false, logger)
62+
client, err := workspace2.Get(ctx, workspace2.GetOptions{
63+
DevPodConfig: devPodConfig,
64+
Args: args,
65+
Owner: cmd.Owner,
66+
Log: logger,
67+
})
6368
if err != nil {
6469
return err
6570
}

cmd/logs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ func (cmd *LogsCmd) Run(ctx context.Context, args []string) error {
5959
return err
6060
}
6161

62-
baseClient, err := workspace.Get(ctx, devPodConfig, args, false, cmd.Owner, false, log.Default)
62+
baseClient, err := workspace.Get(ctx, workspace.GetOptions{
63+
DevPodConfig: devPodConfig,
64+
Args: args,
65+
Owner: cmd.Owner,
66+
Log: log.Default,
67+
})
6368
if err != nil {
6469
return err
6570
}

cmd/logs_daemon.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ func (cmd *LogsDaemonCmd) Run(ctx context.Context, args []string) error {
4242
return err
4343
}
4444

45-
baseClient, err := workspace.Get(ctx, devPodConfig, args, false, cmd.Owner, false, log.Default)
45+
baseClient, err := workspace.Get(ctx, workspace.GetOptions{
46+
DevPodConfig: devPodConfig,
47+
Args: args,
48+
Owner: cmd.Owner,
49+
Log: log.Default,
50+
})
4651
if err != nil {
4752
return err
4853
} else if baseClient.WorkspaceConfig().Machine.ID == "" {

cmd/ping.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ func (cmd *PingCmd) Run(ctx context.Context, args []string) error {
5151
return err
5252
}
5353

54-
client, err := workspace2.Get(
55-
ctx,
56-
devPodConfig,
57-
args,
58-
true,
59-
cmd.Owner,
60-
false,
61-
log.Default.ErrorStreamOnly(),
62-
)
54+
client, err := workspace2.Get(ctx, workspace2.GetOptions{
55+
DevPodConfig: devPodConfig,
56+
Args: args,
57+
ChangeLastUsed: true,
58+
Owner: cmd.Owner,
59+
Log: log.Default.ErrorStreamOnly(),
60+
})
6361
if err != nil {
6462
return err
6563
}

cmd/pro/delete.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,13 @@ func cleanupLocalWorkspaces(
161161
wg.Add(1)
162162
go func(w provider.Workspace) {
163163
defer wg.Done()
164-
client, err := workspace.Get(
165-
ctx,
166-
devPodConfig,
167-
[]string{w.ID},
168-
true,
169-
owner,
170-
true,
171-
log,
172-
)
164+
client, err := workspace.Get(ctx, workspace.GetOptions{
165+
DevPodConfig: devPodConfig,
166+
Args: []string{w.ID},
167+
Owner: owner,
168+
LocalOnly: true,
169+
Log: log,
170+
})
173171
if err != nil {
174172
log.WithFields(logrus.Fields{
175173
"workspaceId": w.ID,

cmd/ssh.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ func NewSSHCmd(f *flags.GlobalFlags) *cobra.Command {
8282
localOnly := cmd.Stdio
8383

8484
ctx := cobraCmd.Context()
85-
client, err := workspace2.Get(
86-
ctx,
87-
devPodConfig,
88-
args,
89-
true,
90-
cmd.Owner,
91-
localOnly,
92-
log.Default.ErrorStreamOnly(),
93-
)
85+
client, err := workspace2.Get(ctx, workspace2.GetOptions{
86+
DevPodConfig: devPodConfig,
87+
Args: args,
88+
ChangeLastUsed: true,
89+
Owner: cmd.Owner,
90+
LocalOnly: localOnly,
91+
Log: log.Default.ErrorStreamOnly(),
92+
})
9493
if err != nil {
9594
return err
9695
}

cmd/status.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ func NewStatusCmd(flags *flags.GlobalFlags) *cobra.Command {
4949
}
5050

5151
logger := log.Default.ErrorStreamOnly()
52-
client, err := workspace2.Get(ctx, devPodConfig, args, false, cmd.Owner, false, logger)
52+
client, err := workspace2.Get(ctx, workspace2.GetOptions{
53+
DevPodConfig: devPodConfig,
54+
Args: args,
55+
Owner: cmd.Owner,
56+
Log: logger,
57+
})
5358
if err != nil {
5459
return err
5560
}

cmd/stop.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,12 @@ func NewStopCmd(flags *flags.GlobalFlags) *cobra.Command {
4242
return fmt.Errorf("decode platform options: %w", err)
4343
}
4444

45-
client, err := workspace2.Get(
46-
ctx,
47-
devPodConfig,
48-
args,
49-
false,
50-
cmd.Owner,
51-
false,
52-
log.Default,
53-
)
45+
client, err := workspace2.Get(ctx, workspace2.GetOptions{
46+
DevPodConfig: devPodConfig,
47+
Args: args,
48+
Owner: cmd.Owner,
49+
Log: log.Default,
50+
})
5451
if err != nil {
5552
return err
5653
}

cmd/troubleshoot.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ func (cmd *TroubleshootCmd) Run(ctx context.Context, args []string) {
110110
)
111111
}
112112

113-
workspaceClient, err := workspace.Get(ctx, info.Config, args, false, cmd.Owner, false, logger)
113+
workspaceClient, err := workspace.Get(ctx, workspace.GetOptions{
114+
DevPodConfig: info.Config,
115+
Args: args,
116+
Owner: cmd.Owner,
117+
Log: logger,
118+
})
114119
if err == nil {
115120
info.Workspace = workspaceClient.WorkspaceConfig()
116121
info.WorkspaceStatus, err = workspaceClient.Status(ctx, client.StatusOptions{})

0 commit comments

Comments
 (0)