-
Notifications
You must be signed in to change notification settings - Fork 553
/
Copy pathdriver_main.go
66 lines (59 loc) · 1.22 KB
/
driver_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
package drivers
import (
"encoding/json"
"fmt"
"io"
"os"
)
// DriverMain helps dry up the implementation of main.go for drivers
func DriverMain(driver Interface) {
method := os.Args[1]
var config Config
switch method {
case "assemble":
b, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to read from stdin")
os.Exit(1)
}
err = json.Unmarshal(b, &config)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse json from stdin: %v\n%s\n", err, b)
os.Exit(1)
}
case "templates":
// No input for this method
case "imports":
// No input for this method
}
var output interface{}
switch method {
case "assemble":
dinfo, err := driver.Assemble(config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
output = dinfo
case "templates":
templates, err := driver.Templates()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
output = templates
case "imports":
collection, err := driver.Imports()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
output = collection
}
b, err := json.Marshal(output)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to marshal json:", err)
os.Exit(1)
}
os.Stdout.Write(b)
}