From b2121147d142a0213a3a086b01558bf0e6b6b9bb Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:15:03 +0100 Subject: [PATCH] feat: add displaying default values (#31) * feat: add displaying default values * changed color of default and added release notes --- cmd/generate.go | 2 +- docs/release_notes/v0.1.2.md | 3 +++ pkg/fetcher/fetch_content.go | 2 +- pkg/generate.go | 4 ++++ pkg/server.go | 5 +++++ pkg/templates/view.html | 10 ++++++++-- 6 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 docs/release_notes/v0.1.2.md diff --git a/cmd/generate.go b/cmd/generate.go index 5bb0792..fb2c7a7 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -7,12 +7,12 @@ import ( "os" "path/filepath" - "github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher" "github.com/spf13/cobra" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" "k8s.io/apimachinery/pkg/util/yaml" "github.com/Skarlso/crd-to-sample-yaml/pkg" + "github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher" ) var ( diff --git a/docs/release_notes/v0.1.2.md b/docs/release_notes/v0.1.2.md new file mode 100644 index 0000000..079a8a9 --- /dev/null +++ b/docs/release_notes/v0.1.2.md @@ -0,0 +1,3 @@ +# v0.1.2 Release + +Display default values. \ No newline at end of file diff --git a/pkg/fetcher/fetch_content.go b/pkg/fetcher/fetch_content.go index ec71170..b9d7c82 100644 --- a/pkg/fetcher/fetch_content.go +++ b/pkg/fetcher/fetch_content.go @@ -6,7 +6,7 @@ import ( "net/http" ) -// Fetcher wraps an http client. +// Fetcher wraps an HTTP client. type Fetcher struct { client *http.Client } diff --git a/pkg/generate.go b/pkg/generate.go index d7719d8..b9ef183 100644 --- a/pkg/generate.go +++ b/pkg/generate.go @@ -104,6 +104,10 @@ func parseProperties(group, version, kind string, properties map[string]v1beta1. // outputValueType generate an output value based on the given type. func outputValueType(v v1beta1.JSONSchemaProps) string { + if v.Default != nil { + return string(v.Default.Raw) + } + switch v.Type { case "string": return "string" diff --git a/pkg/server.go b/pkg/server.go index b84c028..ef8833c 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -218,6 +218,7 @@ type Property struct { Format string Indent int Version string + Default string Required bool Properties []*Property } @@ -256,6 +257,10 @@ func parseCRD(properties map[string]v1beta1.JSONSchemaProps, version string, req Version: version, Required: required, } + if v.Default != nil { + p.Default = string(v.Default.Raw) + } + if len(properties[k].Properties) > 0 && properties[k].AdditionalProperties == nil { requiredList = v.Required out, err := parseCRD(properties[k].Properties, version, requiredList) diff --git a/pkg/templates/view.html b/pkg/templates/view.html index 9a927d1..1d49e24 100644 --- a/pkg/templates/view.html +++ b/pkg/templates/view.html @@ -52,12 +52,15 @@

{{$v.Name}} {{$v.Type}} - {{if .Format}} + {{if $v.Format}} {{.Format}} {{end}} - {{if .Patterns}} + {{if $v.Patterns}} {{.Patterns}} {{end}} + {{if $v.Default}} + {{$v.Default}} + {{end}} {{if $v.Required}} required {{end}} @@ -105,6 +108,9 @@

{{if .Patterns}} {{.Patterns}} {{end}} + {{if .Default}} + {{.Default}} + {{end}} {{if .Required}} required {{end}}