99 "os"
1010 "path/filepath"
1111
12+ "aqwari.net/xml/xmltree"
1213 "github.com/goccy/go-graphviz"
1314 "github.com/goccy/go-graphviz/cgraph"
1415)
@@ -23,7 +24,7 @@ type Edge struct {
2324 NeighborPort string
2425}
2526
26- func SaveTopologyWithGraphviz (ctx context.Context , networkMap * NetworkMap ) error {
27+ func SaveTopologyWithGraphviz (ctx context.Context , networkMap * NetworkMap , timestamp string ) error {
2728 g , err := graphviz .New (ctx )
2829 if err != nil {
2930 return err
@@ -80,42 +81,63 @@ func SaveTopologyWithGraphviz(ctx context.Context, networkMap *NetworkMap) error
8081 // if err := g.Render(ctx, graph, "dot", &buf); err != nil {
8182 // log.Fatal(err)
8283 // }
83- // fmt.Println( buf.String())
84+ // fmt.Printf("\n%v", buf.String())
8485
8586 // image.Image format
8687 outputImg , err := g .RenderImage (ctx , graph )
8788 if err != nil {
8889 return err
8990 }
9091
91- // encode
92- out := make ([]byte , 0 )
93- writer := bytes .NewBuffer (out )
92+ // encode to svg
93+ outputBuf := & bytes.Buffer {}
94+ err = g .Render (ctx , graph , graphviz .SVG , outputBuf )
95+ if err != nil {
96+ return err
97+ }
9498
95- err = png . Encode ( writer , outputImg )
99+ doc , err := xmltree . Parse ( outputBuf . Bytes () )
96100 if err != nil {
97101 return err
98102 }
99103
104+ outputSvg := xmltree .Marshal (doc )
105+
106+ // MacOS Path: /Users/<USERNAME>/Library/Application Support/netmap
100107 appDir , err := utils .CreateDirectoryToSaveOutput ()
101108 if err != nil {
102109 return err
103110 }
104111
105- // creating a file
106- filePath := filepath .Join (appDir , "network-topology.png" )
107- outputFile , err := os .Create (filePath )
112+ // create filenames for svg and png
113+ baseFilename := "graph"
114+ svgFilename := fmt .Sprintf ("%s_%s.svg" , baseFilename , timestamp )
115+ svgFilenamePath := filepath .Join (appDir , svgFilename )
116+ pngFilename := fmt .Sprintf ("%s_%s.png" , baseFilename , timestamp )
117+
118+ err = renderOutputToFilename (svgFilenamePath , outputSvg )
108119 if err != nil {
109120 return err
110121 }
111- defer outputFile .Close ()
112122
113- // writing to a file
114- _ , err = outputFile .Write (writer .Bytes ())
123+ // encode to png
124+ outputImgBytes := make ([]byte , 0 )
125+ writer := bytes .NewBuffer (outputImgBytes )
126+
127+ err = png .Encode (writer , outputImg )
128+ if err != nil {
129+ return err
130+ }
131+
132+ err = renderOutputToFilename (filepath .Join (appDir , pngFilename ), writer .Bytes ())
115133 if err != nil {
116134 return err
117135 }
118- fmt .Printf ("\n Output path: %s" , appDir )
136+
137+ fmt .Printf ("\n Exported graph to .json, .svg, and .png file formats: %s" , appDir )
138+ fmt .Printf ("\n To view the SVG file, copy and paste the path in your favorite browser: %s" , svgFilenamePath )
139+
140+ // TODO: Create a ZIP file with .svg, .png, .jpg, dot files.
119141
120142 // Save as PNG
121143 // filePath := "/Users/roopesh/Desktop/projects/network-mapper/network-topology.png"
@@ -127,3 +149,21 @@ func SaveTopologyWithGraphviz(ctx context.Context, networkMap *NetworkMap) error
127149
128150 return nil
129151}
152+
153+ func renderOutputToFilename (filePath string , outData []byte ) error {
154+ // creating a file
155+ // filePath := filepath.Join(appDir, "network-topology.png")
156+ outputFile , err := os .Create (filePath )
157+ if err != nil {
158+ return err
159+ }
160+ defer outputFile .Close ()
161+
162+ // writing to a file
163+ _ , err = outputFile .Write (outData )
164+ if err != nil {
165+ return err
166+ }
167+
168+ return nil
169+ }
0 commit comments