-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdelete.go
More file actions
94 lines (82 loc) · 2.31 KB
/
delete.go
File metadata and controls
94 lines (82 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/nicksenap/grove/internal/console"
"github.com/nicksenap/grove/internal/lifecycle"
"github.com/nicksenap/grove/internal/picker"
"github.com/nicksenap/grove/internal/state"
"github.com/nicksenap/grove/internal/workspace"
"github.com/spf13/cobra"
)
var (
deleteCmdForce bool
wsDeleteCmdForce bool
)
// deleteCmd is the top-level "gw delete" command.
var deleteCmd = &cobra.Command{
Use: "delete [NAME]",
Short: "Delete a workspace (shortcut for gw ws delete)",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
doDelete(args, deleteCmdForce)
},
}
// wsDeleteCmd is "gw ws delete".
var wsDeleteCmd = &cobra.Command{
Use: "delete [NAME]",
Short: "Delete a workspace",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
doDelete(args, wsDeleteCmdForce)
},
}
func doDelete(args []string, force bool) {
var names []string
if len(args) > 0 {
names = []string{args[0]}
} else {
// Interactive multi-select
workspaces, err := state.Load()
if err != nil {
exitError(err.Error())
}
if len(workspaces) == 0 {
exitError("No workspaces to delete")
}
choices := make([]string, len(workspaces))
for i, ws := range workspaces {
choices[i] = ws.Name
}
selected, err := picker.PickMany("Select workspaces to delete:", choices)
if err != nil {
exitOnPickerErr(err)
}
names = selected
}
if !force {
if !console.Confirm(fmt.Sprintf("Delete %s?", strings.Join(names, ", ")), false) {
return
}
}
for _, name := range names {
// Fire pre_delete hook before teardown (e.g. harvest Claude memory)
ws, _ := state.GetWorkspace(name)
if ws != nil {
vars := lifecycle.Vars{Name: name, Path: ws.Path, Branch: ws.Branch}
if err := lifecycle.Run("pre_delete", vars); err != nil && !errors.Is(err, lifecycle.ErrNoHook) {
console.Warningf("pre_delete hook failed: %s", err)
}
}
if err := workspace.NewService().Delete(name); err != nil {
exitError(err.Error())
}
}
}
func init() {
deleteCmd.Flags().BoolVarP(&deleteCmdForce, "force", "f", false, "Skip confirmation")
deleteCmd.ValidArgsFunction = completeWorkspaceNames
wsDeleteCmd.Flags().BoolVarP(&wsDeleteCmdForce, "force", "f", false, "Skip confirmation")
wsDeleteCmd.ValidArgsFunction = completeWorkspaceNames
}