Skip to content

Commit

Permalink
Merge pull request #87 from k1LoW/hide-details
Browse files Browse the repository at this point in the history
Add `--hide-details` option
  • Loading branch information
k1LoW committed Apr 6, 2021
2 parents 9f36939 + 7e4b769 commit 384b7d1
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 8 deletions.
8 changes: 8 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ func newConfig() (*config.Config, error) {
if err := cfg.Build(); err != nil {
return nil, err
}

if hideDetails {
if err := cfg.HideDetails(); err != nil {
return nil, err
}
}

return cfg, nil
}

Expand All @@ -360,5 +367,6 @@ func init() {
docCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
docCmd.Flags().StringSliceVarP(&nodeLists, "nodes", "n", []string{}, "real node list file path")
docCmd.Flags().BoolVarP(&rmDist, "rm-dist", "", false, "remove all files in the document directory before generating the documents")
docCmd.Flags().BoolVarP(&hideDetails, "hide-details", "", false, "hide details")
rootCmd.AddCommand(docCmd)
}
1 change: 1 addition & 0 deletions cmd/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ func init() {
drawCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
drawCmd.Flags().StringSliceVarP(&nodeLists, "nodes", "n", []string{}, "real node list file path")
drawCmd.Flags().StringVarP(&out, "out", "", "", "output file path")
drawCmd.Flags().BoolVarP(&hideDetails, "hide-details", "", false, "hide details")
rootCmd.AddCommand(drawCmd)
}
17 changes: 9 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import (
)

var (
force bool
format string
layers []string
nodeLists []string
configPath string
out string
rmDist bool
iconPrefix string
force bool
format string
layers []string
nodeLists []string
configPath string
out string
rmDist bool
iconPrefix string
hideDetails bool
)

var rootCmd = &cobra.Command{
Expand Down
83 changes: 83 additions & 0 deletions config/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,86 @@ func (cfg *Config) buildColors() error {
}
return nil
}

const maskedDesc = "\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*."

func (cfg *Config) HideDetails() error {

// labels
labelMap := map[string]string{}
for i, l := range cfg.labels {
n := fmt.Sprintf("label%d", i)
labelMap[l.Id()] = n
l.Name = n
if l.Desc != "" {
l.Desc = maskedDesc
}
}

// layers
layerMap := map[string]string{}
for i, l := range cfg.Layers() {
n := fmt.Sprintf("layer%d", i)
layerMap[l.Id()] = n
l.Name = n
if l.Desc != "" {
l.Desc = maskedDesc
}
}

// views
for i, v := range cfg.Views {
v.Name = fmt.Sprintf("view%d", i)
if v.Desc != "" {
v.Desc = maskedDesc
}
for i, l := range v.Labels {
ll, ok := labelMap[strings.ToLower(l)]
if !ok {
return fmt.Errorf("label not found: %s", l)
}
v.Labels[i] = ll
}
for i, l := range v.Layers {
ll, ok := layerMap[strings.ToLower(l)]
if !ok {
return fmt.Errorf("layer not found: %s", l)
}
v.Layers[i] = ll
}
}

// clusters
for i, c := range cfg.Clusters() {
c.Name = fmt.Sprintf("cluster%d", i)
if c.Desc != "" {
c.Desc = maskedDesc
}
}

// nodes
for i, n := range cfg.Nodes {
n.Name = fmt.Sprintf("node%d", i)
if n.Desc != "" {
n.Desc = maskedDesc
}
n.RealNodes = nil
}

// components
for i, c := range cfg.Components() {
c.Name = fmt.Sprintf("component%d", i)
if c.Desc != "" {
c.Desc = maskedDesc
}
}

// relations
for _, r := range cfg.Relations {
if r.Desc != "" {
r.Desc = maskedDesc
}
}

return nil
}
31 changes: 31 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -241,6 +242,36 @@ func TestBuildNestedCluster(t *testing.T) {
}
}

func TestHideDetails(t *testing.T) {
func() {
tempDir, err := ioutil.TempDir("", "ndiag")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
cfg := New()
if err := cfg.LoadConfigFile(filepath.Join(testdataDir(t), "..", "example", "3-tier", "input", "ndiag.yml")); err != nil {
t.Fatal(err)
}
cfg.DocPath = tempDir
if err := cfg.Build(); err != nil {
t.Fatal(err)
}
want := len(cfg.Components())
if err := cfg.HideDetails(); err != nil {
t.Fatal(err)
}
if got := len(cfg.Components()); got != want {
t.Errorf("got %v\nwant %v", got, want)
}
for _, c := range cfg.Components() {
if !strings.Contains(c.Name, "component") {
t.Errorf("got %v\nwant %v", c.Name, "component*")
}
}
}()
}

func testdataDir(t *testing.T) string {
t.Helper()
wd, err := os.Getwd()
Expand Down

0 comments on commit 384b7d1

Please sign in to comment.