Skip to content

Commit

Permalink
feat: add file values provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mikel-jason committed May 4, 2023
1 parent de1901b commit 28c59b3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
8 changes: 6 additions & 2 deletions assembly/local/Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
version: '3'

tasks:
nginx:
run:
desc: run as helm plugin directly from go code
# examples:
# task local:run -- simple --chart bitnami/nginx --file ./testdata/bitnami.nginx.yaml
# task local:run -- simple --chart bitnami/nginx --stdin < <(cat ./testdata/bitnami.nginx.yaml)
env:
HELM_PLUGINS: "{{ .INJECT_HELM_PLUGINS }}"
cmds:
- cat ./testdata/bitnami.nginx.yaml | helm local {{ .CLI_ARGS }}
- helm local {{ .CLI_ARGS }}
44 changes: 39 additions & 5 deletions cmd/helm-clean-values/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"fmt"

"github.com/sarcaustech/helm-clean-values/pkg/core"
"github.com/sarcaustech/helm-clean-values/pkg/core/adapters/providers/file"
"github.com/sarcaustech/helm-clean-values/pkg/core/adapters/providers/helm"
"github.com/sarcaustech/helm-clean-values/pkg/core/adapters/providers/stdin"
"github.com/sarcaustech/helm-clean-values/pkg/core/adapters/selectors/simple"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)

var simpleInputMethod string // centralize once more than one selector cmd

var simpleCmd = &cli.Command{
Name: "simple",
Usage: "detect unused helm values by comparing with the chart's default values",
Expand All @@ -19,27 +22,58 @@ var simpleCmd = &cli.Command{
Name: "stdin",
Usage: "read input values from STDIN",
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "filepath to read input values from",
},
&cli.StringFlag{
Name: "chart",
Usage: "helm prompt to get the chart",
},
},
Before: func(cCtx *cli.Context) error {
if !cCtx.Bool("stdin") {
methods := 0

if cCtx.Bool("stdin") {
methods++
simpleInputMethod = "stdin"
}
if cCtx.String("file") != "" {
methods++
simpleInputMethod = "file"
}

if methods > 1 {
return fmt.Errorf("too input values provided, expected only one method")
}
if methods == 0 {
return fmt.Errorf("no input values provided")
}

return nil
},
Action: func(cCtx *cli.Context) (err error) {

inputProvider := stdin.ValuesProvider{}
referenceProvider := helm.ValuesProvider{
var inputProvider core.ValuesProvider
switch simpleInputMethod {
case "stdin":
inputProvider = &stdin.ValuesProvider{}
case "file":
inputProvider = &file.ValuesProvider{
Path: cCtx.String("file"),
}
default:
return fmt.Errorf("unknown input method %s", simpleInputMethod)
}

referenceProvider := &helm.ValuesProvider{
BinaryPath: cCtx.String("helm-bin"),
Prompt: cCtx.String("chart"),
}
selector := simple.Selector{}
selector := &simple.Selector{}

cleanedValues, err := core.Run(&inputProvider, &referenceProvider, &selector)
cleanedValues, err := core.Run(inputProvider, referenceProvider, selector)
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/core/adapters/providers/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package file

import (
"fmt"
"os"

"gopkg.in/yaml.v3"
)

type ValuesProvider struct {
Path string
}

func (p *ValuesProvider) Values() (map[string]interface{}, error) {
bytes, err := os.ReadFile(p.Path)
if err != nil {
return nil, fmt.Errorf("cannot not read file %s: %w", p.Path, err)
}

var data map[string]interface{}
err = yaml.Unmarshal(bytes, &data)
if err != nil {
return nil, fmt.Errorf("cannot parse file contents to YAML %s: %w", p.Path, err)
}
return data, nil
}

0 comments on commit 28c59b3

Please sign in to comment.