Skip to content

Commit

Permalink
Repo node children / nice path list sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Feb 8, 2023
1 parent 339a489 commit a76480d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/aem/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *CLI) repoNodeChildrenCmd() *cobra.Command {
c.Error(err)
return
}
c.SetOutput("children", children)
c.SetOutput("children", pkg.NewRepoNodeList(children))
c.Ok("node children read")
},
}
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()
}
4 changes: 2 additions & 2 deletions pkg/repo_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,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 @@ -81,7 +81,7 @@ func (n RepoNode) Breadcrumb() []RepoNode {
}

func (n RepoNode) Child(name string) RepoNode {
return NewNode(n.repo, fmt.Sprintf("%s/%s", strings.TrimPrefix(n.path, "/"), name))
return NewNode(n.repo, fmt.Sprintf("%s/%s", n.path, name))
}

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

0 comments on commit a76480d

Please sign in to comment.