Blame internal/osbuild1/assembler.go

Packit Service 15f37d
package osbuild1
Packit Service 15f37d
Packit Service 15f37d
import (
Packit Service 15f37d
	"encoding/json"
Packit Service 15f37d
	"errors"
Packit Service 15f37d
)
Packit Service 15f37d
Packit Service 15f37d
// An Assembler turns a filesystem tree into a target image.
Packit Service 15f37d
type Assembler struct {
Packit Service 15f37d
	Name    string           `json:"name"`
Packit Service 15f37d
	Options AssemblerOptions `json:"options"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// AssemblerOptions specify the operations of a given assembler-type.
Packit Service 15f37d
type AssemblerOptions interface {
Packit Service 15f37d
	isAssemblerOptions()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
type rawAssembler struct {
Packit Service 15f37d
	Name    string          `json:"name"`
Packit Service 15f37d
	Options json.RawMessage `json:"options"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// UnmarshalJSON unmarshals JSON into an Assembler object. Each type of
Packit Service 15f37d
// assembler has a custom unmarshaller for its options, selected based on the
Packit Service 15f37d
// stage name.
Packit Service 15f37d
func (assembler *Assembler) UnmarshalJSON(data []byte) error {
Packit Service 15f37d
	var rawAssembler rawAssembler
Packit Service 15f37d
	err := json.Unmarshal(data, &rawAssembler)
Packit Service 15f37d
	if err != nil {
Packit Service 15f37d
		return err
Packit Service 15f37d
	}
Packit Service 15f37d
	var options AssemblerOptions
Packit Service 15f37d
	switch rawAssembler.Name {
Packit Service 15f37d
	case "org.osbuild.ostree.commit":
Packit Service 15f37d
		options = new(OSTreeCommitAssemblerOptions)
Packit Service 15f37d
	case "org.osbuild.qemu":
Packit Service 15f37d
		options = new(QEMUAssemblerOptions)
Packit Service 15f37d
	case "org.osbuild.rawfs":
Packit Service 15f37d
		options = new(RawFSAssemblerOptions)
Packit Service 15f37d
	case "org.osbuild.tar":
Packit Service 15f37d
		options = new(TarAssemblerOptions)
Packit Service 15f37d
	default:
Packit Service 15f37d
		return errors.New("unexpected assembler name")
Packit Service 15f37d
	}
Packit Service 15f37d
	err = json.Unmarshal(rawAssembler.Options, options)
Packit Service 15f37d
	if err != nil {
Packit Service 15f37d
		return err
Packit Service 15f37d
	}
Packit Service 15f37d
Packit Service 15f37d
	assembler.Name = rawAssembler.Name
Packit Service 15f37d
	assembler.Options = options
Packit Service 15f37d
Packit Service 15f37d
	return nil
Packit Service 15f37d
}