mirror of
https://github.com/TECHNOFAB11/docsonnet.git
synced 2025-12-13 23:03:59 +01:00
feat: make paths work
This commit is contained in:
parent
21c8ebc75f
commit
75f8e7373c
6 changed files with 83 additions and 77 deletions
77
main.go
77
main.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue