package unpack import ( "fmt" "edge-infra.dev/pkg/f8n/warehouse/cluster" "edge-infra.dev/pkg/f8n/warehouse/oci/layer" ) // Option is a functional option for unpacking pallets. type Option func(*options) func makeOptions(opts ...Option) (*options, error) { o := &options{} for _, opt := range opts { opt(o) } if len(o.keys) > 0 && len(o.types) > 0 { return nil, fmt.Errorf("options ForLayerKeys and ForLayerTypes are mutually " + "exclusive") } if o.infraNamespace != "" && !o.hasInfra() { return nil, fmt.Errorf("if WithInfraNamespace is provided, unpacking the " + "infrastructure layer must be enabled via ForLayerTypes or ForLayerKeys") } return o, nil } type options struct { infraNamespace string keys []string types []layer.Type provider cluster.Provider render bool parameters []map[string]string } func (o *options) hasInfra() bool { for _, k := range o.keys { if k == layer.Infra.String() { return true } } for _, t := range o.types { if t == layer.Infra { return true } } return false } // WithInfraNamespace sets metadata.namespace field for all infrastructure // objects. func WithInfraNamespace(ns string) Option { return func(o *options) { o.infraNamespace = ns } } // ForProvider sets the cluster provider to unpack manifests for. This option // must be provided if the artifact passed to Unpack isn't already a v1.Image or // unwrap into one. func ForProvider(p cluster.Provider) Option { return func(o *options) { o.provider = p } } // RenderWith merges the provided parameters with the already set parameters, // last-in wins for duplicated keys. If this option is provided for unpacking a // Pallet that isn't renderable, no action is taken. func RenderWith(parameters ...map[string]string) Option { return func(o *options) { o.render = true o.parameters = parameters } } // ForLayerTypes returns the layers associated with the input layer types. func ForLayerTypes(t ...layer.Type) Option { return func(o *options) { o.types = t } } // ForLayerKeys returns the layers with matching keys. See layer.Key(). func ForLayerKeys(s ...string) Option { return func(o *options) { o.keys = s } }