Skip to content

Commit

Permalink
Merge pull request #58 from wttech/node-children-cmd
Browse files Browse the repository at this point in the history
Node children command
  • Loading branch information
krystian-panek-vmltech authored Feb 8, 2023
2 parents 11c2664 + a76480d commit 85806cc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
38 changes: 32 additions & 6 deletions cmd/aem/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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", pkg.NewRepoNodeList(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",
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions pkg/repo.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package pkg

import (
"bytes"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/wttech/aemc/pkg/common/fmtx"
"github.com/wttech/aemc/pkg/common/mapsx"
"github.com/wttech/aemc/pkg/repo"
"net/http"
"reflect"
"sort"
"strings"
)

// Repo Facade for communicating with JCR repository.
Expand Down Expand Up @@ -140,3 +144,25 @@ func (r Repo) handleResponse(action string, resp *resty.Response, err error) err
}
return nil
}

func NewRepoNodeList(nodes []RepoNode) NodeList {
var sortedNodes []RepoNode
sortedNodes = append(sortedNodes, nodes...)
sort.SliceStable(sortedNodes, func(i, j int) bool { return strings.Compare(nodes[i].Name(), nodes[j].Name()) < 0 })
return NodeList{Nodes: sortedNodes, Total: len(nodes)}
}

type NodeList struct {
Total int `json:"total" yaml:"total"`
Nodes []RepoNode `json:"nodes" yaml:"nodes"`
}

func (nl NodeList) MarshalText() string {
bs := bytes.NewBufferString("")
bs.WriteString(fmtx.TblMap("stats", "stat", "value", map[string]any{"total": len(nl.Nodes)}))
bs.WriteString("\n")
bs.WriteString(fmtx.TblRows("list", true, []string{"path"}, lo.Map(nl.Nodes, func(node RepoNode, _ int) map[string]any {
return map[string]any{"path": node.path}
})))
return bs.String()
}
9 changes: 6 additions & 3 deletions pkg/repo_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +22,7 @@ type RepoNode struct {
func NewNode(repo Repo, path string) RepoNode {
return RepoNode{
repo: repo,
path: path,
path: "/" + strings.Trim(path, "/"),
}
}

Expand Down Expand Up @@ -95,15 +96,17 @@ 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 {
Children []nodeArrayChild `json:"__children__"`
}

type nodeArrayChild struct {
Name string `json:"__name__"`
Name string `json:"__name__"`
PrimaryType string `json:"jcr:primaryType,omitempty"`
}

func (n RepoNode) Siblings() ([]RepoNode, error) {
Expand Down

0 comments on commit 85806cc

Please sign in to comment.