Blame internal/osbuild2/curl_source.go

Packit Service 15f37d
package osbuild2
Packit Service 15f37d
Packit Service 15f37d
import (
Packit Service 15f37d
	"bytes"
Packit Service 15f37d
	"encoding/json"
Packit Service 15f37d
)
Packit Service 15f37d
Packit Service 15f37d
type CurlSource struct {
Packit Service 15f37d
	Items map[string]CurlSourceItem `json:"items"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
func (CurlSource) isSource() {}
Packit Service 15f37d
Packit Service 15f37d
// CurlSourceItem can be either a URL string or a URL paired with a secrets
Packit Service 15f37d
// provider
Packit Service 15f37d
type CurlSourceItem interface {
Packit Service 15f37d
	isCurlSourceItem()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
type URL string
Packit Service 15f37d
Packit Service 15f37d
func (URL) isCurlSourceItem() {}
Packit Service 15f37d
Packit Service 15f37d
type URLWithSecrets struct {
Packit Service 15f37d
	URL     string      `json:"url"`
Packit Service 15f37d
	Secrets *URLSecrets `json:"secrets,omitempty"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
func (URLWithSecrets) isCurlSourceItem() {}
Packit Service 15f37d
Packit Service 15f37d
type URLSecrets struct {
Packit Service 15f37d
	Name string `json:"name"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// Unmarshal method for CurlSource for handling the CurlSourceItem interface:
Packit Service 15f37d
// Tries each of the implementations until it finds the one that works.
Packit Service 15f37d
func (cs *CurlSource) UnmarshalJSON(data []byte) (err error) {
Packit Service 15f37d
	cs.Items = make(map[string]CurlSourceItem)
Packit Service 15f37d
	type csSimple struct {
Packit Service 15f37d
		Items map[string]URL `json:"items"`
Packit Service 15f37d
	}
Packit Service 15f37d
	simple := new(csSimple)
Packit Service 15f37d
	b := bytes.NewReader(data)
Packit Service 15f37d
	dec := json.NewDecoder(b)
Packit Service 15f37d
	dec.DisallowUnknownFields()
Packit Service 15f37d
	if err = dec.Decode(simple); err == nil {
Packit Service 15f37d
		for k, v := range simple.Items {
Packit Service 15f37d
			cs.Items[k] = v
Packit Service 15f37d
		}
Packit Service 15f37d
		return
Packit Service 15f37d
	}
Packit Service 15f37d
Packit Service 15f37d
	type csWithSecrets struct {
Packit Service 15f37d
		Items map[string]URLWithSecrets `json:"items"`
Packit Service 15f37d
	}
Packit Service 15f37d
	withSecrets := new(csWithSecrets)
Packit Service 15f37d
	b.Reset(data)
Packit Service 15f37d
	if err = dec.Decode(withSecrets); err == nil {
Packit Service 15f37d
		for k, v := range withSecrets.Items {
Packit Service 15f37d
			cs.Items[k] = v
Packit Service 15f37d
		}
Packit Service 15f37d
		return
Packit Service 15f37d
	}
Packit Service 15f37d
Packit Service 15f37d
	return
Packit Service 15f37d
}