Skip to content

Commit

Permalink
reuse graphviz (#198)
Browse files Browse the repository at this point in the history
Signed-off-by: Connor1996 <[email protected]>
  • Loading branch information
Connor1996 authored Nov 6, 2023
1 parent c6c58af commit 8333a0c
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions component/conprof/jeprof/jeprof.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package jeprof

import (
"bytes"
_ "embed"
"fmt"
"io"
"os"
"os/exec"
"strings"

graphviz "github.com/goccy/go-graphviz"
)

//go:embed jeprof.in
Expand Down Expand Up @@ -53,15 +56,34 @@ func ConvertToSVG(data []byte) ([]byte, error) {
return nil, err
}

cmd := exec.Command("perl", "/dev/stdin", "--svg", f.Name()) //nolint:gosec
cmd := exec.Command("perl", "/dev/stdin", "--dot", f.Name()) //nolint:gosec
cmd.Stdin = strings.NewReader(jeprof)
svgContent, err := cmd.Output()
dotContent, err := cmd.Output()
if err != nil {
return nil, err
}
svgContent, err := convertDotToSVG(dotContent)
if err != nil {
return nil, err
}
return svgContent, nil
}

func convertDotToSVG(dotData []byte) ([]byte, error) {
g := graphviz.New()
graph, err := graphviz.ParseBytes(dotData)
if err != nil {
return nil, err
}

buf := bytes.NewBuffer(nil)
err = g.Render(graph, graphviz.SVG, buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func ConvertToText(data []byte) ([]byte, error) {
f, err := os.CreateTemp("", "prof")
if err != nil {
Expand Down

0 comments on commit 8333a0c

Please sign in to comment.