From 868a05e669a59366221b737ef54df893719157da Mon Sep 17 00:00:00 2001 From: Kalle Fagerberg Date: Wed, 27 Mar 2024 16:42:47 +0100 Subject: [PATCH] Added validation --- pkg/config/yamlpath.go | 6 +++++- pkg/patch/patch.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/config/yamlpath.go b/pkg/config/yamlpath.go index 1d4edc5..cca0e5a 100644 --- a/pkg/config/yamlpath.go +++ b/pkg/config/yamlpath.go @@ -19,6 +19,7 @@ package config import ( "encoding" + "fmt" "github.com/invopop/jsonschema" "github.com/spf13/pflag" @@ -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 diff --git a/pkg/patch/patch.go b/pkg/patch/patch.go index a47ff47..da35255 100644 --- a/pkg/patch/patch.go +++ b/pkg/patch/patch.go @@ -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 @@ -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 @@ -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)