Skip to content

Commit c8f2aaa

Browse files
authored
feat(cmd): add delete command for storage (#952)
1 parent 1208bd0 commit c8f2aaa

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

cmd/storage.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010

1111
"github.com/OpenListTeam/OpenList/v4/internal/db"
12+
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1213
"github.com/charmbracelet/bubbles/table"
1314
tea "github.com/charmbracelet/bubbletea"
1415
"github.com/charmbracelet/lipgloss"
@@ -22,8 +23,8 @@ var storageCmd = &cobra.Command{
2223
}
2324

2425
var disableStorageCmd = &cobra.Command{
25-
Use: "disable",
26-
Short: "Disable a storage",
26+
Use: "disable [mount path]",
27+
Short: "Disable a storage by mount path",
2728
RunE: func(cmd *cobra.Command, args []string) error {
2829
if len(args) < 1 {
2930
return fmt.Errorf("mount path is required")
@@ -34,15 +35,48 @@ var disableStorageCmd = &cobra.Command{
3435
storage, err := db.GetStorageByMountPath(mountPath)
3536
if err != nil {
3637
return fmt.Errorf("failed to query storage: %+v", err)
37-
} else {
38-
storage.Disabled = true
39-
err = db.UpdateStorage(storage)
40-
if err != nil {
41-
return fmt.Errorf("failed to update storage: %+v", err)
42-
} else {
43-
fmt.Printf("Storage with mount path [%s] have been disabled\n", mountPath)
38+
}
39+
storage.Disabled = true
40+
err = db.UpdateStorage(storage)
41+
if err != nil {
42+
return fmt.Errorf("failed to update storage: %+v", err)
43+
}
44+
utils.Log.Infof("Storage with mount path [%s] has been disabled from CLI", mountPath)
45+
fmt.Printf("Storage with mount path [%s] has been disabled\n", mountPath)
46+
return nil
47+
},
48+
}
49+
50+
var deleteStorageCmd = &cobra.Command{
51+
Use: "delete [id]",
52+
Short: "Delete a storage by id",
53+
RunE: func(cmd *cobra.Command, args []string) error {
54+
if len(args) < 1 {
55+
return fmt.Errorf("id is required")
56+
}
57+
id, err := strconv.Atoi(args[0])
58+
if err != nil {
59+
return fmt.Errorf("id must be a number")
60+
}
61+
62+
if force, _ := cmd.Flags().GetBool("force"); force {
63+
fmt.Printf("Are you sure you want to delete storage with id [%d]? [y/N]: ", id)
64+
var confirm string
65+
fmt.Scanln(&confirm)
66+
if confirm != "y" && confirm != "Y" {
67+
fmt.Println("Delete operation cancelled.")
68+
return nil
4469
}
4570
}
71+
72+
Init()
73+
defer Release()
74+
err = db.DeleteStorageById(uint(id))
75+
if err != nil {
76+
return fmt.Errorf("failed to delete storage by id: %+v", err)
77+
}
78+
utils.Log.Infof("Storage with id [%d] have been deleted from CLI", id)
79+
fmt.Printf("Storage with id [%d] have been deleted\n", id)
4680
return nil
4781
},
4882
}
@@ -152,6 +186,8 @@ func init() {
152186
storageCmd.AddCommand(disableStorageCmd)
153187
storageCmd.AddCommand(listStorageCmd)
154188
storageCmd.PersistentFlags().IntVarP(&storageTableHeight, "height", "H", 10, "Table height")
189+
storageCmd.AddCommand(deleteStorageCmd)
190+
deleteStorageCmd.Flags().BoolP("force", "f", false, "Force delete without confirmation")
155191
// Here you will define your flags and configuration settings.
156192

157193
// Cobra supports Persistent Flags which will work for this command

0 commit comments

Comments
 (0)