Skip to content

Commit

Permalink
Add logic for replacing user name
Browse files Browse the repository at this point in the history
  • Loading branch information
kondoumh committed Aug 19, 2021
1 parent 81c8242 commit b8f207a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,24 @@ To generate graph data as JSON, specify -j(--json) flag.
sbgraph graph -j=true
```

To generate graph Image as SVG format, specify -m(--image) flag.
If you want replace the output username to something else, create the `<WorkDir>/<project name>_authors.json` file as follows. The `id` must match what actually exists.

```json
{
"authors": [
{
"id": "5b123abcdeefe345689seded",
"name": "Alice"
},
{
"id": "5d3435ddfdaab341234dedea",
"name": "Bob"
}
]
}
```

To generate graph image as SVG format, specify -m(--image) flag.

```
sbgraph graph -m=true
Expand Down
18 changes: 17 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ func buildGraph(cmd *cobra.Command) {
}
uNodes := map[string]int{}
if includeUser {
var authors types.Authors
if (file.Exists(projectName + "_authors.json", config.WorkDir)) {
err := authors.ReadFrom(projectName, config.WorkDir)
CheckErr(err)
}
for _, u := range users {
if len(u.PagesCreated) == 0 {
continue
Expand All @@ -125,7 +130,18 @@ func buildGraph(cmd *cobra.Command) {
if anonymize {
username = u.ID[5:10]
} else {
username = u.DisplayName
if len(authors.Authors) > 0 {
for _, a := range authors.Authors {
if u.ID == a.ID {
username = a.Name
}
}
if username == "" {
username = u.DisplayName
}
} else {
username = u.DisplayName
}
}
gid := graph.AddNode(username)
graph.NodeAttribute(gid, graphviz.FillColor, "cyan")
Expand Down
6 changes: 6 additions & 0 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ func ReadBytes(fileName string, outDir string) ([]byte, error) {
}
return raw, err
}

// Exists will check if the file exists
func Exists(fileName string, outDir string) bool {
_, err := os.Stat(outDir + "/" + fileName)
return err == nil
}
30 changes: 30 additions & 0 deletions pkg/types/author.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package types

import (
"encoding/json"

"github.com/mamezou-tech/sbgraph/pkg/file"
)

// Authors represents authors of the Scrapbox project
type Authors struct {
Authors []Author `json:"authors"`
}

// Author represents author of the Scrapbox page
type Author struct {
ID string `json:"id"`
Name string `json:"name"`
}

// ReadFrom will deserialize Authors from file
func (authors *Authors) ReadFrom(projectName string, workDir string) error {
bytes, err := file.ReadBytes(projectName+"_authors.json", workDir)
if err != nil {
return err
}
if err := json.Unmarshal(bytes, &authors); err != nil {
return err
}
return nil
}

0 comments on commit b8f207a

Please sign in to comment.