Skip to content

Commit

Permalink
Added validation
Browse files Browse the repository at this point in the history
  • Loading branch information
applejag committed Mar 27, 2024
1 parent cf85744 commit 868a05e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/config/yamlpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config

import (
"encoding"
"fmt"

"github.com/invopop/jsonschema"
"github.com/spf13/pflag"
Expand All @@ -36,13 +37,16 @@ var _ encoding.TextUnmarshaler = &YAMLPathPattern{}
var _ jsonSchemaInterface = YAMLPathPattern{}

func (r *YAMLPathPattern) String() string {
if r == nil {
return ""
}
return r.Source
}

func (r *YAMLPathPattern) Set(value string) error {
path, err := yamlpath.NewPath(value)
if err != nil {
return err
return fmt.Errorf("parse yamlPath: %w", err)
}
r.YAMLPath = path
r.Source = value
Expand Down
24 changes: 24 additions & 0 deletions pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func Apply(repoDir string, patch config.PackageRepoPatch, tmplCtx TemplateContex
func applyRegexPatch(fstore FileStore, tmplCtx TemplateContext, patch config.PatchRegex) error {
log.Debug().Str("file", patch.File).Stringer("match", patch.Match).Msg("Patching regex.")

if patch.File == "" {
return fmt.Errorf("missing required field 'file'")
}
if patch.Match == nil {
return fmt.Errorf("missing required field 'match'")
}
if patch.Replace == nil {
return fmt.Errorf("missing required field 'replace'")
}

content, err := fstore.ReadFile(patch.File)
if err != nil {
return err
Expand Down Expand Up @@ -131,6 +141,16 @@ func regexSubmatchIndicesToStrings(line []byte, indices []int) []string {
func applyYAMLPatch(fstore FileStore, tmplCtx TemplateContext, patch config.PatchYAML) error {
log.Debug().Str("file", patch.File).Stringer("yamlpath", patch.YAMLPath).Msg("Patching YAML.")

if patch.File == "" {
return fmt.Errorf("missing required field 'file'")
}
if patch.YAMLPath == nil {
return fmt.Errorf("missing required field 'yamlPath'")
}
if patch.Replace == nil {
return fmt.Errorf("missing required field 'replace'")
}

content, err := fstore.ReadFile(patch.File)
if err != nil {
return err
Expand Down Expand Up @@ -200,6 +220,10 @@ func yamlEncode(obj any, indent int) ([]byte, error) {
}

func applyHelmDepUpdatePatch(repoDir string, tmplCtx TemplateContext, patch config.PatchHelmDepUpdate) error {
if patch.Chart == nil {
return fmt.Errorf("missing required field 'chart'")
}

chart, err := patch.Chart.ExecuteString(tmplCtx)
if err != nil {
return fmt.Errorf("execute chart dir template: %w", err)
Expand Down

0 comments on commit 868a05e

Please sign in to comment.