diff --git a/cmd/zrok/copy.go b/cmd/zrok/copy.go index 3995de97..ddd359f5 100644 --- a/cmd/zrok/copy.go +++ b/cmd/zrok/copy.go @@ -35,7 +35,7 @@ func newCopyCommand() *copyCommand { func (cmd *copyCommand) run(_ *cobra.Command, args []string) { sourceUrl, err := url.Parse(args[0]) if err != nil { - tui.Error(fmt.Sprintf("invalid source URL '%v'", args[0]), err) + tui.Error(fmt.Sprintf("invalid source '%v'", args[0]), err) } if sourceUrl.Scheme == "" { sourceUrl.Scheme = "file" @@ -47,7 +47,7 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) { } targetUrl, err := url.Parse(targetStr) if err != nil { - tui.Error(fmt.Sprintf("invalid target URL '%v'", targetStr), err) + tui.Error(fmt.Sprintf("invalid target '%v'", targetStr), err) } if targetUrl.Scheme == "" { targetUrl.Scheme = "file" @@ -84,11 +84,11 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) { source, err := sync.TargetForURL(sourceUrl, root) if err != nil { - tui.Error("error creating target", err) + tui.Error(fmt.Sprintf("error creating target for '%v'", sourceUrl), err) } target, err := sync.TargetForURL(targetUrl, root) if err != nil { - tui.Error("error creating target", err) + tui.Error(fmt.Sprintf("error creating target for '%v'", targetUrl), err) } if err := sync.OneWay(source, target, cmd.sync); err != nil { diff --git a/cmd/zrok/ls.go b/cmd/zrok/ls.go index 737fb941..aa3d287c 100644 --- a/cmd/zrok/ls.go +++ b/cmd/zrok/ls.go @@ -14,29 +14,29 @@ import ( ) func init() { - rootCmd.AddCommand(newDirCommand().cmd) + rootCmd.AddCommand(newLsCommand().cmd) } -type dirCommand struct { +type lsCommand struct { cmd *cobra.Command } -func newDirCommand() *dirCommand { +func newLsCommand() *lsCommand { cmd := &cobra.Command{ Use: "ls ", - Short: "List the contents of drive ('http://', 'zrok://', and 'file://' supported)", + Short: "List the contents of drive ('http://', 'zrok://','file://')", Aliases: []string{"dir"}, Args: cobra.ExactArgs(1), } - command := &dirCommand{cmd: cmd} + command := &lsCommand{cmd: cmd} cmd.Run = command.run return command } -func (cmd *dirCommand) run(_ *cobra.Command, args []string) { +func (cmd *lsCommand) run(_ *cobra.Command, args []string) { targetUrl, err := url.Parse(args[0]) if err != nil { - tui.Error(fmt.Sprintf("invalid target URL '%v'", args[0]), err) + tui.Error(fmt.Sprintf("invalid target '%v'", args[0]), err) } if targetUrl.Scheme == "" { targetUrl.Scheme = "file" @@ -49,7 +49,7 @@ func (cmd *dirCommand) run(_ *cobra.Command, args []string) { target, err := sync.TargetForURL(targetUrl, root) if err != nil { - tui.Error(fmt.Sprintf("error creating target for '%v'", targetUrl.String()), err) + tui.Error(fmt.Sprintf("error creating target for '%v'", targetUrl), err) } objects, err := target.Dir("/") diff --git a/cmd/zrok/rm.go b/cmd/zrok/rm.go new file mode 100644 index 00000000..692d6ac0 --- /dev/null +++ b/cmd/zrok/rm.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(newRmCommand().cmd) +} + +type rmCommand struct { + cmd *cobra.Command +} + +func newRmCommand() *rmCommand { + cmd := &cobra.Command{ + Use: "rm ", + Short: "Remove (delete) the contents of drive ('http://', 'zrok://', 'file://')", + Aliases: []string{"del"}, + Args: cobra.ExactArgs(1), + } + command := &rmCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *rmCommand) 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.Rm("/"); err != nil { + tui.Error("error removing", err) + } +} diff --git a/drives/sync/filesystem.go b/drives/sync/filesystem.go index 68721b41..28f30297 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) Rm(path string) error { + return os.RemoveAll(filepath.Join(t.cfg.Root, path)) +} + func (t *FilesystemTarget) SetModificationTime(path string, mtime time.Time) error { targetPath := filepath.Join(t.cfg.Root, path) if err := os.Chtimes(targetPath, time.Now(), mtime); err != nil { diff --git a/drives/sync/model.go b/drives/sync/model.go index 34b8306e..499c4d97 100644 --- a/drives/sync/model.go +++ b/drives/sync/model.go @@ -20,5 +20,6 @@ type Target interface { Mkdir(path string) error ReadStream(path string) (io.ReadCloser, error) WriteStream(path string, stream io.Reader, mode os.FileMode) 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 8d53a4cc..f5a2bdb5 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) Rm(path string) error { + return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path)) +} + func (t *WebDAVTarget) SetModificationTime(path string, mtime time.Time) error { return t.dc.Touch(context.Background(), filepath.Join(t.cfg.URL.Path, path), mtime) } diff --git a/drives/sync/zrok.go b/drives/sync/zrok.go index 4d036e0e..be1d129c 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) Rm(path string) error { + return t.dc.RemoveAll(context.Background(), filepath.Join(t.cfg.URL.Path, path)) +} + func (t *ZrokTarget) SetModificationTime(path string, mtime time.Time) error { return t.dc.Touch(context.Background(), filepath.Join(t.cfg.URL.Path, path), mtime) }