Skip to content

Commit

Permalink
make docs and schema working
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Aug 14, 2024
1 parent c899c1f commit 91e7675
Show file tree
Hide file tree
Showing 5 changed files with 2,304 additions and 30 deletions.
34 changes: 20 additions & 14 deletions hack/create-zarf-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

set -euo pipefail

# Create the json schema for the zarf.yaml
go run main.go internal gen-config-schema > zarf.schema.json
add_yaml_extensions() {
local input_file=$1
local dst_folder="schema"

# Adds pattern properties to all definitions to allow for yaml extensions
jq '
def addPatternProperties:
. +
if has("properties") then
{"patternProperties": {"^x-": {}}}
else
{}
end;
jq '
def addPatternProperties:
. +
if has("properties") then
{"patternProperties": {"^x-": {}}}
else
{}
end;
walk(if type == "object" then addPatternProperties else . end)
' zarf.schema.json > temp_zarf.schema.json
walk(if type == "object" then addPatternProperties else . end)
' "$input_file" > "$dst_folder/$input_file"
rm "$input_file"
}

mv temp_zarf.schema.json zarf.schema.json
go run schema/src/main.go v1alpha1 > "zarf_package_v1alpha1.schema.json"
go run schema/src/main.go v1beta1 > "zarf_package_v1beta1.schema.json"

add_yaml_extensions "zarf_package_v1alpha1.schema.json"
add_yaml_extensions "zarf_package_v1beta1.schema.json"
2 changes: 2 additions & 0 deletions schema/src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zarf-dev/zarf v0.38.2 h1:1MPhFeFp2orXuN1Xzb5pSY88h6hK1ZHvNj5rXHGV+14=
github.com/zarf-dev/zarf v0.38.2/go.mod h1:R3yCUkrGU4zIXcYG1Vi7GJi3+enNdXFXWhLypT4yS5o=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
31 changes: 15 additions & 16 deletions schema/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@ import (
"github.com/zarf-dev/zarf/src/api/v1beta1"
)

func addGoComments(reflector *jsonschema.Reflector, apiVersion string) error {
// Get the file path of the currently executing file
_, filename, _, ok := runtime.Caller(0)
if !ok {
return errors.New("error getting file path")
}

typePackagePath := filepath.Join(filename, "..", "..", "..", "src", "api", apiVersion)
if err := reflector.AddGoComments("github.com/zarf-dev/zarf", typePackagePath); err != nil {
return err
}

return nil
}

var apiVersionToObject = map[string]interface{}{
"v1alpha1": &v1alpha1.ZarfPackage{},
"v1beta1": &v1beta1.ZarfPackage{},
}

func genSchema(apiVersion string) (string, error) {
reflector := jsonschema.Reflector(jsonschema.Reflector{ExpandedStruct: true})
if err := addGoComments(&reflector, apiVersion); err != nil {

// AddGoComments breaks if called with a absolute path, so we move to the directory of the go executable
// then use a relative path to the package
_, filename, _, ok := runtime.Caller(1)
if !ok {
return "", errors.New("unable to get the current filename")
}
goExecDir := filepath.Dir(filename)
if err := os.Chdir(goExecDir); err != nil {
return "", err
}

typePackagePath := filepath.Join("..", "..", "src", "api", apiVersion)

if err := reflector.AddGoComments("github.com/zarf-dev/zarf/schema/src", typePackagePath); err != nil {
return "", err
}

Expand Down
Loading

0 comments on commit 91e7675

Please sign in to comment.