MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / resolveReference

Function resolveReference

pkg/policies/policies.go:1065–1100  ·  view source on GitHub ↗

resolveReference resolves a reference (which may be relative) against a base path If ref is absolute (has a scheme), it returns ref as-is If basePath is a file:// path, it uses filepath.Join for resolution If basePath is an http(s):// URL, it uses URL parsing for resolution

(ref, basePath string)

Source from the content-addressed store, hash-verified

1063// If basePath is a file:// path, it uses filepath.Join for resolution
1064// If basePath is an http(s):// URL, it uses URL parsing for resolution
1065func resolveReference(ref, basePath string) (string, error) {
1066 // Check if ref is already absolute (has a scheme)
1067 refScheme, _ := RefParts(ref)
1068 if refScheme != "" {
1069 // Already absolute, return as-is
1070 return ref, nil
1071 }
1072
1073 // Get the scheme of basePath
1074 baseScheme, baseLoc := RefParts(basePath)
1075
1076 switch baseScheme {
1077 case fileScheme:
1078 // File path resolution
1079 return filepath.Join(filepath.Dir(baseLoc), ref), nil
1080 case httpScheme, httpsScheme:
1081 // HTTP(S) URL resolution
1082 baseURL, err := url.Parse(basePath)
1083 if err != nil {
1084 return "", fmt.Errorf("invalid base URL %q: %w", basePath, err)
1085 }
1086
1087 // Parse the reference relative to the base URL
1088 resolvedURL, err := baseURL.Parse(ref)
1089 if err != nil {
1090 return "", fmt.Errorf("failed to resolve reference %q against base %q: %w", ref, basePath, err)
1091 }
1092
1093 return resolvedURL.String(), nil
1094 case "":
1095 // No scheme in basePath, treat as file path
1096 return filepath.Join(filepath.Dir(basePath), ref), nil
1097 default:
1098 return "", fmt.Errorf("unsupported base path scheme: %s", baseScheme)
1099 }
1100}
1101
1102func loadPolicyScript(spec *v1.PolicySpecV2, basePath string) ([]byte, error) {
1103 var content []byte

Callers 1

loadPolicyScriptFunction · 0.85

Calls 3

RefPartsFunction · 0.85
ParseMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected