mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-04 10:39:47 -06:00
154 lines
3.8 KiB
Go
154 lines
3.8 KiB
Go
package fs
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
iofs "io/fs"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/shroff/phylum/server/internal/command/common"
|
|
"github.com/shroff/phylum/server/internal/core"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func setupImportCommand() *cobra.Command {
|
|
cmd := cobra.Command{
|
|
Use: "import <fs-path> <target> [name]",
|
|
Short: "Import from filesystem",
|
|
Args: cobra.RangeArgs(2, 3),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
f := common.UserFileSystem(cmd)
|
|
importPath := args[0]
|
|
|
|
stat, err := os.Stat(importPath)
|
|
if err != nil {
|
|
fmt.Println("unable to stat '" + importPath + "': " + err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
destName := stat.Name()
|
|
if len(args) > 2 {
|
|
destName = args[2]
|
|
if core.CheckResourceNameInvalid(destName) {
|
|
fmt.Println("invalid name: '" + destName + "'")
|
|
}
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
if recursive, _ := cmd.Flags().GetBool("recursive"); !recursive {
|
|
fmt.Println("could not import '" + importPath + "': is a directory. use -r to import")
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
|
|
var size int = 0
|
|
create := make([]core.CreateResourcesParams, 0)
|
|
copy := make(map[string]uuid.UUID)
|
|
ids := make(map[string]uuid.UUID)
|
|
targetRootID, _ := uuid.NewV7()
|
|
ids["."] = targetRootID
|
|
|
|
if stat.IsDir() {
|
|
dirFS := os.DirFS(importPath)
|
|
|
|
err := iofs.WalkDir(dirFS, ".", func(p string, d iofs.DirEntry, err error) error {
|
|
if p != "." && err == nil {
|
|
info, err := d.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
len := int(info.Size())
|
|
if d.IsDir() {
|
|
len = 0
|
|
}
|
|
size += len
|
|
if core.CheckResourceNameInvalid(d.Name()) {
|
|
return core.ErrResourceNameInvalid
|
|
}
|
|
ids[p], _ = uuid.NewV7()
|
|
parent := ids[path.Dir(p)]
|
|
create = append(create, core.CreateResourcesParams{
|
|
Parent: parent,
|
|
ID: ids[p],
|
|
Name: d.Name(),
|
|
Dir: d.IsDir(),
|
|
})
|
|
if !d.IsDir() {
|
|
copy[p] = ids[p]
|
|
}
|
|
|
|
}
|
|
return err
|
|
})
|
|
if err != nil {
|
|
fmt.Println("could not import '" + importPath + "': " + err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
fmt.Printf("Importing %d files (%d bytes) across %d dirs\n", len(copy), size, 1+len(create)-len(copy))
|
|
|
|
var conflictResolution core.ResourceBindConflictResolution = core.ResourceBindConflictResolutionError
|
|
if force {
|
|
conflictResolution = core.ResourceBindConflictResolutionDelete
|
|
}
|
|
err = f.RunInTx(func(f core.FileSystem) error {
|
|
if _, err = f.CreateResourceByPath(args[1]+"/"+destName, targetRootID, stat.IsDir(), false, conflictResolution); err != nil {
|
|
if err == core.ErrResourceNameConflict {
|
|
err = errors.New("resource with name '" + destName + "' already exist. use -f to overwrite")
|
|
}
|
|
return err
|
|
}
|
|
_, err = f.CreateResources(create)
|
|
return err
|
|
})
|
|
|
|
if err == nil {
|
|
err = func() error {
|
|
for k, v := range copy {
|
|
if err := copyContents(f, path.Join(importPath, k), v); err != nil {
|
|
return errors.New("unable to copy " + k + " to " + v.String() + ": " + err.Error())
|
|
}
|
|
}
|
|
return nil
|
|
}()
|
|
}
|
|
if err != nil {
|
|
fmt.Println("could not import '" + importPath + "': " + err.Error())
|
|
os.Exit(1)
|
|
}
|
|
},
|
|
}
|
|
cmd.Flags().BoolP("force", "f", false, "Overwrite destination if it exists")
|
|
cmd.Flags().BoolP("recursive", "r", false, "Recursive import")
|
|
return &cmd
|
|
}
|
|
|
|
func copyContents(f core.FileSystem, src string, id uuid.UUID) error {
|
|
fmt.Println("importing " + src + " to " + id.String())
|
|
in, err := os.Open(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer in.Close()
|
|
if r, err := f.ResourceByID(id); err != nil {
|
|
return err
|
|
} else {
|
|
out, err := f.OpenWrite(r, uuid.Nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer out.Close()
|
|
_, err = io.Copy(out, in)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|