2023-06-24 06:18:17 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-06-26 10:25:38 +00:00
|
|
|
"fmt"
|
2023-07-30 07:47:22 +00:00
|
|
|
"io"
|
2023-06-24 06:18:17 +00:00
|
|
|
"net/http"
|
2023-07-30 07:47:22 +00:00
|
|
|
"os"
|
2023-09-02 07:00:44 +00:00
|
|
|
"path/filepath"
|
2023-06-24 06:18:17 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-06-26 10:25:38 +00:00
|
|
|
const (
|
|
|
|
githubURI = "github:"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetURI(url string, f func(url string, i []byte) error) error {
|
|
|
|
if strings.HasPrefix(url, githubURI) {
|
|
|
|
parts := strings.Split(url, ":")
|
|
|
|
repoParts := strings.Split(parts[1], "@")
|
|
|
|
branch := "main"
|
|
|
|
|
|
|
|
if len(repoParts) > 1 {
|
|
|
|
branch = repoParts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := strings.Split(repoParts[0], "/")
|
|
|
|
org := repoPath[0]
|
|
|
|
project := repoPath[1]
|
|
|
|
projectPath := strings.Join(repoPath[2:], "/")
|
|
|
|
|
|
|
|
url = fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s", org, project, branch, projectPath)
|
|
|
|
}
|
|
|
|
|
2023-06-24 06:18:17 +00:00
|
|
|
if strings.HasPrefix(url, "file://") {
|
|
|
|
rawURL := strings.TrimPrefix(url, "file://")
|
2023-09-02 07:00:44 +00:00
|
|
|
// checks if the file is symbolic, and resolve if so - otherwise, this function returns the path unmodified.
|
|
|
|
resolvedFile, err := filepath.EvalSymlinks(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-24 06:18:17 +00:00
|
|
|
// Read the response body
|
2023-09-02 07:00:44 +00:00
|
|
|
body, err := os.ReadFile(resolvedFile)
|
2023-06-24 06:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a struct
|
2023-06-26 10:25:38 +00:00
|
|
|
return f(url, body)
|
2023-06-24 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send a GET request to the URL
|
|
|
|
response, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// Read the response body
|
2023-07-30 07:47:22 +00:00
|
|
|
body, err := io.ReadAll(response.Body)
|
2023-06-24 06:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a struct
|
2023-06-26 10:25:38 +00:00
|
|
|
return f(url, body)
|
2023-06-24 06:18:17 +00:00
|
|
|
}
|