-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Go] Added Results.ToDOT, and cmd/todot utility
Signed-off-by: Andrea Barberio <insomniac@slackware.it>
- Loading branch information
1 parent
859ee5b
commit 7554a8d
Showing
4 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* SPDX-License-Identifier: BSD-2-Clause */ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"go/build" | ||
"log" | ||
"os" | ||
|
||
flag "github.com/spf13/pflag" | ||
|
||
"github.com/insomniacslk/dublin-traceroute/go/dublintraceroute/results" | ||
) | ||
|
||
func init() { | ||
// Ensure that CGO is disabled | ||
var ctx build.Context | ||
if ctx.CgoEnabled { | ||
fmt.Println("Disabling CGo") | ||
ctx.CgoEnabled = false | ||
} | ||
} | ||
|
||
var ( | ||
flagOutputFile = flag.StringP("output", "o", "-", "Output file. Use \"-\" to print to standard output") | ||
) | ||
|
||
func main() { | ||
|
||
flag.Parse() | ||
|
||
if len(flag.Args()) != 1 { | ||
log.Fatal("Missing JSON file name") | ||
} | ||
|
||
buf, err := os.ReadFile(flag.Arg(0)) | ||
if err != nil { | ||
log.Fatalf("Failed to read file '%s': %v", flag.Arg(0), err) | ||
} | ||
|
||
var result results.Results | ||
if err := json.Unmarshal(buf, &result); err != nil { | ||
log.Fatalf("Failed to unmarshal JSON into Results: %v", err) | ||
} | ||
output, err := result.ToDOT() | ||
if err != nil { | ||
log.Fatalf("Failed to convert to DOT: %v", err) | ||
} | ||
if *flagOutputFile == "-" { | ||
fmt.Println(output) | ||
} else { | ||
err := os.WriteFile(*flagOutputFile, []byte(output), 0644) | ||
if err != nil { | ||
log.Fatalf("Failed to write DOT file: %v", err) | ||
} | ||
log.Printf("Saved DOT file to %s", *flagOutputFile) | ||
log.Printf("Run `dot -Tpng \"%s\" -o \"%s.png\"` to convert to PNG", *flagOutputFile, *flagOutputFile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters