This commit is contained in:
Michael Quigley 2024-01-10 14:35:27 -05:00
parent 4b7f5df1ee
commit c743622462
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
7 changed files with 150 additions and 17 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/openziti/zrok/environment" "github.com/openziti/zrok/environment"
"github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/sdk/golang/sdk" "github.com/openziti/zrok/sdk/golang/sdk"
"github.com/openziti/zrok/tui" "github.com/openziti/zrok/tui"
"github.com/openziti/zrok/util/sync" "github.com/openziti/zrok/util/sync"
@ -81,11 +80,11 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
} }
}() }()
source, err := cmd.createTarget(sourceUrl, root) source, err := sync.TargetForURL(sourceUrl, root)
if err != nil { if err != nil {
tui.Error("error creating target", err) tui.Error("error creating target", err)
} }
target, err := cmd.createTarget(targetUrl, root) target, err := sync.TargetForURL(targetUrl, root)
if err != nil { if err != nil {
tui.Error("error creating target", err) tui.Error("error creating target", err)
} }
@ -96,16 +95,3 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
fmt.Println("copy complete!") fmt.Println("copy complete!")
} }
func (cmd *copyCommand) createTarget(url *url.URL, root env_core.Root) (sync.Target, error) {
switch url.Scheme {
case "file":
return sync.NewFilesystemTarget(&sync.FilesystemTargetConfig{Root: url.Path}), nil
case "zrok":
return sync.NewZrokTarget(&sync.ZrokTargetConfig{URL: url, Root: root})
default:
return sync.NewWebDAVTarget(&sync.WebDAVTargetConfig{URL: url, Username: "", Password: ""})
}
}

65
cmd/zrok/dir.go Normal file
View File

@ -0,0 +1,65 @@
package main
import (
"fmt"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/util"
"github.com/openziti/zrok/util/sync"
"github.com/spf13/cobra"
"net/url"
)
func init() {
rootCmd.AddCommand(newDirCommand().cmd)
}
type dirCommand struct {
cmd *cobra.Command
}
func newDirCommand() *dirCommand {
cmd := &cobra.Command{
Use: "dir <target>",
Short: "List the contents of <target> ('http://', 'zrok://', and 'file://' supported)",
Aliases: []string{"ls"},
Args: cobra.ExactArgs(1),
}
command := &dirCommand{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *dirCommand) 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)
}
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.String()), err)
}
objects, err := target.Dir("/")
if err != nil {
tui.Error("error listing directory", err)
}
for _, object := range objects {
if object.IsDir {
fmt.Printf("<dir> %-32s\n", object.Path)
} else {
fmt.Printf(" %-32s %-16s %s\n", object.Path, util.BytesToSize(object.Size), object.Modified.String())
}
}
fmt.Println()
}

View File

@ -52,6 +52,27 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
return t.tree, nil return t.tree, nil
} }
func (t *FilesystemTarget) Dir(path string) ([]*Object, error) {
des, err := os.ReadDir(t.cfg.Root)
if err != nil {
return nil, err
}
var objects []*Object
for _, de := range des {
fi, err := de.Info()
if err != nil {
return nil, err
}
objects = append(objects, &Object{
Path: de.Name(),
IsDir: de.IsDir(),
Size: fi.Size(),
Modified: fi.ModTime(),
})
}
return objects, nil
}
func (t *FilesystemTarget) Mkdir(path string) error { func (t *FilesystemTarget) Mkdir(path string) error {
return os.MkdirAll(filepath.Join(t.cfg.Root, path), os.ModePerm) return os.MkdirAll(filepath.Join(t.cfg.Root, path), os.ModePerm)
} }

View File

@ -16,6 +16,7 @@ type Object struct {
type Target interface { type Target interface {
Inventory() ([]*Object, error) Inventory() ([]*Object, error)
Dir(path string) ([]*Object, error)
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

23
util/sync/target.go Normal file
View File

@ -0,0 +1,23 @@
package sync
import (
"github.com/openziti/zrok/environment/env_core"
"github.com/pkg/errors"
"net/url"
)
func TargetForURL(url *url.URL, root env_core.Root) (Target, error) {
switch url.Scheme {
case "file":
return NewFilesystemTarget(&FilesystemTargetConfig{Root: url.Path}), nil
case "zrok":
return NewZrokTarget(&ZrokTargetConfig{URL: url, Root: root})
case "http", "https":
return NewWebDAVTarget(&WebDAVTargetConfig{URL: url, Username: "", Password: ""})
default:
return nil, errors.Errorf("unknown URL scheme '%v'", url.Scheme)
}
}

View File

@ -59,7 +59,25 @@ func (t *WebDAVTarget) Inventory() ([]*Object, error) {
IsDir: fi.IsDir, IsDir: fi.IsDir,
Size: fi.Size, Size: fi.Size,
Modified: fi.ModTime, Modified: fi.ModTime,
ETag: fi.ETag, })
}
}
return objects, nil
}
func (t *WebDAVTarget) Dir(path string) ([]*Object, error) {
fis, err := t.dc.Readdir(context.Background(), t.cfg.URL.Path, false)
if err != nil {
return nil, err
}
var objects []*Object
for _, fi := range fis {
if fi.Path != "/" {
objects = append(objects, &Object{
Path: filepath.Base(fi.Path),
IsDir: fi.IsDir,
Size: fi.Size,
Modified: fi.ModTime,
}) })
} }
} }

View File

@ -84,6 +84,25 @@ func (t *ZrokTarget) Inventory() ([]*Object, error) {
return objects, nil return objects, nil
} }
func (t *ZrokTarget) Dir(path string) ([]*Object, error) {
fis, err := t.dc.Readdir(context.Background(), t.cfg.URL.Path, false)
if err != nil {
return nil, err
}
var objects []*Object
for _, fi := range fis {
if fi.Path != "/" {
objects = append(objects, &Object{
Path: filepath.Base(fi.Path),
IsDir: fi.IsDir,
Size: fi.Size,
Modified: fi.ModTime,
})
}
}
return objects, nil
}
func (t *ZrokTarget) Mkdir(path string) error { func (t *ZrokTarget) Mkdir(path string) error {
return t.dc.Mkdir(context.Background(), filepath.Join(t.cfg.URL.Path, path)) return t.dc.Mkdir(context.Background(), filepath.Join(t.cfg.URL.Path, path))
} }