Skip to content

Commit

Permalink
fix formatting for k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Nov 29, 2024
1 parent b740414 commit 00bf32e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ go 1.21
require (
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -16,3 +18,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
23 changes: 13 additions & 10 deletions pkg/shcv/shcv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"strings"

"gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
)

// ValueRef represents a Helm value reference found in templates.
Expand Down Expand Up @@ -222,32 +222,35 @@ func (c *Chart) ProcessReferences() {
for i := range c.ValuesFiles {
file := &c.ValuesFiles[i] // Get pointer to existing ValueFile

// iterate over each reference
// iterate over each template reference
for _, ref := range templateRefs {
// Always set the value, whether it exists or not
setNestedValue(file.Values, ref.Path, ref.DefaultValue)
file.Changed = true
// Only set the value if it doesn't already exist or has a default value
if !valueExists(file.Values, ref.Path) {
setNestedValue(file.Values, ref.Path, ref.DefaultValue)
file.Changed = true
}
}
}
}

// UpdateValueFiles ensures all referenced values exist in values.yaml.
// It adds missing values with appropriate defaults and updates the file atomically.
// It adds missing values with appropriate defaults and updates the file.
// The operation is skipped if no changes are needed.
func (c *Chart) UpdateValueFiles() error {
// iterate over each values file
for i := range c.ValuesFiles {
file := &c.ValuesFiles[i] // Get pointer to existing ValueFile
file := &c.ValuesFiles[i]
if !file.Changed {
continue
}

// Write updated values to files
// Convert to YAML with proper formatting
data, err := yaml.Marshal(file.Values)
if err != nil {
return fmt.Errorf("marshaling values: %w", err)
return fmt.Errorf("encoding values: %w", err)
}

// Write the formatted YAML to file
if err := os.WriteFile(file.Path, data, 0644); err != nil {
return fmt.Errorf("writing values file: %w", err)
}
Expand Down Expand Up @@ -285,7 +288,7 @@ func setNestedValue(values map[string]any, path string, value string) {
current[parts[len(parts)-1]] = value
}

// Helper function to check if a value exists at the given path
// valueExists is a function to check if a value exists in the values map at the given path
func valueExists(values map[string]any, path string) bool {
current := values
parts := strings.Split(path, ".")
Expand Down
2 changes: 1 addition & 1 deletion pkg/shcv/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shcv

// Version is the current version of shcv
const Version = "1.0.5"
const Version = "1.0.6"

0 comments on commit 00bf32e

Please sign in to comment.