Files
phylum/server/internal/command/fs/mkdir.go
T
2024-10-20 10:33:14 +05:30

44 lines
1.1 KiB
Go

package fs
import (
"strings"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/command/common"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func setupMkdirCommand() *cobra.Command {
cmd := cobra.Command{
Use: "mkdir <path>",
Short: "Create Directory",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := common.UserFileSystem(cmd)
path := args[0]
if _, err := f.ResourceByPath(path); err == nil {
logrus.Fatal("Resource already exists: " + path)
}
// TODO: Streamline with WebDAV mkcol request handling
path = strings.TrimRight(path, "/")
index := strings.LastIndex(path, "/")
parentPath := path[0:index]
parent, err := f.ResourceByPath(parentPath)
if err != nil {
logrus.Fatal("Parent resource does not exist: " + parentPath)
}
id := uuid.New()
name := path[index+1:]
if _, err = f.CreateMemberResource(parent, id, name, true); err != nil {
logrus.Fatal(err)
} else {
logrus.Info("Created directory " + path + " (" + id.String() + ")")
}
},
}
return &cmd
}