mirror of
https://github.com/TECHNOFAB11/docsonnet.git
synced 2025-12-11 22:10:13 +01:00
feat: add extcode flag
This commit is contained in:
parent
629398bcac
commit
894f3a5b2a
3 changed files with 19 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
|
.idea/
|
||||||
dist
|
dist
|
||||||
docsonnet
|
docsonnet
|
||||||
|
|
|
||||||
13
main.go
13
main.go
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-clix/cli"
|
"github.com/go-clix/cli"
|
||||||
|
|
||||||
|
|
@ -25,12 +26,22 @@ func main() {
|
||||||
outputRaw := root.Flags().Bool("raw", false, "don't transform, dump raw eval result")
|
outputRaw := root.Flags().Bool("raw", false, "don't transform, dump raw eval result")
|
||||||
urlPrefix := root.Flags().String("urlPrefix", "/", "url-prefix for frontmatter")
|
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)")
|
jpath := root.Flags().StringSliceP("jpath", "J", []string{"vendor"}, "Specify an additional library search dir (right-most wins)")
|
||||||
|
extcode := root.Flags().StringSlice("ext-code", []string{}, "specify additional ext code")
|
||||||
|
|
||||||
root.Run = func(cmd *cli.Command, args []string) error {
|
root.Run = func(cmd *cli.Command, args []string) error {
|
||||||
file := args[0]
|
file := args[0]
|
||||||
|
|
||||||
log.Println("Extracting from Jsonnet")
|
log.Println("Extracting from Jsonnet")
|
||||||
data, err := docsonnet.Extract(file, docsonnet.Opts{JPath: *jpath})
|
|
||||||
|
opts := docsonnet.Opts{JPath: *jpath, ExtCode: map[string]string{}}
|
||||||
|
if len(*extcode) > 0 {
|
||||||
|
for _, ext := range *extcode {
|
||||||
|
res := strings.Split(ext, "=")
|
||||||
|
key, value := res[0], res[1]
|
||||||
|
opts.ExtCode[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data, err := docsonnet.Extract(file, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Extracting:", err)
|
log.Fatalln("Extracting:", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Opts struct {
|
type Opts struct {
|
||||||
JPath []string
|
JPath []string
|
||||||
|
ExtCode map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load extracts and transforms the docsonnet data in `filename`, returning the
|
// Load extracts and transforms the docsonnet data in `filename`, returning the
|
||||||
|
|
@ -51,6 +52,10 @@ func Extract(filename string, opts Opts) ([]byte, error) {
|
||||||
// invoke load.libsonnet
|
// invoke load.libsonnet
|
||||||
vm.ExtCode("main", fmt.Sprintf(`(import "%s")`, filename))
|
vm.ExtCode("main", fmt.Sprintf(`(import "%s")`, filename))
|
||||||
|
|
||||||
|
for k, v := range opts.ExtCode {
|
||||||
|
vm.ExtCode(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
data, err := vm.EvaluateAnonymousSnippet("load.libsonnet", string(load))
|
data, err := vm.EvaluateAnonymousSnippet("load.libsonnet", string(load))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue