Skip to content

Commit

Permalink
feat: add displaying default values (#31)
Browse files Browse the repository at this point in the history
* feat: add displaying default values

* changed color of default and added release notes
  • Loading branch information
Skarlso authored Nov 17, 2023
1 parent bef274c commit b212114
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 3 additions & 0 deletions docs/release_notes/v0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v0.1.2 Release

Display default values.
2 changes: 1 addition & 1 deletion pkg/fetcher/fetch_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
)

// Fetcher wraps an http client.
// Fetcher wraps an HTTP client.
type Fetcher struct {
client *http.Client
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ type Property struct {
Format string
Indent int
Version string
Default string
Required bool
Properties []*Property
}
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions pkg/templates/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ <h1>
<details class="collapse-panel">
<summary class="collapse-header position-relative">
{{$v.Name}} <kbd class="text-muted">{{$v.Type}}</kbd>
{{if .Format}}
{{if $v.Format}}
<kbd class="text-muted">{{.Format}}</kbd>
{{end}}
{{if .Patterns}}
{{if $v.Patterns}}
<kbd class="text-muted">{{.Patterns}}</kbd>
{{end}}
{{if $v.Default}}
<kbd class="text-primary">{{$v.Default}}</kbd>
{{end}}
{{if $v.Required}}
<span class="badge badge-primary">required</span>
{{end}}
Expand Down Expand Up @@ -105,6 +108,9 @@ <h1>
{{if .Patterns}}
<kbd class="text-muted">{{.Patterns}}</kbd>
{{end}}
{{if .Default}}
<kbd class="text-primary">{{.Default}}</kbd>
{{end}}
{{if .Required}}
<span class="badge badge-primary">required</span>
{{end}}
Expand Down

0 comments on commit b212114

Please sign in to comment.