shcv

package
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 8 Imported by: 0

README

shcv

import "github.com/agentstation/shcv/pkg/shcv"

Package shcv provides functionality to synchronize Helm chart values by analyzing template files and updating values files accordingly.

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

Basic usage:

chart, err := shcv.NewChart("./my-chart")
if err != nil {
	log.Fatal(err)
}

// Process the chart
if err := chart.LoadValueFiles(); err != nil {
	log.Fatal(err)
}
if err := chart.FindTemplates(); err != nil {
	log.Fatal(err)
}
if err := chart.ParseTemplates(); err != nil {
	log.Fatal(err)
}
if err := chart.ProcessReferences(); err != nil {
	log.Fatal(err)
}
if err := chart.UpdateValueFiles(); err != nil {
	log.Fatal(err)
}

Configuration options:

chart, err := shcv.NewChart("./my-chart",
	shcv.WithValuesFileNames([]string{"values.yaml", "values-prod.yaml"}),
	shcv.WithTemplatesDir("custom-templates"),
	shcv.WithVerbose(true),
)

Key features:

  • Detects all Helm value references in template files
  • Supports multiple values files
  • Supports nested value structures
  • Handles default values in templates
  • Creates missing values with their default values
  • Preserves existing values, structure, and data types (e.g., numbers, strings)
  • Provides line number and source file tracking
  • Uses atomic file operations
  • Provides robust error handling

Error handling:

  • Invalid chart directory structure
  • Missing or inaccessible templates directory
  • Permission issues with values files
  • Invalid YAML syntax
  • Concurrent file access conflicts

Requirements:

  • Go 1.21 or later
  • Valid Helm chart directory structure
  • Read/write permissions for chart directory

Index

Constants

Version is the current version of shcv

const Version = "1.0.6"

type Chart

Chart represents a Helm chart structure and manages its values and templates. It provides functionality to scan templates for value references and ensure all referenced values are properly defined in values.yaml.

type Chart struct {
    // Dir is the root directory of the chart
    Dir string
    // ValuesFiles is the path to values.yaml
    ValuesFiles []ValueFile
    // References tracks all .Values references found in templates
    References []ValueRef
    // Templates lists all discovered template files
    Templates []string
    // contains filtered or unexported fields
}

func NewChart
func NewChart(dir string, opts ...Option) (*Chart, error)

NewChart creates a new Chart instance for the given directory.

func (*Chart) FindTemplates
func (c *Chart) FindTemplates() error

FindTemplates discovers all template files in the chart's templates directory. It looks for files with .yaml, .yml, or .tpl extensions. Returns an error if the templates directory cannot be accessed.

func (*Chart) LoadValueFiles
func (c *Chart) LoadValueFiles() error

LoadValueFiles loads the current values from the value files provided. If the file doesn't exist, an empty values map is initialized. Returns an error if the file exists but cannot be read or parsed.

func (*Chart) ParseTemplates
func (c *Chart) ParseTemplates() error

ParseTemplates scans all discovered templates for .Values references. It identifies both simple references and those with default values. The references are stored in the Chart's References slice.

func (*Chart) ProcessReferences
func (c *Chart) ProcessReferences()

ProcessReferences ensures all referenced values exist in values.yaml.

func (*Chart) UpdateValueFiles
func (c *Chart) UpdateValueFiles() error

UpdateValueFiles ensures all referenced values exist in values.yaml. It adds missing values with appropriate defaults and updates the file. The operation is skipped if no changes are needed.

type Option

Option is a functional option for configuring the Chart processing.

type Option func(*config)

func WithTemplatesDir
func WithTemplatesDir(dir string) Option

WithTemplatesDir sets the templates directory.

func WithValuesFileNames
func WithValuesFileNames(names []string) Option

WithValuesFileNames sets the values file names.

func WithVerbose
func WithVerbose(verbose bool) Option

WithVerbose sets the verbose flag.

type ValueFile

ValueFile represents a values file

type ValueFile struct {
    // Path is the path to the values file
    Path string
    // Values contains the values from the values file
    Values map[string]any
    // Changed indicates whether values were modified during processing
    Changed bool
}

type ValueRef

ValueRef represents a Helm value reference found in templates. It tracks where values are used in templates and their default values if specified.

type ValueRef struct {
    // Path is the full dot-notation path to the value (e.g. "gateway.domain")
    Path string
    // DefaultValue is the value specified in the template using the default function
    DefaultValue string
    // SourceFile is the template file where this reference was found
    SourceFile string
    // LineNumber is the line number in the source file where the reference appears
    LineNumber int
}

func ParseFile
func ParseFile(content, templatePath string) []ValueRef

ParseFile parses a template file and returns all value references

func (*ValueRef) ID
func (v *ValueRef) ID() string

ID returns a unique identifier for the value reference

Generated by gomarkdoc

Documentation

Overview

Package shcv provides functionality to synchronize Helm chart values by analyzing template files and updating values files accordingly.

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

Basic usage:

chart, err := shcv.NewChart("./my-chart")
if err != nil {
	log.Fatal(err)
}

// Process the chart
if err := chart.LoadValueFiles(); err != nil {
	log.Fatal(err)
}
if err := chart.FindTemplates(); err != nil {
	log.Fatal(err)
}
if err := chart.ParseTemplates(); err != nil {
	log.Fatal(err)
}
if err := chart.ProcessReferences(); err != nil {
	log.Fatal(err)
}
if err := chart.UpdateValueFiles(); err != nil {
	log.Fatal(err)
}

Configuration options:

chart, err := shcv.NewChart("./my-chart",
	shcv.WithValuesFileNames([]string{"values.yaml", "values-prod.yaml"}),
	shcv.WithTemplatesDir("custom-templates"),
	shcv.WithVerbose(true),
)

Key features:

  • Detects all Helm value references in template files
  • Supports multiple values files
  • Supports nested value structures
  • Handles default values in templates
  • Creates missing values with their default values
  • Preserves existing values, structure, and data types (e.g., numbers, strings)
  • Provides line number and source file tracking
  • Uses atomic file operations
  • Provides robust error handling

Error handling:

  • Invalid chart directory structure
  • Missing or inaccessible templates directory
  • Permission issues with values files
  • Invalid YAML syntax
  • Concurrent file access conflicts

Requirements:

  • Go 1.21 or later
  • Valid Helm chart directory structure
  • Read/write permissions for chart directory

Index

Constants

View Source
const Version = "1.0.7"

Version is the current version of shcv

Variables

This section is empty.

Functions

This section is empty.

Types

type Chart

type Chart struct {
	// Dir is the root directory of the chart
	Dir string
	// ValuesFiles is the path to values.yaml
	ValuesFiles []ValueFile
	// References tracks all .Values references found in templates
	References []ValueRef
	// Templates lists all discovered template files
	Templates []string
	// contains filtered or unexported fields
}

Chart represents a Helm chart structure and manages its values and templates. It provides functionality to scan templates for value references and ensure all referenced values are properly defined in values.yaml.

func NewChart

func NewChart(dir string, opts ...Option) (*Chart, error)

NewChart creates a new Chart instance for the given directory.

func (*Chart) FindTemplates

func (c *Chart) FindTemplates() error

FindTemplates discovers all template files in the chart's templates directory. It looks for files with .yaml, .yml, or .tpl extensions. Returns an error if the templates directory cannot be accessed.

func (*Chart) LoadValueFiles added in v1.0.6

func (c *Chart) LoadValueFiles() error

LoadValueFiles loads the current values from the value files provided. If the file doesn't exist, an empty values map is initialized. Returns an error if the file exists but cannot be read or parsed.

func (*Chart) ParseTemplates

func (c *Chart) ParseTemplates() error

ParseTemplates scans all discovered templates for .Values references. It identifies both simple references and those with default values. The references are stored in the Chart's References slice.

func (*Chart) ProcessReferences added in v1.0.6

func (c *Chart) ProcessReferences()

ProcessReferences ensures all referenced values exist in values.yaml.

func (*Chart) UpdateValueFiles added in v1.0.6

func (c *Chart) UpdateValueFiles() error

UpdateValueFiles ensures all referenced values exist in values.yaml. It adds missing values with appropriate defaults and updates the file. The operation is skipped if no changes are needed.

type Option added in v1.0.6

type Option func(*config)

Option is a functional option for configuring the Chart processing.

func WithTemplatesDir added in v1.0.6

func WithTemplatesDir(dir string) Option

WithTemplatesDir sets the templates directory.

func WithValuesFileNames added in v1.0.6

func WithValuesFileNames(names []string) Option

WithValuesFileNames sets the values file names.

func WithVerbose added in v1.0.6

func WithVerbose(verbose bool) Option

WithVerbose sets the verbose flag.

type ValueFile added in v1.0.6

type ValueFile struct {
	// Path is the path to the values file
	Path string
	// Values contains the values from the values file
	Values map[string]any
	// Changed indicates whether values were modified during processing
	Changed bool
}

ValueFile represents a values file

type ValueRef

type ValueRef struct {
	// Path is the full dot-notation path to the value (e.g. "gateway.domain")
	Path string
	// DefaultValue is the value specified in the template using the default function
	DefaultValue string
	// SourceFile is the template file where this reference was found
	SourceFile string
	// LineNumber is the line number in the source file where the reference appears
	LineNumber int
}

ValueRef represents a Helm value reference found in templates. It tracks where values are used in templates and their default values if specified.

func ParseFile added in v1.0.6

func ParseFile(content, templatePath string) []ValueRef

ParseFile parses a template file and returns all value references

func (*ValueRef) ID added in v1.0.6

func (v *ValueRef) ID() string

ID returns a unique identifier for the value reference

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL