-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
85 lines (73 loc) · 2.01 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/nikolaydubina/jsonl-graph/color"
"github.com/nikolaydubina/jsonl-graph/dot"
"github.com/nikolaydubina/jsonl-graph/graph"
)
type renderable interface {
Render() string
}
func getFileFromLocalFiles(path string) ([]byte, error) {
var t http.Transport
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
c := http.Client{Transport: &t}
res, err := c.Get(path)
if err != nil {
return nil, fmt.Errorf("can not load file at path %s: %w", path, err)
}
return io.ReadAll(res.Body)
}
// decideOrientation picks orientation. Typically graphs with lots of data in nodes better look in TopDown orientation.
func decideOrientation(g graph.Graph) dot.Orientation {
for _, n := range g.Nodes {
if len(n) > 1 {
return dot.TB
}
}
return dot.LR
}
func main() {
var colorSchemeFilePath string
var lrOrientation bool
var tbOrientation bool
// TODO: help message with examples
flag.StringVar(&colorSchemeFilePath, "color-scheme", "", "optional file-path to colorscheme file (e.g. file://basic-colors.json)")
flag.BoolVar(&lrOrientation, "lr", false, "left-right orientation")
flag.BoolVar(&tbOrientation, "tb", false, "top-bottom orientation")
flag.Parse()
// read graph
g, err := graph.NewGraphFromJSONL(os.Stdin)
if err != nil {
log.Fatalf("can no read graph from json: %s", err)
}
// orientation
if lrOrientation && tbOrientation {
log.Fatalf("can not have lr and tb orientation at same time")
}
var orientation dot.Orientation
switch {
case lrOrientation:
orientation = dot.LR
case tbOrientation:
orientation = dot.TB
default:
orientation = decideOrientation(g)
}
var conf color.ColorConfig
if colorSchemeFilePath != "" {
if colorFile, err := getFileFromLocalFiles(colorSchemeFilePath); err == nil {
if err := json.Unmarshal(colorFile, &conf); err != nil {
log.Fatalf("bad color config: %s", err)
}
}
}
r := dot.NewColoredGraph(g, orientation, conf)
os.Stdout.WriteString(r.Render())
}