1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/cmd/cli/kubectl-kyverno/userinfo/userinfo.go
Charles-Edouard Brétéché e8bc38f25b
refactor: introduce userinfo package in the cli (#8272)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-09-05 17:10:27 +00:00

39 lines
1.2 KiB
Go

package userinfo
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/go-git/go-billy/v5"
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"sigs.k8s.io/yaml"
)
func Load(fs billy.Filesystem, path string, resourcePath string) (*kyvernov1beta1.RequestInfo, error) {
var userInfo kyvernov1beta1.RequestInfo
if fs != nil {
filep, 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(filep)
if err != nil {
return nil, fmt.Errorf("Error: failed to read file %s: %w", filep.Name(), err)
}
if err := yaml.UnmarshalStrict(bytes, &userInfo); err != nil {
return nil, sanitizederror.NewWithError("failed to decode yaml", err)
}
} else {
bytes, err := os.ReadFile(filepath.Clean(filepath.Join(resourcePath, path)))
if err != nil {
return nil, sanitizederror.NewWithError("unable to read yaml", err)
}
if err := yaml.UnmarshalStrict(bytes, &userInfo); err != nil {
return nil, sanitizederror.NewWithError("failed to decode yaml", err)
}
}
return &userInfo, nil
}