'zrok md' (#438)

This commit is contained in:
Michael Quigley 2024-01-11 13:04:27 -05:00
parent 21a470d60b
commit 492337ed8b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

54
cmd/zrok/md.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(newMdCommand().cmd)
}
type mdCommand struct {
cmd *cobra.Command
}
func newMdCommand() *mdCommand {
cmd := &cobra.Command{
Use: "md <target>",
Short: "Make directory at <target> ('http://', 'zrok://', 'file://')",
Aliases: []string{"mkdir"},
Args: cobra.ExactArgs(1),
}
command := &mdCommand{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *mdCommand) 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.Mkdir("/"); err != nil {
tui.Error("error creating directory", err)
}
}