'zrok mv' (#438)

This commit is contained in:
Michael Quigley 2024-01-11 13:19:20 -05:00
parent 492337ed8b
commit f39a09efdf
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 67 additions and 0 deletions

54
cmd/zrok/mv.go Normal file
View File

@ -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)
}
}

View File

@ -131,6 +131,10 @@ func (t *FilesystemTarget) WriteStream(path string, stream io.Reader, mode os.Fi
return nil 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 { func (t *FilesystemTarget) Rm(path string) error {
return os.RemoveAll(filepath.Join(t.cfg.Root, path)) return os.RemoveAll(filepath.Join(t.cfg.Root, path))
} }

View File

@ -20,6 +20,7 @@ type Target interface {
Mkdir(path string) error Mkdir(path string) error
ReadStream(path string) (io.ReadCloser, error) ReadStream(path string) (io.ReadCloser, error)
WriteStream(path string, stream io.Reader, mode os.FileMode) error WriteStream(path string, stream io.Reader, mode os.FileMode) error
Move(src, dest string) error
Rm(path string) error Rm(path string) error
SetModificationTime(path string, mtime time.Time) error SetModificationTime(path string, mtime time.Time) error
} }

View File

@ -113,6 +113,10 @@ func (t *WebDAVTarget) WriteStream(path string, rs io.Reader, _ os.FileMode) err
return nil 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 { func (t *WebDAVTarget) Rm(path string) error {
return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path)) return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path))
} }

View File

@ -132,6 +132,10 @@ func (t *ZrokTarget) WriteStream(path string, rs io.Reader, _ os.FileMode) error
return nil 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 { func (t *ZrokTarget) Rm(path string) error {
return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path)) return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path))
} }