-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
93 lines (73 loc) · 2.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"github.com/lucasepe/yml2dot/parser"
"github.com/lucasepe/yml2dot/renderer"
)
const (
maxFileSize int64 = 512 * 1024 // 512 Kb
banner = `+--------+ +-------+
| YAML +----->| Dot |
+--------+ +-------+`
)
var (
flagBlockStart string
flagBlockEnd string
)
func main() {
configureFlags()
src, err := inputSource()
exitOnErr(err)
defer src.Close()
res, err := parser.Parse(src, flagBlockStart, flagBlockEnd)
exitOnErr(err)
fmt.Print(renderer.Render(res))
}
func configureFlags() {
name := appName()
flag.CommandLine.Usage = func() {
fmt.Print(banner, "\n")
fmt.Printf("Turn YAML into beautiful Graph.\n\n")
fmt.Print("USAGE:\n\n")
fmt.Printf(" %s [flags] <path/to/your/file>\n", name)
fmt.Printf(" echo \"your yaml here\" | %s\n\n", name)
fmt.Print("EXAMPLE(s):\n\n")
fmt.Printf(" %s -from '/****' -to '****/' MyClass.java | dot -Tpng > output.png\n", name)
fmt.Printf(" %s config.yml | dot -Tpng > output.png\n", name)
fmt.Printf(" cat config.yml | %s | dot -Tpng > output.png\n\n", name)
fmt.Print("FLAGS:\n\n")
flag.CommandLine.SetOutput(os.Stdout)
flag.CommandLine.PrintDefaults()
flag.CommandLine.SetOutput(ioutil.Discard) // hide flag errors
fmt.Print(" -help\n\tprints this message\n")
fmt.Println()
fmt.Println("Crafted with passion by Luca Sepe - https://github.com/lucasepe/yml2dot")
}
flag.CommandLine.SetOutput(ioutil.Discard) // hide flag errors
flag.CommandLine.Init(os.Args[0], flag.ExitOnError)
flag.CommandLine.StringVar(&flagBlockStart, "from", "", "pattern that marks the beginning of the YAML block")
flag.CommandLine.StringVar(&flagBlockEnd, "to", "", "pattern that marks the end of the YAML block")
flag.CommandLine.Parse(os.Args[1:])
}
func appName() string {
return filepath.Base(os.Args[0])
}
// inputSource determines the source of the input, either from a file or stdin.
func inputSource() (io.ReadCloser, error) {
if flag.CommandLine.Arg(0) == "" {
return os.Stdin, nil
}
return os.Open(flag.Args()[0])
}
// exitOnErr check for an error and eventually exit
func exitOnErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
os.Exit(1)
}
}