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

IZE-598 added generation of markdown files with IZE specification #491

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 65 additions & 0 deletions internal/commands/doc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package commands

import (
"github.com/hazelops/ize/internal/schema"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"os"
"path/filepath"
"strings"
"text/template"
)

func NewCmdDoc() *cobra.Command {
Expand All @@ -25,6 +31,26 @@ func NewCmdDoc() *cobra.Command {
return err
}

sections := []string{"main", "terraform", "ecs", "alias", "serverless", "tunnel"}

err = os.MkdirAll("./website/schema", 0777)
if err != nil {
return err
}

t := template.New("schema")
t, err = t.Parse(sectionTmpl)
if err != nil {
return err
}

for _, s := range sections {
err := generateSection(s, t)
if err != nil {
return err
}
}

pterm.Success.Printfln("Docs generated")

return nil
Expand All @@ -33,3 +59,42 @@ func NewCmdDoc() *cobra.Command {

return cmd
}

func generateSection(name string, t *template.Template) error {
s := schema.GetSchema()
filename := "README.md"
if name != "main" {
filename = strings.ToUpper(name) + ".md"
s = schema.GetSchema()[name].Items
}
f, err := os.Create(filepath.Join(".", "website", "schema", filename))
if err != nil {
return err
}

err = t.Execute(f, Section{
Name: cases.Title(language.Und).String(name),
Items: s,
})
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}

return nil
}

type Section struct {
Name string
schema.Items
}

var sectionTmpl = `# {{.Name}} Section
| Parameter | Required | Description |
| --- | --- | --- |
{{range $k, $v := .Items}}| {{$k}} | {{if $v.Required}}yes{{else}}no {{end}}| {{$v.Description}} |
psihachina marked this conversation as resolved.
Show resolved Hide resolved
{{end}}
`
6 changes: 3 additions & 3 deletions internal/schema/ize-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"$ref": "#/definitions/app"
}
},
"description": "Apps configuration.",
"description": "(deprecated) Apps configuration.",
"additionalProperties": false
},
"ecs": {
Expand Down Expand Up @@ -205,7 +205,7 @@
"description": "(optional) Terraform-specific AWS profile (optional) can be specified here (but normally it should be inherited from a global AWS_PROFILE)."
}
},
"description": "Infrastructure configuration.",
"description": "(deprecated) Infrastructure configuration.",
"additionalProperties": false
},
"tunnel": {
Expand Down Expand Up @@ -308,7 +308,7 @@
"description": "(optional) expresses startup and shutdown dependencies between apps"
}
},
"description": "App configuration.",
"description": "(deprecated) App configuration.",
"additionalProperties": false
},
"ecs": {
Expand Down
60 changes: 60 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/santhosh-tekuri/jsonschema"
"github.com/xeipuuv/gojsonschema"
"golang.org/x/exp/slices"
"strings"

// Enable support for embedded static resources
Expand Down Expand Up @@ -55,3 +56,62 @@ func GetJsonSchema() interface{} {

return json
}

func GetSchema() Items {
compiler := jsonschema.NewCompiler()
compiler.Draft = jsonschema.Draft7
if err := compiler.AddResource("schema.json", strings.NewReader(Schema)); err != nil {
panic(err)
}
compiler.ExtractAnnotations = true
schema, err := compiler.Compile("schema.json")
if err != nil {
panic(err)
}

items := Items{}
getProperties(items, schema)

return items
}

func getProperties(items Items, schema *jsonschema.Schema) {
for k, v := range schema.Properties {
if strings.Contains(v.Description, "deprecated") {
continue
}
if !slices.Contains(v.Types, "object") {
r := slices.Contains(schema.Required, k)
items[k] = Item{
Default: v.Default,
Required: r,
Description: v.Description,
}
} else {
r := slices.Contains(schema.Required, k)
i := Items{}
if len(v.PatternProperties) == 0 {
getProperties(i, v)
} else {
for _, p := range v.PatternProperties {
getProperties(i, p.Ref)
}
}
items[k] = Item{
Default: v.Default,
Required: r,
Description: v.Description,
Items: i,
}
}
}
}

type Item struct {
Default interface{}
Required bool
Description string
Items Items
}

type Items map[string]Item