MCPcopy Create free account
hub / github.com/conforma/cli / GetPolicyConfig

Function GetPolicyConfig

internal/validate/helpers.go:31–71  ·  view source on GitHub ↗

Determine policyConfig

(ctx context.Context, policyConfiguration string)

Source from the content-addressed store, hash-verified

29
30// Determine policyConfig
31func GetPolicyConfig(ctx context.Context, policyConfiguration string) (string, error) {
32 // If the input starts with a JSON object, treat it as a JSON string
33 if len(policyConfiguration) > 0 && policyConfiguration[0] == '{' {
34 log.Debugf("Treating input as JSON string (length: %d)", len(policyConfiguration))
35 return policyConfiguration, nil
36 }
37
38 // If policyConfiguration is not detected as a file and is detected as a git URL,
39 // or if policyConfiguration is an https URL try to download a config file from
40 // the provided source. If successful we read its contents and return it.
41 if source.SourceIsGit(policyConfiguration) && !source.SourceIsFile(policyConfiguration) || source.SourceIsHttp(policyConfiguration) {
42 log.Debugf("Fetching policy config from url: %s", policyConfiguration)
43
44 // Create a temporary dir to download the config. This is separate from the workDir usd
45 // later for downloading policy sources, but it doesn't matter because this dir is not
46 // used again once the config file has been read.
47 fs := utils.FS(ctx)
48 tmpDir, err := utils.CreateWorkDir(fs)
49 if err != nil {
50 return "", err
51 }
52 defer utils.CleanupWorkDir(fs, tmpDir)
53
54 // Git download and find a suitable config file
55 configFile, err := source.GoGetterDownload(ctx, tmpDir, policyConfiguration)
56 if err != nil {
57 return "", err
58 }
59 log.Debugf("Loading %s as policy configuration", configFile)
60 return ReadFile(ctx, configFile)
61 } else if source.SourceIsFile(policyConfiguration) && utils.HasJsonOrYamlExt(policyConfiguration) {
62 // If policyConfiguration is detected as a file and it has a json or yaml extension,
63 // we read its contents and return it.
64 log.Debugf("Loading %s as policy configuration", policyConfiguration)
65 return ReadFile(ctx, policyConfiguration)
66 }
67
68 // If policyConfiguration is not a file path, git url, or https url,
69 // we assume it's a string and return it as is.
70 return policyConfiguration, nil
71}
72
73// Read file from the workspace and return its contents.
74func ReadFile(ctx context.Context, fileName string) (string, error) {

Callers 1

TestGetPolicyConfigFunction · 0.85

Calls 9

SourceIsGitFunction · 0.92
SourceIsFileFunction · 0.92
SourceIsHttpFunction · 0.92
FSFunction · 0.92
CreateWorkDirFunction · 0.92
CleanupWorkDirFunction · 0.92
GoGetterDownloadFunction · 0.92
HasJsonOrYamlExtFunction · 0.92
ReadFileFunction · 0.85

Tested by 1

TestGetPolicyConfigFunction · 0.68