diff --git a/server/internal/command/fs/cmd.go b/server/internal/command/fs/cmd.go index dddabdf1..21254c13 100644 --- a/server/internal/command/fs/cmd.go +++ b/server/internal/command/fs/cmd.go @@ -23,6 +23,7 @@ func SetupCommand() *cobra.Command { setupCatCommand(), setupChpermCommand(), setupCpCommand(), + setupImportCommand(), setupLsCommand(), setupMkdirCommand(), setupMvCommand(), diff --git a/server/internal/command/fs/import.go b/server/internal/command/fs/import.go new file mode 100644 index 00000000..f01137a7 --- /dev/null +++ b/server/internal/command/fs/import.go @@ -0,0 +1,78 @@ +package fs + +import ( + "errors" + "fmt" + "io" + "os" + + "github.com/google/uuid" + "github.com/shroff/phylum/server/internal/command/common" + "github.com/shroff/phylum/server/internal/core/fs" + "github.com/spf13/cobra" +) + +func setupImportCommand() *cobra.Command { + cmd := cobra.Command{ + Use: "import ( | ) []", + Short: "Import from filesystem", + Args: cobra.RangeArgs(2, 3), + Run: func(cmd *cobra.Command, args []string) { + f := common.UserFileSystem(cmd) + target, err := f.ResourceByPathOrUUID(args[0]) + if err != nil { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + os.Exit(1) + } + + stat, err := os.Stat(args[1]) + if err != nil { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + os.Exit(1) + } + name := stat.Name() + if len(args) > 2 { + name = args[2] + } + + force, _ := cmd.Flags().GetBool("force") + if force { + f.DeleteChildRecursive(target, name, true) + } else { + _, err := f.WithRoot(target.ID).ResourceByPath(name) + if err == nil { + fmt.Println("could not import '" + args[1] + "': resource with name '" + name + "' already exist. use -f to overwrite") + os.Exit(1) + } else if !errors.Is(err, fs.ErrResourceNotFound) { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + os.Exit(1) + } + } + if stat.IsDir() { + + } else { + in, err := os.Open(args[1]) + if err != nil { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + } + defer in.Close() + id, _ := uuid.NewUUID() + if r, err := f.CreateMemberResource(target, id, name, false); err != nil { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + } else { + out, err := f.OpenWrite(r) + if err != nil { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + } + defer out.Close() + _, err = io.Copy(out, in) + if err != nil { + fmt.Println("could not import '" + args[1] + "': " + err.Error()) + } + } + } + }, + } + cmd.Flags().BoolP("force", "f", false, "Overwrite destination if it exists") + return &cmd +} diff --git a/server/internal/core/fs/delete.go b/server/internal/core/fs/delete.go index 7de1a76a..232d6f03 100644 --- a/server/internal/core/fs/delete.go +++ b/server/internal/core/fs/delete.go @@ -15,6 +15,7 @@ func (f filesystem) DeleteChildRecursive(r Resource, name string, hardDelete boo } return err } else { + // TODO: #incorrectdata inherited permissions should include r.Permisisons child := ResourceFromDB(result, r.UserPermissions, r.Path+"/"+result.Name, r.InheritedPermissions) return f.DeleteRecursive(child, hardDelete) }