Skip to content

Commit

Permalink
'zrok mv' (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Jan 11, 2024
1 parent 492337e commit f39a09e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/zrok/mv.go
Original file line number Diff line number Diff line change
@@ -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 <target> <newPath>",
Short: "Move the drive <target> to <newPath> ('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)
}
}
4 changes: 4 additions & 0 deletions drives/sync/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
1 change: 1 addition & 0 deletions drives/sync/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions drives/sync/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
4 changes: 4 additions & 0 deletions drives/sync/zrok.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit f39a09e

Please sign in to comment.