...
1 package unpack
2
3 import (
4 "fmt"
5
6 "edge-infra.dev/pkg/f8n/warehouse/cluster"
7 "edge-infra.dev/pkg/f8n/warehouse/oci/layer"
8 )
9
10
11 type Option func(*options)
12
13 func makeOptions(opts ...Option) (*options, error) {
14 o := &options{}
15 for _, opt := range opts {
16 opt(o)
17 }
18
19 if len(o.keys) > 0 && len(o.types) > 0 {
20 return nil, fmt.Errorf("options ForLayerKeys and ForLayerTypes are mutually " +
21 "exclusive")
22 }
23
24 if o.infraNamespace != "" && !o.hasInfra() {
25 return nil, fmt.Errorf("if WithInfraNamespace is provided, unpacking the " +
26 "infrastructure layer must be enabled via ForLayerTypes or ForLayerKeys")
27 }
28
29 return o, nil
30 }
31
32 type options struct {
33 infraNamespace string
34 keys []string
35 types []layer.Type
36 provider cluster.Provider
37 render bool
38 parameters []map[string]string
39 }
40
41 func (o *options) hasInfra() bool {
42 for _, k := range o.keys {
43 if k == layer.Infra.String() {
44 return true
45 }
46 }
47 for _, t := range o.types {
48 if t == layer.Infra {
49 return true
50 }
51 }
52 return false
53 }
54
55
56
57 func WithInfraNamespace(ns string) Option {
58 return func(o *options) {
59 o.infraNamespace = ns
60 }
61 }
62
63
64
65
66 func ForProvider(p cluster.Provider) Option {
67 return func(o *options) {
68 o.provider = p
69 }
70 }
71
72
73
74
75 func RenderWith(parameters ...map[string]string) Option {
76 return func(o *options) {
77 o.render = true
78 o.parameters = parameters
79 }
80 }
81
82
83 func ForLayerTypes(t ...layer.Type) Option {
84 return func(o *options) {
85 o.types = t
86 }
87 }
88
89
90 func ForLayerKeys(s ...string) Option {
91 return func(o *options) {
92 o.keys = s
93 }
94 }
95
View as plain text