Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 81 additions & 60 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/sirupsen/logrus"
"github.com/skevetter/devpod/cmd/completion"
"github.com/skevetter/devpod/cmd/flags"
client2 "github.com/skevetter/devpod/pkg/client"
Expand All @@ -31,27 +30,8 @@ func NewDeleteCmd(flags *flags.GlobalFlags) *cobra.Command {
Short: "Deletes an existing workspace",
Long: `Deletes an existing workspace. You can specify the workspace by its path or name.
If the workspace is not found, you can use the --ignore-not-found flag to treat it as a successful delete.`,
RunE: func(_ *cobra.Command, args []string) error {
_, err := clientimplementation.DecodeOptionsFromEnv(
clientimplementation.DevPodFlagsDelete,
&cmd.DeleteOptions,
)
if err != nil {
return fmt.Errorf("decode up options: %w", err)
}

ctx := context.Background()
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
if err != nil {
return err
}

err = clientimplementation.DecodePlatformOptionsFromEnv(&cmd.Platform)
if err != nil {
return fmt.Errorf("decode platform options: %w", err)
}

return cmd.Run(ctx, devPodConfig, args)
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd, args)
},
ValidArgsFunction: func(
rootCmd *cobra.Command, args []string, toComplete string,
Expand All @@ -78,50 +58,91 @@ If the workspace is not found, you can use the --ignore-not-found flag to treat
}

// Run runs the command logic.
func (cmd *DeleteCmd) Run(ctx context.Context, devPodConfig *config.Config, args []string) error {
if len(args) == 0 {
workspaceName, err := workspace.Delete(
ctx,
devPodConfig,
args,
cmd.IgnoreNotFound,
cmd.Force,
cmd.DeleteOptions,
cmd.Owner,
log.Default,
)
if err != nil {
return err
}
log.WithFields(logrus.Fields{
"workspace": workspaceName,
})
log.Default.Donef("deleted workspace")
return nil
func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error {
devPodConfig, err := cmd.loadConfig()
if err != nil {
return err
}

ctx := cobraCmd.Context()
if len(args) <= 1 {
return cmd.deleteSingle(ctx, devPodConfig, args)
}

return cmd.deleteMultiple(ctx, devPodConfig, args)
}

func (cmd *DeleteCmd) loadConfig() (*config.Config, error) {
_, err := clientimplementation.DecodeOptionsFromEnv(
clientimplementation.DevPodFlagsDelete,
&cmd.DeleteOptions,
)
if err != nil {
return nil, fmt.Errorf("decode delete options: %w", err)
}

if err := clientimplementation.DecodePlatformOptionsFromEnv(&cmd.Platform); err != nil {
return nil, fmt.Errorf("decode platform options: %w", err)
}

return config.LoadConfig(cmd.Context, cmd.Provider)
}

func (cmd *DeleteCmd) deleteSingle(
ctx context.Context,
devPodConfig *config.Config,
args []string,
) error {
name, err := cmd.deleteWorkspace(ctx, devPodConfig, args)
if err != nil {
return err
}

log.Default.Donef("deleted workspace %s", name)

return nil
}

func (cmd *DeleteCmd) deleteMultiple(
ctx context.Context,
devPodConfig *config.Config,
args []string,
) error {
var errs []error
for _, arg := range args {
workspaceName, err := workspace.Delete(
ctx,
devPodConfig,
[]string{arg},
cmd.IgnoreNotFound,
cmd.Force,
cmd.DeleteOptions,
cmd.Owner,
log.Default,
)
name, err := cmd.deleteWorkspace(ctx, devPodConfig, []string{arg})
if err != nil {
log.WithFields(logrus.Fields{
"workspace": arg,
"err": err,
}).Error("failed to delete workspace")
errs = append(errs, fmt.Errorf("failed to delete workspace %s: %w", arg, err))

continue
}
log.WithFields(logrus.Fields{
"workspace": workspaceName,
})
log.Default.Donef("deleted workspace")

log.Default.Donef("deleted workspace %s", name)
}

if len(errs) > 0 {
return fmt.Errorf(
"%d workspace(s) failed to delete: %v",
len(errs),
errs,
)
}

return nil
}

func (cmd *DeleteCmd) deleteWorkspace(
ctx context.Context,
devPodConfig *config.Config,
args []string,
) (string, error) {
return workspace.Delete(ctx, workspace.DeleteOptions{
DevPodConfig: devPodConfig,
Args: args,
IgnoreNotFound: cmd.IgnoreNotFound,
Force: cmd.Force,
ClientDelete: cmd.DeleteOptions,
Owner: cmd.Owner,
Log: log.Default,
})
}
7 changes: 6 additions & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func NewExportCmd(flags *flags.GlobalFlags) *cobra.Command {
func (cmd *ExportCmd) Run(ctx context.Context, devPodConfig *config.Config, args []string) error {
// try to load workspace
logger := log.Default.ErrorStreamOnly()
client, err := workspace2.Get(ctx, devPodConfig, args, false, cmd.Owner, false, logger)
client, err := workspace2.Get(ctx, workspace2.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
Owner: cmd.Owner,
Log: logger,
})
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func (cmd *LogsCmd) Run(ctx context.Context, args []string) error {
return err
}

baseClient, err := workspace.Get(ctx, devPodConfig, args, false, cmd.Owner, false, log.Default)
baseClient, err := workspace.Get(ctx, workspace.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
Owner: cmd.Owner,
Log: log.Default,
})
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/logs_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func (cmd *LogsDaemonCmd) Run(ctx context.Context, args []string) error {
return err
}

baseClient, err := workspace.Get(ctx, devPodConfig, args, false, cmd.Owner, false, log.Default)
baseClient, err := workspace.Get(ctx, workspace.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
Owner: cmd.Owner,
Log: log.Default,
})
if err != nil {
return err
} else if baseClient.WorkspaceConfig().Machine.ID == "" {
Expand Down
16 changes: 7 additions & 9 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ func (cmd *PingCmd) Run(ctx context.Context, args []string) error {
return err
}

client, err := workspace2.Get(
ctx,
devPodConfig,
args,
true,
cmd.Owner,
false,
log.Default.ErrorStreamOnly(),
)
client, err := workspace2.Get(ctx, workspace2.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
ChangeLastUsed: true,
Owner: cmd.Owner,
Log: log.Default.ErrorStreamOnly(),
})
if err != nil {
return err
}
Expand Down
16 changes: 7 additions & 9 deletions cmd/pro/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,13 @@ func cleanupLocalWorkspaces(
wg.Add(1)
go func(w provider.Workspace) {
defer wg.Done()
client, err := workspace.Get(
ctx,
devPodConfig,
[]string{w.ID},
true,
owner,
true,
log,
)
client, err := workspace.Get(ctx, workspace.GetOptions{
DevPodConfig: devPodConfig,
Args: []string{w.ID},
Owner: owner,
LocalOnly: true,
Log: log,
})
if err != nil {
log.WithFields(logrus.Fields{
"workspaceId": w.ID,
Expand Down
17 changes: 8 additions & 9 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ func NewSSHCmd(f *flags.GlobalFlags) *cobra.Command {
localOnly := cmd.Stdio

ctx := cobraCmd.Context()
client, err := workspace2.Get(
ctx,
devPodConfig,
args,
true,
cmd.Owner,
localOnly,
log.Default.ErrorStreamOnly(),
)
client, err := workspace2.Get(ctx, workspace2.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
ChangeLastUsed: true,
Owner: cmd.Owner,
LocalOnly: localOnly,
Log: log.Default.ErrorStreamOnly(),
})
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func NewStatusCmd(flags *flags.GlobalFlags) *cobra.Command {
}

logger := log.Default.ErrorStreamOnly()
client, err := workspace2.Get(ctx, devPodConfig, args, false, cmd.Owner, false, logger)
client, err := workspace2.Get(ctx, workspace2.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
Owner: cmd.Owner,
Log: logger,
})
if err != nil {
return err
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ func NewStopCmd(flags *flags.GlobalFlags) *cobra.Command {
return fmt.Errorf("decode platform options: %w", err)
}

client, err := workspace2.Get(
ctx,
devPodConfig,
args,
false,
cmd.Owner,
false,
log.Default,
)
client, err := workspace2.Get(ctx, workspace2.GetOptions{
DevPodConfig: devPodConfig,
Args: args,
Owner: cmd.Owner,
Log: log.Default,
})
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/troubleshoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func (cmd *TroubleshootCmd) Run(ctx context.Context, args []string) {
)
}

workspaceClient, err := workspace.Get(ctx, info.Config, args, false, cmd.Owner, false, logger)
workspaceClient, err := workspace.Get(ctx, workspace.GetOptions{
DevPodConfig: info.Config,
Args: args,
Owner: cmd.Owner,
Log: logger,
})
if err == nil {
info.Workspace = workspaceClient.WorkspaceConfig()
info.WorkspaceStatus, err = workspaceClient.Status(ctx, client.StatusOptions{})
Expand Down
Loading
Loading