Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Nov 28, 2024
1 parent 0e3a5a0 commit c7f2eb0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/agentstation/uuidkey/ci.yaml?style=flat-square)](https://github.com/agentstation/uuidkey/actions)
[![codecov](https://codecov.io/gh/agentstation/shcv/branch/master/graph/badge.svg?token=7A0O794SOM)](https://codecov.io/gh/agentstation/shcv)
[![License](https://img.shields.io/github/license/agentstation/shcv.svg)](LICENSE)
[![Version](https://img.shields.io/badge/version-1.0.3-blue.svg)](https://github.com/agentstation/shcv/releases)
[![Version](https://img.shields.io/badge/version-1.0.4-blue.svg)](https://github.com/agentstation/shcv/releases)
[![Go Version](https://img.shields.io/badge/go-%3E%3D%201.21-blue)](go.mod)

`shcv` is a command-line tool and Go package that helps maintain Helm chart values by automatically synchronizing `values.yaml` with the parameters used in your Helm templates. It scans all template files for `{{ .Values.* }}` expressions and ensures they are properly defined in your values file.
Expand Down
4 changes: 2 additions & 2 deletions pkg/shcv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Package shcv provides functionality to synchronize Helm chart values by analyzin

The package helps maintain Helm charts by automatically detecting all \{\{ .Values.\* \}\} expressions in template files and ensuring they are properly defined in the values file. It uses atomic file operations to ensure data integrity and provides robust error handling.

Version: 1.0.3 Requires: Go 1.21 or later
Version: 1.0.4 Requires: Go 1.21 or later

Basic usage:

Expand Down Expand Up @@ -93,7 +93,7 @@ Error Handling:
<a name="Version"></a>Version is the current version of the shcv package

```go
const Version = "1.0.3"
const Version = "1.0.4"
```

<a name="Chart"></a>
Expand Down
2 changes: 1 addition & 1 deletion pkg/shcv/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The package helps maintain Helm charts by automatically detecting all {{ .Values
expressions in template files and ensuring they are properly defined in the values file.
It uses atomic file operations to ensure data integrity and provides robust error handling.
Version: 1.0.3
Version: 1.0.4
Requires: Go 1.21 or later
Basic usage:
Expand Down
52 changes: 26 additions & 26 deletions pkg/shcv/shcv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Version is the current version of the shcv package
const Version = "1.0.3"
const Version = "1.0.4"

// ValueRef represents a Helm value reference found in templates.
// It tracks where values are used in templates and their default values if specified.
Expand Down Expand Up @@ -176,6 +176,9 @@ func (c *Chart) FindTemplates() error {
// It identifies both simple references and those with default values.
// The references are stored in the Chart's References slice.
func (c *Chart) ParseTemplates() error {
// Map to track the last default value for each path
lastDefaults := make(map[string]string)

for _, template := range c.Templates {
content, err := os.ReadFile(template)
if err != nil {
Expand All @@ -184,56 +187,53 @@ func (c *Chart) ParseTemplates() error {

lines := strings.Split(string(content), "\n")
for lineNum, line := range lines {
// Track all default values found in this line
defaultsInLine := make(map[string]string)

// Check for values with double-quoted defaults
matches := defaultRegex.FindAllStringSubmatch(line, -1)
for _, match := range matches {
c.References = append(c.References, ValueRef{
Path: match[1],
DefaultValue: match[2],
SourceFile: template,
LineNumber: lineNum + 1,
})
defaultsInLine[match[1]] = match[2]
}

// Check for values with single-quoted defaults
matches = defaultSingleQuoteRegex.FindAllStringSubmatch(line, -1)
for _, match := range matches {
c.References = append(c.References, ValueRef{
Path: match[1],
DefaultValue: match[2],
SourceFile: template,
LineNumber: lineNum + 1,
})
defaultsInLine[match[1]] = match[2]
}

// Check for values with numeric defaults
matches = defaultNumericRegex.FindAllStringSubmatch(line, -1)
for _, match := range matches {
// Convert numeric default to string
c.References = append(c.References, ValueRef{
Path: match[1],
DefaultValue: match[2],
SourceFile: template,
LineNumber: lineNum + 1,
})
defaultsInLine[match[1]] = match[2]
}

// Check for values without defaults
// Update lastDefaults with any new defaults found in this line
for path, value := range defaultsInLine {
lastDefaults[path] = value
}

// Check for all value references (with or without defaults)
matches = valueRegex.FindAllStringSubmatch(line, -1)
for _, match := range matches {
// Skip if we already found this with a default value
path := match[1]
// Use the last known default value for this path, if any
defaultValue := lastDefaults[path]

// Skip if we already found this exact reference
found := false
for _, ref := range c.References {
if ref.Path == match[1] && ref.SourceFile == template && ref.LineNumber == lineNum+1 {
if ref.Path == path && ref.SourceFile == template && ref.LineNumber == lineNum+1 {
found = true
break
}
}
if !found {
c.References = append(c.References, ValueRef{
Path: match[1],
SourceFile: template,
LineNumber: lineNum + 1,
Path: path,
DefaultValue: defaultValue,
SourceFile: template,
LineNumber: lineNum + 1,
})
}
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/shcv/shcv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,46 @@ spec:
},
},
},
{
name: "multiple defaults for same value",
template: `
metadata:
domain: {{ .Values.domain | default "example.com" }}
spec:
url: {{ .Values.domain | default "api.example.com" }}`,
want: []ValueRef{
{
Path: "domain",
DefaultValue: "example.com",
LineNumber: 3,
},
{
Path: "domain",
DefaultValue: "api.example.com",
LineNumber: 5,
},
},
},
{
name: "multiple defaults in same line",
template: `
spec:
urls:
- {{ .Values.domain | default "example.com" }}/api
- {{ .Values.domain | default "api.example.com" }}/auth`,
want: []ValueRef{
{
Path: "domain",
DefaultValue: "example.com",
LineNumber: 4,
},
{
Path: "domain",
DefaultValue: "api.example.com",
LineNumber: 5,
},
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -203,6 +243,53 @@ spec:
}
}

func TestDefaultValues(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "helm-test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

templatesDir := filepath.Join(tmpDir, "templates")
require.NoError(t, os.MkdirAll(templatesDir, 0755))

// Create a template with multiple default values
templateContent := `
apiVersion: v1
kind: Service
metadata:
domain: {{ .Values.domain | default "example.com" }}
spec:
url: {{ .Values.domain | default "api.example.com" }}`

err = os.WriteFile(filepath.Join(templatesDir, "service.yaml"), []byte(templateContent), 0644)
require.NoError(t, err)

chart, err := NewChart(tmpDir, nil)
require.NoError(t, err)

err = chart.FindTemplates()
require.NoError(t, err)

err = chart.ParseTemplates()
require.NoError(t, err)

err = chart.LoadValues()
require.NoError(t, err)

err = chart.UpdateValues()
require.NoError(t, err)

// Read the generated values.yaml
data, err := os.ReadFile(filepath.Join(tmpDir, "values.yaml"))
require.NoError(t, err)

var values map[string]interface{}
err = yaml.Unmarshal(data, &values)
require.NoError(t, err)

// Verify that the last default value was used
assert.Equal(t, "api.example.com", values["domain"])
}

func TestValuesHandling(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "helm-test")
require.NoError(t, err)
Expand Down

0 comments on commit c7f2eb0

Please sign in to comment.