From f39a09efdf0fcf5c28b7c7c674bdda3164b3a34b Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 11 Jan 2024 13:19:20 -0500 Subject: [PATCH] 'zrok mv' (#438) --- cmd/zrok/mv.go | 54 +++++++++++++++++++++++++++++++++++++++ drives/sync/filesystem.go | 4 +++ drives/sync/model.go | 1 + drives/sync/webdav.go | 4 +++ drives/sync/zrok.go | 4 +++ 5 files changed, 67 insertions(+) create mode 100644 cmd/zrok/mv.go diff --git a/cmd/zrok/mv.go b/cmd/zrok/mv.go new file mode 100644 index 00000000..c30e52d9 --- /dev/null +++ b/cmd/zrok/mv.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "github.com/openziti/zrok/drives/sync" + "github.com/openziti/zrok/environment" + "github.com/openziti/zrok/tui" + "github.com/spf13/cobra" + "net/url" +) + +func init() { + rootCmd.AddCommand(newMvCommand().cmd) +} + +type mvCommand struct { + cmd *cobra.Command +} + +func newMvCommand() *mvCommand { + cmd := &cobra.Command{ + Use: "mv ", + Short: "Move the drive to ('http://', 'zrok://', 'file://')", + Aliases: []string{"move"}, + Args: cobra.ExactArgs(2), + } + command := &mvCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *mvCommand) run(_ *cobra.Command, args []string) { + targetUrl, err := url.Parse(args[0]) + if err != nil { + tui.Error(fmt.Sprintf("invalid target '%v'", args[0]), err) + } + if targetUrl.Scheme == "" { + targetUrl.Scheme = "file" + } + + root, err := environment.LoadRoot() + if err != nil { + tui.Error("error loading root", err) + } + + target, err := sync.TargetForURL(targetUrl, root) + if err != nil { + tui.Error(fmt.Sprintf("error creating target for '%v'", targetUrl), err) + } + + if err := target.Move("/", args[1]); err != nil { + tui.Error("error moving", err) + } +} diff --git a/drives/sync/filesystem.go b/drives/sync/filesystem.go index 28f30297..cde7271e 100644 --- a/drives/sync/filesystem.go +++ b/drives/sync/filesystem.go @@ -131,6 +131,10 @@ func (t *FilesystemTarget) WriteStream(path string, stream io.Reader, mode os.Fi return nil } +func (t *FilesystemTarget) Move(src, dest string) error { + return os.Rename(filepath.Join(t.cfg.Root, src), filepath.Join(filepath.Dir(t.cfg.Root), dest)) +} + func (t *FilesystemTarget) Rm(path string) error { return os.RemoveAll(filepath.Join(t.cfg.Root, path)) } diff --git a/drives/sync/model.go b/drives/sync/model.go index 499c4d97..be0bd422 100644 --- a/drives/sync/model.go +++ b/drives/sync/model.go @@ -20,6 +20,7 @@ type Target interface { Mkdir(path string) error ReadStream(path string) (io.ReadCloser, error) WriteStream(path string, stream io.Reader, mode os.FileMode) error + Move(src, dest string) error Rm(path string) error SetModificationTime(path string, mtime time.Time) error } diff --git a/drives/sync/webdav.go b/drives/sync/webdav.go index f5a2bdb5..3f76ef16 100644 --- a/drives/sync/webdav.go +++ b/drives/sync/webdav.go @@ -113,6 +113,10 @@ func (t *WebDAVTarget) WriteStream(path string, rs io.Reader, _ os.FileMode) err return nil } +func (t *WebDAVTarget) Move(src, dest string) error { + return t.dc.MoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, src), dest, true) +} + func (t *WebDAVTarget) Rm(path string) error { return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path)) } diff --git a/drives/sync/zrok.go b/drives/sync/zrok.go index be1d129c..48d78ff5 100644 --- a/drives/sync/zrok.go +++ b/drives/sync/zrok.go @@ -132,6 +132,10 @@ func (t *ZrokTarget) WriteStream(path string, rs io.Reader, _ os.FileMode) error return nil } +func (t *ZrokTarget) Move(src, dest string) error { + return t.dc.MoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, src), dest, true) +} + func (t *ZrokTarget) Rm(path string) error { return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path)) }