|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/blampe/goat" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + log.SetFlags(0) |
| 16 | + |
| 17 | + var inputFilename string |
| 18 | + var outputFilename string |
| 19 | + var format string |
| 20 | + |
| 21 | + flag.StringVar(&inputFilename, "i", "", "Input filename (default stdin)") |
| 22 | + flag.StringVar(&outputFilename, "o", "", "Output filename (default stdout for SVG)") |
| 23 | + flag.StringVar(&format, "f", "svg", "Output format: svg (default: svg)") |
| 24 | + flag.Parse() |
| 25 | + |
| 26 | + format = strings.ToLower(format) |
| 27 | + if format != "svg" { |
| 28 | + log.Fatalf("unrecognized format: %s", format) |
| 29 | + } |
| 30 | + |
| 31 | + input := os.Stdin |
| 32 | + if inputFilename != "" { |
| 33 | + if _, err := os.Stat(inputFilename); os.IsNotExist(err) { |
| 34 | + log.Fatalf("input file not found: %s", inputFilename) |
| 35 | + } |
| 36 | + var err error |
| 37 | + input, err = os.Open(inputFilename) |
| 38 | + defer input.Close() |
| 39 | + if err != nil { |
| 40 | + log.Fatal(err) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + output := os.Stdout |
| 45 | + if outputFilename != "" { |
| 46 | + var err error |
| 47 | + output, err = os.Create(outputFilename) |
| 48 | + defer output.Close() |
| 49 | + if err != nil { |
| 50 | + log.Fatal(err) |
| 51 | + } |
| 52 | + // warn the user if he is writing to an extension different to the |
| 53 | + // file format |
| 54 | + ext := filepath.Ext(outputFilename) |
| 55 | + if fmt.Sprintf(".%s", format) != ext { |
| 56 | + log.Printf("Warning: writing to '%s' with extension '%s' and format %s", outputFilename, ext, strings.ToUpper(format)) |
| 57 | + } |
| 58 | + } else { |
| 59 | + // check that we are not writing binary data to terminal |
| 60 | + fileInfo, _ := os.Stdout.Stat() |
| 61 | + isTerminal := (fileInfo.Mode() & os.ModeCharDevice) != 0 |
| 62 | + if isTerminal && format != "svg" { |
| 63 | + log.Fatalf("refuse to write binary data to terminal: %s", format) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + switch format { |
| 68 | + case "svg": |
| 69 | + goat.BuildAndWriteSVG(input, output) |
| 70 | + } |
| 71 | +} |
0 commit comments