|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(url, "file://") {
|
|
|
|
rawURL := strings.TrimPrefix(url, "file://")
|
|
|
|
// Read the response body
|
|
|
|
body, err := ioutil.ReadFile(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a struct
|
|
|
|
return f(url, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a struct
|
|
|
|
return f(url, body)
|
|
|
|
}
|