diff --git a/cmd/storage.go b/cmd/storage.go index 405e676c..c744dace 100644 --- a/cmd/storage.go +++ b/cmd/storage.go @@ -9,6 +9,7 @@ import ( "strconv" "github.com/OpenListTeam/OpenList/v4/internal/db" + "github.com/OpenListTeam/OpenList/v4/pkg/utils" "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -22,8 +23,8 @@ var storageCmd = &cobra.Command{ } var disableStorageCmd = &cobra.Command{ - Use: "disable", - Short: "Disable a storage", + Use: "disable [mount path]", + Short: "Disable a storage by mount path", RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { return fmt.Errorf("mount path is required") @@ -34,15 +35,48 @@ var disableStorageCmd = &cobra.Command{ storage, err := db.GetStorageByMountPath(mountPath) if err != nil { return fmt.Errorf("failed to query storage: %+v", err) - } else { - storage.Disabled = true - err = db.UpdateStorage(storage) - if err != nil { - return fmt.Errorf("failed to update storage: %+v", err) - } else { - fmt.Printf("Storage with mount path [%s] have been disabled\n", mountPath) + } + storage.Disabled = true + err = db.UpdateStorage(storage) + if err != nil { + return fmt.Errorf("failed to update storage: %+v", err) + } + utils.Log.Infof("Storage with mount path [%s] has been disabled from CLI", mountPath) + fmt.Printf("Storage with mount path [%s] has been disabled\n", mountPath) + return nil + }, +} + +var deleteStorageCmd = &cobra.Command{ + Use: "delete [id]", + Short: "Delete a storage by id", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return fmt.Errorf("id is required") + } + id, err := strconv.Atoi(args[0]) + if err != nil { + return fmt.Errorf("id must be a number") + } + + if force, _ := cmd.Flags().GetBool("force"); force { + fmt.Printf("Are you sure you want to delete storage with id [%d]? [y/N]: ", id) + var confirm string + fmt.Scanln(&confirm) + if confirm != "y" && confirm != "Y" { + fmt.Println("Delete operation cancelled.") + return nil } } + + Init() + defer Release() + err = db.DeleteStorageById(uint(id)) + if err != nil { + return fmt.Errorf("failed to delete storage by id: %+v", err) + } + utils.Log.Infof("Storage with id [%d] have been deleted from CLI", id) + fmt.Printf("Storage with id [%d] have been deleted\n", id) return nil }, } @@ -152,6 +186,8 @@ func init() { storageCmd.AddCommand(disableStorageCmd) storageCmd.AddCommand(listStorageCmd) storageCmd.PersistentFlags().IntVarP(&storageTableHeight, "height", "H", 10, "Table height") + storageCmd.AddCommand(deleteStorageCmd) + deleteStorageCmd.Flags().BoolP("force", "f", false, "Force delete without confirmation") // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command