'zrok rm' (#438); lint removal and messaging cleanup

This commit is contained in:
Michael Quigley 2024-01-11 12:11:21 -05:00
parent fcb156d650
commit 21a470d60b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
7 changed files with 79 additions and 12 deletions

View File

@ -35,7 +35,7 @@ func newCopyCommand() *copyCommand {
func (cmd *copyCommand) run(_ *cobra.Command, args []string) { func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
sourceUrl, err := url.Parse(args[0]) sourceUrl, err := url.Parse(args[0])
if err != nil { 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 == "" { if sourceUrl.Scheme == "" {
sourceUrl.Scheme = "file" sourceUrl.Scheme = "file"
@ -47,7 +47,7 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
} }
targetUrl, err := url.Parse(targetStr) targetUrl, err := url.Parse(targetStr)
if err != nil { 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 == "" { if targetUrl.Scheme == "" {
targetUrl.Scheme = "file" targetUrl.Scheme = "file"
@ -84,11 +84,11 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
source, err := sync.TargetForURL(sourceUrl, root) source, err := sync.TargetForURL(sourceUrl, root)
if err != nil { 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) target, err := sync.TargetForURL(targetUrl, root)
if err != nil { 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 { if err := sync.OneWay(source, target, cmd.sync); err != nil {

View File

@ -14,29 +14,29 @@ import (
) )
func init() { func init() {
rootCmd.AddCommand(newDirCommand().cmd) rootCmd.AddCommand(newLsCommand().cmd)
} }
type dirCommand struct { type lsCommand struct {
cmd *cobra.Command cmd *cobra.Command
} }
func newDirCommand() *dirCommand { func newLsCommand() *lsCommand {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "ls <target>", Use: "ls <target>",
Short: "List the contents of drive <target> ('http://', 'zrok://', and 'file://' supported)", Short: "List the contents of drive <target> ('http://', 'zrok://','file://')",
Aliases: []string{"dir"}, Aliases: []string{"dir"},
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
} }
command := &dirCommand{cmd: cmd} command := &lsCommand{cmd: cmd}
cmd.Run = command.run cmd.Run = command.run
return command return command
} }
func (cmd *dirCommand) run(_ *cobra.Command, args []string) { func (cmd *lsCommand) run(_ *cobra.Command, args []string) {
targetUrl, err := url.Parse(args[0]) targetUrl, err := url.Parse(args[0])
if err != nil { 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 == "" { if targetUrl.Scheme == "" {
targetUrl.Scheme = "file" targetUrl.Scheme = "file"
@ -49,7 +49,7 @@ func (cmd *dirCommand) run(_ *cobra.Command, args []string) {
target, err := sync.TargetForURL(targetUrl, root) target, err := sync.TargetForURL(targetUrl, root)
if err != nil { 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("/") objects, err := target.Dir("/")

54
cmd/zrok/rm.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(newRmCommand().cmd)
}
type rmCommand struct {
cmd *cobra.Command
}
func newRmCommand() *rmCommand {
cmd := &cobra.Command{
Use: "rm <target>",
Short: "Remove (delete) the contents of drive <target> ('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)
}
}

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) Rm(path string) error {
return os.RemoveAll(filepath.Join(t.cfg.Root, path))
}
func (t *FilesystemTarget) SetModificationTime(path string, mtime time.Time) error { func (t *FilesystemTarget) SetModificationTime(path string, mtime time.Time) error {
targetPath := filepath.Join(t.cfg.Root, path) targetPath := filepath.Join(t.cfg.Root, path)
if err := os.Chtimes(targetPath, time.Now(), mtime); err != nil { if err := os.Chtimes(targetPath, time.Now(), mtime); err != nil {

View File

@ -20,5 +20,6 @@ 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
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) 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 { func (t *WebDAVTarget) SetModificationTime(path string, mtime time.Time) error {
return t.dc.Touch(context.Background(), filepath.Join(t.cfg.URL.Path, path), mtime) return t.dc.Touch(context.Background(), filepath.Join(t.cfg.URL.Path, path), mtime)
} }

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) 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 { func (t *ZrokTarget) SetModificationTime(path string, mtime time.Time) error {
return t.dc.Touch(context.Background(), filepath.Join(t.cfg.URL.Path, path), mtime) return t.dc.Touch(context.Background(), filepath.Join(t.cfg.URL.Path, path), mtime)
} }