1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-10 01:46:55 +00:00
kyverno/cmd/cli/kubectl-kyverno/utils/values/load.go
Charles-Edouard Brétéché 26e5bd76c7
fix: refactor cli values loading and remove dead code (#7739)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-07-03 18:52:05 +05:30

37 lines
691 B
Go

package values
import (
"encoding/json"
"io"
"os"
"github.com/go-git/go-billy/v5"
"k8s.io/apimachinery/pkg/util/yaml"
)
func readFile(f billy.Filesystem, filepath string) ([]byte, error) {
if f != nil {
filep, err := f.Open(filepath)
if err != nil {
return nil, err
}
return io.ReadAll(filep)
}
return os.ReadFile(filepath)
}
func Load(f billy.Filesystem, filepath string) (*Values, error) {
yamlBytes, err := readFile(f, filepath)
if err != nil {
return nil, err
}
jsonBytes, err := yaml.ToJSON(yamlBytes)
if err != nil {
return nil, err
}
vals := &Values{}
if err := json.Unmarshal(jsonBytes, vals); err != nil {
return nil, err
}
return vals, nil
}