feat: make paths work

This commit is contained in:
sh0rez 2020-05-02 19:54:34 +02:00
parent 21c8ebc75f
commit 75f8e7373c
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1
6 changed files with 83 additions and 77 deletions

77
main.go
View file

@ -1,11 +1,9 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/go-clix/cli"
@ -22,66 +20,25 @@ func main() {
Short: "Utility to parse and transform Jsonnet code that uses the docsonnet extension",
}
root.AddCommand(loadCmd(), renderCmd())
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
}
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 {
return err
}
}
return nil
}
if err := root.Execute(); err != nil {
log.Fatalln(err)
}
}
func loadCmd() *cli.Command {
cmd := &cli.Command{
Use: "load",
Short: "extracts docsonnet from Jsonnet and prints it as JSON",
Args: cli.ArgsExact(1),
}
cmd.Run = func(cmd *cli.Command, args []string) error {
pkg, err := docsonnet.Load(args[0])
if err != nil {
return err
}
data, err := json.MarshalIndent(pkg, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
return cmd
}
func renderCmd() *cli.Command {
cmd := &cli.Command{
Use: "render",
Short: "writes all found docsonnet packages to Markdown (.md) files, suitable for e.g. GitHub",
Args: cli.ArgsExact(1),
}
output := cmd.Flags().StringP("output", "o", "docs", "directory to write the .md files to")
cmd.Run = func(cmd *cli.Command, args []string) error {
pkg, err := docsonnet.Load(args[0])
if err != nil {
return err
}
for path, pkg := range render.Paths(*pkg) {
to := filepath.Join(*output, path)
if err := os.MkdirAll(filepath.Dir(to), os.ModePerm); err != nil {
return err
}
data := render.Render(pkg)
if err := ioutil.WriteFile(to, []byte(data), 0644); err != nil {
return err
}
}
return nil
}
return cmd
}