[server][cmd] Trash delete, and hard delete directly from fs rm

This commit is contained in:
Abhishek Shroff
2025-03-19 14:58:12 +05:30
parent bbb77037be
commit 666c06ff38
4 changed files with 59 additions and 6 deletions
+3 -1
View File
@@ -29,10 +29,12 @@ func setupRmCommand() *cobra.Command {
}
}
r.DeleteRecursive(true)
hard, _ := cmd.Flags().GetBool("hard")
r.DeleteRecursive(!hard)
},
}
cmd.Flags().BoolP("recursive", "r", false, "Recursive (required for directories)")
cmd.Flags().BoolP("hard", "x", false, "Permanently delete (do not send to trash)")
return &cmd
}
+1
View File
@@ -21,6 +21,7 @@ func SetupCommand() *cobra.Command {
flags.StringP("user", "u", "phylum", "User")
cmd.AddCommand(
setupDeleteCommand(),
setupEmptyCommand(),
setupListCommand(),
setupRestoreCommand(),
+44
View File
@@ -0,0 +1,44 @@
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.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := common.UserFileSystem(cmd)
idStr := args[0]
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)
}
}
r.DeleteRecursive(false)
},
}
cmd.Flags().BoolP("recursive", "r", false, "Recursive (required for directories)")
return &cmd
}
+11 -5
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/command/common"
"github.com/spf13/cobra"
)
@@ -15,15 +16,20 @@ func setupRestoreCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := common.UserFileSystem(cmd)
for _, uuid := range args {
r, err := f.ResourceByPathOrUUID(uuid)
for _, idStr := range args {
id, err := uuid.Parse(idStr)
if err != nil {
fmt.Println("cannot restore'" + uuid + "': " + err.Error())
fmt.Println("cannot remove '" + idStr + "': " + err.Error())
os.Exit(1)
}
r, err := f.ResourceByID(id)
if err != nil {
fmt.Println("cannot restore'" + id.String() + "': " + err.Error())
os.Exit(1)
}
if r.Dir() {
if recursive, err := cmd.Flags().GetBool("recursive"); err != nil || !recursive {
fmt.Println("cannot restore '" + uuid + "': Is a directory. Try again with '-r'")
fmt.Println("cannot restore '" + id.String() + "': Is a directory. Try again with '-r'")
os.Exit(1)
}
}
@@ -32,7 +38,7 @@ func setupRestoreCommand() *cobra.Command {
autoRename, _ := cmd.Flags().GetBool("auto-rename")
r, items, size, err := r.Restore(p, name, autoRename)
if err != nil {
fmt.Println("cannot restore '" + uuid + "': " + err.Error())
fmt.Println("cannot restore '" + id.String() + "': " + err.Error())
os.Exit(1)
}