Skip to content

Commit

Permalink
兼容 JSON 格式的输入
Browse files Browse the repository at this point in the history
  • Loading branch information
eaglexiang committed Dec 12, 2024
1 parent 6970c33 commit c98eca5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
# Go workspace file
go.work

.vscode
45 changes: 43 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

func main() {
Expand All @@ -14,10 +18,47 @@ func main() {
fmt.Println("source file cannot be empty")
return
}
filename := *src

cmd := fmt.Sprintf("cat %s | flamegraph.pl > flamegraph.svg", *src)
err := exec.Command("bash", "-c", cmd).Run()
ext := filepath.Ext(filename)

var err error
switch ext {
case ".json":
err = jsonDraw(filename)
default:
err = defaultDraw(filename)
}
if err != nil {
fmt.Println(err)
}
}

func defaultDraw(filename string) (err error) {
cmd := fmt.Sprintf("cat %s | flamegraph.pl > flamegraph.svg", filename)
err = exec.Command("bash", "-c", cmd).Run()
return
}

func jsonDraw(filename string) (err error) {
buf, err := os.ReadFile(filename)
if err != nil {
return
}
lines := []string{}
err = json.Unmarshal(buf, &lines)
if err != nil {
return
}

text := strings.Join(lines, "\n")
err = os.WriteFile("tmp.log", []byte(text), os.ModePerm)
if err != nil {
return
}
defer os.Remove("tmp.log")

err = defaultDraw("tmp.log")

return
}

0 comments on commit c98eca5

Please sign in to comment.