mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
35 lines
708 B
Go
35 lines
708 B
Go
|
package context
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"os"
|
||
|
|
||
|
"github.com/go-git/go-billy/v5"
|
||
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/v1alpha1"
|
||
|
"k8s.io/apimachinery/pkg/util/yaml"
|
||
|
)
|
||
|
|
||
|
func Load(f billy.Filesystem, filepath string) (*v1alpha1.Context, error) {
|
||
|
yamlBytes, err := readFile(f, filepath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
vals := &v1alpha1.Context{}
|
||
|
if err := yaml.UnmarshalStrict(yamlBytes, vals); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return vals, nil
|
||
|
}
|
||
|
|
||
|
func readFile(f billy.Filesystem, filepath string) ([]byte, error) {
|
||
|
if f != nil {
|
||
|
file, err := f.Open(filepath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer file.Close()
|
||
|
return io.ReadAll(file)
|
||
|
}
|
||
|
return os.ReadFile(filepath)
|
||
|
}
|