-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
71 lines (59 loc) · 1.77 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
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/go-clix/cli"
"github.com/jsonnet-libs/docsonnet/pkg/docsonnet"
"github.com/jsonnet-libs/docsonnet/pkg/render"
)
func main() {
log.SetFlags(0)
root := &cli.Command{
Use: "docsonnet <file>",
Short: "Utility to parse and transform Jsonnet code that uses the docsonnet extension",
Args: cli.ArgsExact(1),
}
dir := root.Flags().StringP("output", "o", "docs", "directory to write the .md files to")
outputJSON := root.Flags().Bool("json", false, "print loaded docsonnet as JSON")
outputRaw := root.Flags().Bool("raw", false, "don't transform, dump raw eval result")
urlPrefix := root.Flags().String("urlPrefix", "/", "url-prefix for frontmatter")
jpath := root.Flags().StringSliceP("jpath", "J", []string{"vendor"}, "Specify an additional library search dir (right-most wins)")
root.Run = func(cmd *cli.Command, args []string) error {
file := args[0]
log.Println("Extracting from Jsonnet")
data, err := docsonnet.Extract(file, docsonnet.Opts{JPath: *jpath})
if err != nil {
log.Fatalln("Extracting:", err)
}
if *outputRaw {
fmt.Println(string(data))
return nil
}
log.Println("Transforming to docsonnet model")
pkg, err := docsonnet.Transform(data)
if err != nil {
log.Fatalln("Transforming:", err)
}
if *outputJSON {
data, err := json.MarshalIndent(pkg, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
log.Println("Rendering markdown")
n, err := render.To(*pkg, *dir, render.Opts{
URLPrefix: *urlPrefix,
})
if err != nil {
log.Fatalln("Rendering:", err)
}
log.Printf("Success! Rendered %v packages from '%s' to '%s'", n, file, *dir)
return nil
}
if err := root.Execute(); err != nil {
log.Fatalln(err)
}
}