diff --git a/cmd/aem/repo.go b/cmd/aem/repo.go index cd25b38c..bc48577b 100644 --- a/cmd/aem/repo.go +++ b/cmd/aem/repo.go @@ -23,14 +23,15 @@ func (c *CLI) repoNodeCmd() *cobra.Command { Use: "node", Short: "CRUD operations on JCR repository", } - cmd.AddCommand(c.repoNodeRead()) - cmd.AddCommand(c.repoNodeSave()) - cmd.AddCommand(c.repoNodeDelete()) + cmd.AddCommand(c.repoNodeReadCmd()) + cmd.AddCommand(c.repoNodeSaveCmd()) + cmd.AddCommand(c.repoNodeDeleteCmd()) + cmd.AddCommand(c.repoNodeChildrenCmd()) return cmd } -func (c *CLI) repoNodeRead() *cobra.Command { +func (c *CLI) repoNodeReadCmd() *cobra.Command { cmd := &cobra.Command{ Use: "read", Short: "Read node", @@ -50,7 +51,32 @@ func (c *CLI) repoNodeRead() *cobra.Command { return cmd } -func (c *CLI) repoNodeSave() *cobra.Command { +func (c *CLI) repoNodeChildrenCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "children", + Short: "Read node children", + Aliases: []string{"ls"}, + Run: func(cmd *cobra.Command, args []string) { + instance, err := c.aem.InstanceManager().One() + if err != nil { + c.Error(err) + return + } + node := repoNodeByFlags(cmd, *instance) + children, err := node.Children() + if err != nil { + c.Error(err) + return + } + c.SetOutput("children", children) + c.Ok("node children read") + }, + } + repoNodeDefineFlags(cmd) + return cmd +} + +func (c *CLI) repoNodeSaveCmd() *cobra.Command { cmd := &cobra.Command{ Use: "save", Short: "Create or update node", @@ -95,7 +121,7 @@ func (c *CLI) repoNodeSave() *cobra.Command { return cmd } -func (c *CLI) repoNodeDelete() *cobra.Command { +func (c *CLI) repoNodeDeleteCmd() *cobra.Command { cmd := &cobra.Command{ Use: "delete", Short: "Delete node", diff --git a/pkg/repo_node.go b/pkg/repo_node.go index 86434bc5..c5c34637 100644 --- a/pkg/repo_node.go +++ b/pkg/repo_node.go @@ -9,6 +9,7 @@ import ( "github.com/wttech/aemc/pkg/common/langx" "github.com/wttech/aemc/pkg/common/stringsx" "golang.org/x/exp/maps" + "strings" ) // RepoNode represents single node in JCR repository @@ -80,7 +81,7 @@ func (n RepoNode) Breadcrumb() []RepoNode { } func (n RepoNode) Child(name string) RepoNode { - return NewNode(n.repo, fmt.Sprintf("%s/%s", n.path, name)) + return NewNode(n.repo, fmt.Sprintf("%s/%s", strings.TrimPrefix(n.path, "/"), name)) } func (n RepoNode) Children() ([]RepoNode, error) { @@ -95,7 +96,8 @@ func (n RepoNode) Children() ([]RepoNode, error) { if err != nil { return nil, fmt.Errorf("cannot parse children of node '%s': %w", n.path, err) } - return lo.Map(children.Children, func(child nodeArrayChild, _ int) RepoNode { return n.Child(child.Name) }), nil + childrenWithType := lo.Filter(children.Children, func(c nodeArrayChild, _ int) bool { return c.PrimaryType != "" }) + return lo.Map(childrenWithType, func(child nodeArrayChild, _ int) RepoNode { return n.Child(child.Name) }), nil } type nodeArrayChildren struct { @@ -103,7 +105,8 @@ type nodeArrayChildren struct { } type nodeArrayChild struct { - Name string `json:"__name__"` + Name string `json:"__name__"` + PrimaryType string `json:"jcr:primaryType,omitempty"` } func (n RepoNode) Siblings() ([]RepoNode, error) {