mirror of
https://github.com/TECHNOFAB11/docsonnet.git
synced 2025-12-13 23:03:59 +01:00
refactor(render): filewriting in render pkg
This commit is contained in:
parent
f284bff4e5
commit
14e9fc8f3f
7 changed files with 68 additions and 285 deletions
31
main.go
31
main.go
|
|
@ -1,10 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-clix/cli"
|
||||
|
||||
|
|
@ -20,19 +19,27 @@ func main() {
|
|||
Short: "Utility to parse and transform Jsonnet code that uses the docsonnet extension",
|
||||
}
|
||||
|
||||
output := root.Flags().StringP("output", "o", "docs", "directory to write the .md files to")
|
||||
root.Run = func(cmd *cli.Command, args []string) error {
|
||||
pkg, err := docsonnet.Load(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dir := root.Flags().StringP("output", "o", "docs", "directory to write the .md files to")
|
||||
outputMd := root.Flags().Bool("md", true, "render as markdown files")
|
||||
outputJSON := root.Flags().Bool("json", false, "print loaded docsonnet as JSON")
|
||||
|
||||
data := render.Render(*pkg)
|
||||
for k, v := range data {
|
||||
fmt.Println(k)
|
||||
if err := ioutil.WriteFile(filepath.Join(*output, k), []byte(v), 0644); err != nil {
|
||||
root.Run = func(cmd *cli.Command, args []string) error {
|
||||
file := args[0]
|
||||
|
||||
switch {
|
||||
case *outputJSON:
|
||||
model, err := docsonnet.Load(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(model, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(string(data))
|
||||
case *outputMd:
|
||||
return render.To(file, *dir)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue