Skip to content

Commit

Permalink
feat: confirmation on workspace removal (#380)
Browse files Browse the repository at this point in the history
Signed-off-by: Ezhil Shanmugham <[email protected]>
  • Loading branch information
ezhil56x authored Apr 12, 2024
1 parent c1d70a2 commit ac87c61
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions pkg/cmd/workspace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ var DeleteCmd = &cobra.Command{
return
}

c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}

activeProfile, err := c.GetActiveProfile()
if err != nil {
log.Fatal(err)
}

ctx := context.Background()
var workspace *serverapiclient.WorkspaceDTO

Expand All @@ -97,17 +87,30 @@ var DeleteCmd = &cobra.Command{
return
}

res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, *workspace.Id).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
if !yesFlag {
form := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title(fmt.Sprintf("Delete workspace %s?", *workspace.Name)).
Description(fmt.Sprintf("Are you sure you want to delete workspace %s?", *workspace.Name)).
Value(&yesFlag),
),
).WithTheme(views.GetCustomTheme())

err := form.Run()
if err != nil {
log.Fatal(err)
}
}

err = config.RemoveWorkspaceSshEntries(activeProfile.Id, *workspace.Id)
if err != nil {
log.Fatal(err)
if yesFlag {
err := removeWorkspace(ctx, apiClient, workspace)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println("Operation canceled.")
}

util.RenderInfoMessage(fmt.Sprintf("Workspace %s successfully deleted", *workspace.Name))
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
Expand Down Expand Up @@ -145,3 +148,28 @@ func DeleteAllWorkspaces() error {
}
return nil
}

func removeWorkspace(ctx context.Context, apiClient *serverapiclient.APIClient, workspace *serverapiclient.WorkspaceDTO) error {
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, *workspace.Id).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
}

c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}

activeProfile, err := c.GetActiveProfile()
if err != nil {
log.Fatal(err)
}

err = config.RemoveWorkspaceSshEntries(activeProfile.Id, *workspace.Id)
if err != nil {
log.Fatal(err)
}

util.RenderInfoMessage(fmt.Sprintf("Workspace %s successfully deleted", *workspace.Name))
return nil
}

0 comments on commit ac87c61

Please sign in to comment.