Skip to content

Commit 75a056e

Browse files
dmacvicarblampe
authored andcommitted
Implement a more consistent CLI interface
By default, read from stdin, unless `-i infile` is given. By default, write to stdout, unless `-o outfile` is given or a binary format with `-f` is selected. By default, it writes in SVG format, unless another format is specified with `-f`.
1 parent 912ed95 commit 75a056e

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ instead I recommend you look at these forks:
2020

2121
* [@bep] is the fork currently used by Hugo, which I expect to be more active
2222
over time.
23-
* [@dmacvicar] has improved SVG/PDF rendering, font handling, and CLI
24-
experience.
23+
* [@dmacvicar] has improved SVG/PNG/PDF rendering.
2524
* [@sw46] has implemented a really wonderful hand-drawn style worth checking
2625
out.
2726

2827
## Usage
2928

3029
```bash
3130
$ go get github.com/blampe/goat
32-
$ goat my-cool-diagram.txt > my-cool-diagram.svg
31+
$ cat my-cool-diagram.txt | goat > my-cool-diagram.svg
3332
```
3433

34+
By default, the program reads from stdin, unless `-i infile` is given.
35+
36+
By default, the program writes to stdout, unless `-o outfile` is given or a
37+
binary format with `-f` is selected.
38+
39+
By default, it writes in [SVG] format, unless another format is specified with
40+
`-f`.
41+
3542
## TODO
3643

3744
- Dashed lines signaled by `:` or `=`.
@@ -195,5 +202,6 @@ More examples are available [here](examples).
195202
[@bep]: https://github.com/bep/goat/
196203
[@dmacvicar]: https://github.com/dmacvicar/goat
197204
[@sw46]: https://github.com/sw46/goat/
205+
[SVG]: https://en.wikipedia.org/wiki/Scalable_Vector_Graphics
198206
[markdeep.mini.js]: http://casual-effects.com/markdeep/
199207
[v0.93.0]: https://github.com/gohugoio/hugo/releases/tag/v0.93.0

cmd/goat/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)