Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use jsonnet render engine in go binary #54

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
module github.com/jsonnet-libs/docsonnet

go 1.19
go 1.21.1

require (
github.com/go-clix/cli v0.1.2-0.20200502172020-b8f4629e879a
github.com/google/go-cmp v0.4.0
github.com/google/go-jsonnet v0.18.0
github.com/markbates/pkger v0.15.1
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.2.7
github.com/go-clix/cli v0.2.0
github.com/google/go-jsonnet v0.20.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobuffalo/here v0.6.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
34 changes: 6 additions & 28 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/go-clix/cli v0.1.2-0.20200502172020-b8f4629e879a h1:nh+UOawbjKgiUAJAgi8JHctNebEu6mjwDXsv8Xdln8w=
github.com/go-clix/cli v0.1.2-0.20200502172020-b8f4629e879a/go.mod h1:dYJevXraB9mXZFhz5clyQestG0qGcmT5rRC/P9etoRQ=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-jsonnet v0.18.0 h1:/6pTy6g+Jh1a1I2UMoAODkqELFiVIdOxbNwv0DDzoOg=
github.com/google/go-jsonnet v0.18.0/go.mod h1:C3fTzyVJDslXdiTqw/bTFk7vSGyCtH3MGRbDfvEwGd0=
github.com/go-clix/cli v0.2.0 h1:rqpcyS/cvshOhXkwii0V+7nWetDVC8cp4pKI7JiCIS8=
github.com/go-clix/cli v0.2.0/go.mod h1:yWI9abpv187r47lDjz8Z9TWev93aUTWaW2seSb5JmPQ=
github.com/google/go-jsonnet v0.20.0 h1:WG4TTSARuV7bSm4PMB4ohjxe33IHT5WVTrJSU33uT4g=
github.com/google/go-jsonnet v0.20.0/go.mod h1:VbgWF9JX7ztlv770x/TolZNGGFfiHEVx9G6ca2eUmeA=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/markbates/pkger v0.15.1 h1:3MPelV53RnGSW07izx5xGxl4e/sdRD6zqseIk0rMASY=
github.com/markbates/pkger v0.15.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
Expand All @@ -34,16 +19,9 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
Expand Down
27 changes: 0 additions & 27 deletions load.libsonnet

This file was deleted.

104 changes: 76 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package main

import (
"embed"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/go-clix/cli"
"github.com/google/go-jsonnet"
)

"github.com/jsonnet-libs/docsonnet/pkg/docsonnet"
"github.com/jsonnet-libs/docsonnet/pkg/render"
var (
//go:embed doc-util
embedded embed.FS
)

func main() {
Expand All @@ -21,51 +28,92 @@ func main() {
}

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})
vm := jsonnet.MakeVM()
importer, err := newImporter(*jpath)
if err != nil {
log.Fatalln("Extracting:", err)
}
if *outputRaw {
fmt.Println(string(data))
return nil
return err
}
vm.Importer(importer)

log.Println("Transforming to docsonnet model")
pkg, err := docsonnet.Transform(data)
renderSnippet, err := embedded.ReadFile("doc-util/render.libsonnet")
if err != nil {
log.Fatalln("Transforming:", err)
return err
}
if *outputJSON {
data, err := json.MarshalIndent(pkg, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil

snippet := fmt.Sprintf(`(%s).render(std.extVar('main'))`, renderSnippet)

vm.ExtCode("main", fmt.Sprintf(`(import "%s")`, file))

jsonStr, err := vm.EvaluateAnonymousSnippet("main.libsonnet", snippet)
if err != nil {
return err
}

log.Println("Rendering markdown")
n, err := render.To(*pkg, *dir, render.Opts{
URLPrefix: *urlPrefix,
})
var output map[string]string
err = json.Unmarshal([]byte(jsonStr), &output)
if err != nil {
log.Fatalln("Rendering:", err)
return err
}

log.Printf("Success! Rendered %v packages from '%s' to '%s'", n, file, *dir)
for k, v := range output {
fullpath := filepath.Join(*dir, k)
dir := filepath.Dir(fullpath)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
if err := os.WriteFile(fullpath, []byte(v), 0644); err != nil {
return err
}
}
Comment on lines +63 to +72
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is copied from pkg/render/render.go.

return nil
}

if err := root.Execute(); err != nil {
log.Fatalln(err)
}
}

// importer wraps jsonnet.FileImporter
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is copied from pkg/docsonnet/load.go.

type importer struct {
fi jsonnet.FileImporter
util jsonnet.Contents
}

func newImporter(paths []string) (*importer, error) {
load, err := embedded.ReadFile("doc-util/main.libsonnet")
if err != nil {
return nil, err
}

render, err := embedded.ReadFile("doc-util/render.libsonnet")
if err != nil {
return nil, err
}

main := strings.ReplaceAll(string(load), "(import './render.libsonnet')", string(render))

return &importer{
fi: jsonnet.FileImporter{JPaths: paths},
util: jsonnet.MakeContents(main),
}, nil
}

var docUtilPaths = []string{
"doc-util/main.libsonnet",
"github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet",
}

func (i *importer) Import(importedFrom, importedPath string) (contents jsonnet.Contents, foundAt string, err error) {
for _, p := range docUtilPaths {
if importedPath == p {
return i.util, "main.libsonnet", nil
}
}

return i.fi.Import(importedFrom, importedPath)
}
Loading
Loading