1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/cmd/cli/kubectl-kyverno/userinfo/userinfo.go
Charles-Edouard Brétéché 2e4bf7ee83
feat: fix user infos used in tests (#8429)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-09-18 10:51:35 +00:00

44 lines
1.2 KiB
Go

package userinfo
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/go-git/go-billy/v5"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/v1alpha1"
"sigs.k8s.io/yaml"
)
func load(fs billy.Filesystem, path string, resourcePath string) ([]byte, error) {
if fs != nil {
file, err := fs.Open(filepath.Join(resourcePath, path))
if err != nil {
return nil, fmt.Errorf("Unable to open userInfo file: %s. \nerror: %s", path, err)
}
bytes, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("Error: failed to read file %s: %w", file.Name(), err)
}
return bytes, err
} else {
bytes, err := os.ReadFile(filepath.Clean(filepath.Join(resourcePath, path)))
if err != nil {
return nil, fmt.Errorf("unable to read yaml (%w)", err)
}
return bytes, err
}
}
func Load(fs billy.Filesystem, path string, resourcePath string) (*v1alpha1.UserInfo, error) {
bytes, err := load(fs, path, resourcePath)
if err != nil {
return nil, fmt.Errorf("unable to read yaml (%w)", err)
}
var userInfo v1alpha1.UserInfo
if err := yaml.UnmarshalStrict(bytes, &userInfo); err != nil {
return nil, fmt.Errorf("failed to decode yaml (%w)", err)
}
return &userInfo, nil
}