squadron/internal/jsonschema/loadmap.go
Kevin Franklin Kim 4b88ffbd15
feat: bump linter
2025-10-10 11:54:31 +02:00

53 lines
886 B
Go

package jsonschema
import (
"context"
"encoding/json"
"io"
"net/http"
"os"
"strings"
"github.com/pterm/pterm"
)
// LoadMap fetches the JSON schema from a given URL
func LoadMap(ctx context.Context, url string) (map[string]any, error) {
var (
err error
body []byte
)
if strings.HasPrefix(url, "http") {
pterm.Debug.Printfln("Loading map from %s", url)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
} else {
body, err = os.ReadFile(url)
if err != nil {
return nil, err
}
}
var schema map[string]any
if err := json.Unmarshal(body, &schema); err != nil {
return nil, err
}
return schema, nil
}