mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 20:26:26 +08:00
feat(cmd): add delete command for storage (#952)
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/OpenListTeam/OpenList/v4/internal/db"
|
"github.com/OpenListTeam/OpenList/v4/internal/db"
|
||||||
|
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
|
||||||
"github.com/charmbracelet/bubbles/table"
|
"github.com/charmbracelet/bubbles/table"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
@ -22,8 +23,8 @@ var storageCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var disableStorageCmd = &cobra.Command{
|
var disableStorageCmd = &cobra.Command{
|
||||||
Use: "disable",
|
Use: "disable [mount path]",
|
||||||
Short: "Disable a storage",
|
Short: "Disable a storage by mount path",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return fmt.Errorf("mount path is required")
|
return fmt.Errorf("mount path is required")
|
||||||
@ -34,15 +35,48 @@ var disableStorageCmd = &cobra.Command{
|
|||||||
storage, err := db.GetStorageByMountPath(mountPath)
|
storage, err := db.GetStorageByMountPath(mountPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to query storage: %+v", err)
|
return fmt.Errorf("failed to query storage: %+v", err)
|
||||||
} else {
|
}
|
||||||
storage.Disabled = true
|
storage.Disabled = true
|
||||||
err = db.UpdateStorage(storage)
|
err = db.UpdateStorage(storage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to update storage: %+v", err)
|
return fmt.Errorf("failed to update storage: %+v", err)
|
||||||
} else {
|
}
|
||||||
fmt.Printf("Storage with mount path [%s] have been disabled\n", mountPath)
|
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
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -152,6 +186,8 @@ func init() {
|
|||||||
storageCmd.AddCommand(disableStorageCmd)
|
storageCmd.AddCommand(disableStorageCmd)
|
||||||
storageCmd.AddCommand(listStorageCmd)
|
storageCmd.AddCommand(listStorageCmd)
|
||||||
storageCmd.PersistentFlags().IntVarP(&storageTableHeight, "height", "H", 10, "Table height")
|
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.
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
Reference in New Issue
Block a user