mirror of
https://github.com/openziti/zrok.git
synced 2024-11-22 08:03:49 +01:00
'zrok mv' (#438)
This commit is contained in:
parent
492337ed8b
commit
f39a09efdf
54
cmd/zrok/mv.go
Normal file
54
cmd/zrok/mv.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -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))
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user