core functionality

This commit is contained in:
Diego Fernando Carrión 2023-05-12 12:40:10 +02:00
parent 3a561d0f2c
commit 536af9ea0c
No known key found for this signature in database
GPG Key ID: 811EF2E03998BFC4
4 changed files with 470 additions and 1 deletions

25
go.mod
View File

@ -1,3 +1,26 @@
module gitlab.com/CRTHaze/helm-schema-2-doc
module gitlab.com/CRThaze/helm-schema-2-doc
go 1.20
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
sigs.k8s.io/yaml v1.3.0
)
require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/crypto v0.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

75
go.sum Normal file
View File

@ -0,0 +1,75 @@
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
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/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
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/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea h1:vLCWI/yYrdEHyN2JzIzPO3aaQJHQdp89IZBA/+azVC4=
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/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/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

46
main.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"flag"
"fmt"
"os"
jsp "gitlab.com/CRThaze/helm-schema-2-doc/parser"
)
type repeatStringFlag []string
func (r *repeatStringFlag) String() string {
return ""
}
func (r *repeatStringFlag) Set(value string) error {
*r = append(*r, value)
return nil
}
var flags struct {
output string
skip repeatStringFlag
}
func init() {
flag.StringVar(&flags.output, "o", "values.md", "The output file to write the resulting documentation.")
flag.Var(&flags.skip, "s", "Values to omit from the output, in the format '.Value.name'. Can be used multiple times.")
flag.Parse()
}
func main() {
valuesSet, err := jsp.ParseSchemaFile()
if err != nil {
fmt.Printf("ERROR: %+v\n", err)
os.Exit(1)
}
for _, toSkip := range flags.skip {
delete(valuesSet, toSkip)
}
if err := os.WriteFile(flags.output, valuesSet.Render(), 0644); err != nil {
fmt.Printf("ERROR: %+v\n", err)
os.Exit(1)
}
}

325
parser/parser.go Normal file
View File

@ -0,0 +1,325 @@
package parser
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"text/template"
sprig "github.com/Masterminds/sprig/v3"
gjs "github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"
)
type JSONType uint8
const (
Null JSONType = iota
Boolean
Object
Array
Number
String
Integer
InvalidType = ^JSONType(0)
)
func (t JSONType) String() string {
switch t {
case Null:
return "null"
case Boolean:
return "boolean"
case Object:
return "object"
case Array:
return "array"
case Number:
return "number"
case String:
return "string"
case Integer:
return "integer"
default:
return ""
}
}
func parseJSONType(t string) (JSONType, error) {
switch t {
case "null":
return Null, nil
case "boolean":
return Boolean, nil
case "object":
return Object, nil
case "array":
return Array, nil
case "number":
return Number, nil
case "string":
return String, nil
case "integer":
return Integer, nil
default:
return InvalidType, fmt.Errorf("'%s' is not a valid JSON Type", t)
}
}
const (
defaultSchemaFile = "values.schema.json"
defaultValuesFile = "values.yaml"
rootAddressPart = "Values"
undefinedDefault = "undefined"
)
var defaultTemplate = "| Value | Type | Required | Description | Default |\n" +
"|-------|------|----------|-------------|---------|\n" +
"{{- range . }}\n" +
"| `{{ .Address }}` | `{{ .Type }}` | {{ ternary \"yes\" \"no\" .Required }} | {{ .Description }} | `{{ .Default }}` |\n" +
"{{- end }}\n" +
"|-------|------|----------|-------------|---------|\n"
type ValueInfo struct {
Key string
Address string
Type JSONType
Description string
Required bool
Default string
}
type valuesMap map[string]interface{}
func (v valuesMap) hasKey(key string) bool {
if _, ok := v[key]; ok {
return true
}
return false
}
func (v valuesMap) lookupDefault(keys ...string) (strVal string) {
var ok bool
var addrLocation interface{}
currentValue := v
strVal = undefinedDefault
// fmt.Println(keys)
for i, addr := range keys {
// for j := 0; j < i; j++ {
// fmt.Printf("\t")
// }
// fmt.Println(addr)
addrLocation, ok = currentValue[addr]
if !ok {
// for j := 0; j < i; j++ {
// fmt.Printf("\t")
// }
// fmt.Println("KEY NOT FOUND")
return
}
currentValue, ok = addrLocation.(map[string]interface{})
if !ok && i != len(keys)-1 {
// for j := 0; j < i; j++ {
// fmt.Printf("\t")
// }
// fmt.Println("COULD NOT CONTINUE")
return
}
}
strVal = fmt.Sprint(addrLocation)
return
}
type schemaMap map[string]interface{}
func (s schemaMap) hasKey(key string) bool {
if _, ok := s[key]; ok {
return true
}
return false
}
func (s schemaMap) getType() JSONType {
if s.hasKey("type") {
t, _ := parseJSONType(s["type"].(string))
return t
}
return InvalidType
}
func (s schemaMap) isLeaf() bool {
switch s.getType() {
case Object, Array, InvalidType:
return false
}
return true
}
func (s schemaMap) toInfo(address string, values valuesMap, required bool) (vi ValueInfo) {
addressParts := strings.Split(address, ".")
vi.Key = addressParts[len(addressParts)-1]
vi.Address = address
vi.Required = required
if s.hasKey("description") {
vi.Description = s["description"].(string)
}
vi.Type = s.getType()
vi.Default = values.lookupDefault(addressParts[2:]...)
if vi.Default == "" {
vi.Default = `""`
}
vi.Default = strings.TrimSpace(vi.Default)
return
}
type ValuesSet map[string]ValueInfo
func (vs ValuesSet) RenderWith(templ string, wr io.Writer) error {
tpl, err := template.New("values").Funcs(sprig.FuncMap()).Parse(templ)
if err != nil {
return err
}
if err := tpl.Execute(wr, vs); err != nil {
return err
}
return nil
}
func (vs ValuesSet) Render() []byte {
var buf bytes.Buffer
vs.RenderWith(defaultTemplate, &buf)
return buf.Bytes()
}
func getSchemaFilePath(path []string) (schemaPath string, e error) {
schemaDir := "."
schemaFile := defaultSchemaFile
if len(path) > 0 {
if fileInfo, err := os.Stat(path[0]); err != nil {
if fileInfo.IsDir() {
schemaDir = path[0]
} else {
schemaPath = path[0]
}
} else if os.IsNotExist(err) {
e = err
return
}
}
if schemaPath == "" {
schemaPath = fmt.Sprintf("%s%c%s", schemaDir, os.PathSeparator, schemaFile)
}
return
}
func getValuesFilePath(path []string) (valuesPath string, e error) {
valuesDir := "."
valuesFile := defaultValuesFile
if len(path) > 1 {
if fileInfo, err := os.Stat(path[1]); err != nil {
if fileInfo.IsDir() {
valuesDir = path[1]
} else {
valuesPath = path[1]
}
} else if os.IsNotExist(err) {
e = err
return
}
}
if valuesPath == "" {
valuesPath = fmt.Sprintf("%s%c%s", valuesDir, os.PathSeparator, valuesFile)
}
return
}
func recurseSchema(
address string,
schemaPortion schemaMap,
values valuesMap,
vs ValuesSet,
required bool,
) {
if schemaPortion.isLeaf() {
info := schemaPortion.toInfo(address, values, required)
vs[address] = info
return
}
if schemaPortion.getType() == Object {
if schemaPortion.hasKey("properties") {
for prop, document := range schemaPortion["properties"].(map[string]interface{}) {
req := false
if schemaPortion.hasKey("required") {
for _, rprop := range schemaPortion["required"].([]interface{}) {
if rprop.(string) == prop {
req = true
break
}
}
}
recurseSchema(fmt.Sprintf("%s.%s", address, prop), document.(map[string]interface{}), values, vs, req)
}
}
}
}
func schemaValidation(schemaContent, valuesContent []byte) error {
sl := gjs.NewSchemaLoader()
sl.Validate = true
compiledSchema, err := sl.Compile(gjs.NewStringLoader(string(schemaContent)))
if err != nil {
return err
}
if result, err := compiledSchema.Validate(gjs.NewStringLoader(string(valuesContent))); err != nil {
return err
} else if !result.Valid() {
return fmt.Errorf("Values file is not valid: %s", result.Errors())
}
return nil
}
func ParseSchemaFile(path ...string) (ValuesSet, error) {
schemaPath, err := getSchemaFilePath(path)
if err != nil {
return nil, err
}
schemaContent, err := ioutil.ReadFile(schemaPath)
if err != nil {
return nil, err
}
valuesPath, err := getValuesFilePath(path)
if err != nil {
return nil, err
}
valuesContent, err := ioutil.ReadFile(valuesPath)
if err != nil {
return nil, err
}
valuesJSONContent, err := yaml.YAMLToJSON(valuesContent)
if err != nil {
return nil, err
}
// This will allow us to make assumptions about the schema structure and format.
if err := schemaValidation(schemaContent, valuesJSONContent); err != nil {
return nil, err
}
var schema schemaMap
if err := json.Unmarshal(schemaContent, &schema); err != nil {
return nil, err
}
var values valuesMap
if err := json.Unmarshal(valuesJSONContent, &values); err != nil {
return nil, err
}
vs := ValuesSet{}
recurseSchema(fmt.Sprintf(".%s", rootAddressPart), schema, values, vs, true)
return vs, nil
}