Files
phylum/server/internal/command/trash/delete.go
2025-04-10 17:27:18 +05:30

50 lines
1.2 KiB
Go

package trash
import (
"fmt"
"os"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/command/common"
"github.com/spf13/cobra"
)
func setupDeleteCommand() *cobra.Command {
cmd := cobra.Command{
Use: "delete uuid",
Short: "Permanently delete resource from trash",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := common.UserFileSystem(cmd)
for _, idStr := range args {
id, err := uuid.Parse(idStr)
if err != nil {
fmt.Println("cannot remove '" + idStr + "': " + err.Error())
os.Exit(1)
}
r, err := f.ResourceByID(id)
if err != nil {
fmt.Println("cannot remove '" + id.String() + "': " + err.Error())
os.Exit(1)
}
if r.Dir() {
if recursive, _ := cmd.Flags().GetBool("recursive"); !recursive {
fmt.Println("cannot remove '" + id.String() + "': Is a directory. Try again with '-r'")
os.Exit(1)
}
}
if _, err := r.DeleteRecursive(false); err != nil {
fmt.Println("cannot remove '" + id.String() + "': " + err.Error())
os.Exit(1)
}
}
},
}
cmd.Flags().BoolP("recursive", "r", false, "Recursive (required for directories)")
return &cmd
}