GetPathForResource tries to load a file or URL from the given path. If the path starts with "http://" or "https://", it will try to load the file from the URL and save it in a temporary file. It will return the path to the temporary file. If the path is an actual file path, it will return the filepa
(resourcePath string)
| 39 | // in a temporary file. It will return the path to the temporary file. |
| 40 | // If the path is an actual file path, it will return the filepath |
| 41 | func GetPathForResource(resourcePath string) (string, error) { |
| 42 | if _, err := os.Stat(resourcePath); err == nil { |
| 43 | return resourcePath, nil |
| 44 | } |
| 45 | |
| 46 | // Try to load the resource from a URL |
| 47 | raw, err := loadResourceFromURLOrEnv(resourcePath) |
| 48 | if err != nil { |
| 49 | return "", fmt.Errorf("loading resource: %w", err) |
| 50 | } |
| 51 | |
| 52 | // If the resource is loaded from a URL, save it in a temporary file |
| 53 | return createTempFile(resourcePath, raw) |
| 54 | } |
| 55 | |
| 56 | func loadResourceFromURLOrEnv(resourcePath string) ([]byte, error) { |
| 57 | parts := strings.SplitAfterN(resourcePath, "://", 2) |
no test coverage detected