diff --git a/README.md b/README.md index 65cc94e..7bf9d41 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,9 @@ sbgraph fetch To fetch from a private project, you needs to set the cookie to environment variables. -```bash -$ export SB_COOKIE_ID=connect.sid -$ export SB_COOKIE_VALUE=your-fancy-cookie +``` +export SB_COOKIE_ID=connect.sid +export SB_COOKIE_VALUE=your-fancy-cookie ``` ### Aggregate user activites in the project @@ -129,9 +129,26 @@ sbgraph graph -t 100 Graphviz dot file will be created at `/.dot`. + +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. + +``` +sbgraph graph -m=true +``` + +Graphviz needs to be installed. You can not specify layout engine. + ### Visualize with Graphviz -You can generate graph image with dot command. e.g. +You can generate SVG with graph sub command, but to specify more options such as layout engine, you can use dot command directly. + +e.g. ``` dot .dot -Tsvg -Kfdp -Goverlap=prism -.svg diff --git a/cmd/graph.go b/cmd/graph.go index 2923e3c..c86f8b3 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -60,6 +60,7 @@ func init() { graphCmd.PersistentFlags().BoolP("include", "i", false, "Include user node") graphCmd.PersistentFlags().BoolP("anonymize", "a", false, "Anonymize user") graphCmd.PersistentFlags().BoolP("json", "j", false, "Output as JSON format") + graphCmd.PersistentFlags().BoolP("image", "m", false, "Output SVG image") rootCmd.AddCommand(graphCmd) } @@ -70,8 +71,10 @@ func buildGraph(cmd *cobra.Command) { threshold, _ := cmd.PersistentFlags().GetInt("threshold") includeUser, _ := cmd.PersistentFlags().GetBool("include") anonymize, _ := cmd.PersistentFlags().GetBool("anonymize") - oJson, _ := cmd.PersistentFlags().GetBool("json") - fmt.Printf("Build graph project : %s, threshold : %d, include user: %t, anonymize : %t, json : %t\n", projectName, threshold, includeUser, anonymize, oJson) + oJSON, _ := cmd.PersistentFlags().GetBool("json") + oSVG, _ := cmd.PersistentFlags().GetBool("image") + + fmt.Printf("Build graph project : %s, threshold : %d, include user: %t, anonymize : %t, json : %t, svg : %t\n", projectName, threshold, includeUser, anonymize, oJSON, oSVG) var proj types.Project err := proj.ReadFrom(projectName, config.WorkDir) CheckErr(err) @@ -151,11 +154,15 @@ func buildGraph(cmd *cobra.Command) { } err = writeDot(graph, projectName, config.WorkDir) CheckErr(err) - if oJson { + if oJSON { data, _ := json.Marshal(pGraph) err = file.WriteBytes(data, projectName+"_graph.json", config.WorkDir) CheckErr(err) } + if oSVG { + err = writeSvg(graph, projectName, config.WorkDir) + CheckErr(err) + } } func createGraph() graphviz.Graph { @@ -185,3 +192,8 @@ func writeDot(graph graphviz.Graph, projectName string, workDir string) error { graph.GenerateDOT(file) return nil } + +func writeSvg(graph graphviz.Graph, projectName string, workDir string) error { + path := fmt.Sprintf("%s/%s.svg", workDir, projectName) + return graph.GenerateImage("dot", path, "svg") +}