From b8f207a5d79e72e2e5ceab3acc5ffcd8f6ea49fa Mon Sep 17 00:00:00 2001 From: kondoumh Date: Thu, 19 Aug 2021 12:56:03 +0900 Subject: [PATCH] Add logic for replacing user name --- README.md | 19 ++++++++++++++++++- cmd/graph.go | 18 +++++++++++++++++- pkg/file/file.go | 6 ++++++ pkg/types/author.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 pkg/types/author.go diff --git a/README.md b/README.md index 7bf9d41..aa7f476 100644 --- a/README.md +++ b/README.md @@ -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 `/_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 diff --git a/cmd/graph.go b/cmd/graph.go index 68e4596..a72c9a2 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -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 @@ -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") diff --git a/pkg/file/file.go b/pkg/file/file.go index 9a1670e..569b30b 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -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 +} diff --git a/pkg/types/author.go b/pkg/types/author.go new file mode 100644 index 0000000..8a06ebc --- /dev/null +++ b/pkg/types/author.go @@ -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 +}