| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"regexp" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"cuelabs.dev/go/oci/ociregistry" |
| 24 |
"cuelabs.dev/go/oci/ociregistry/ociclient" |
| 25 |
"cuelabs.dev/go/oci/ociregistry/ocidebug" |
| 26 |
"cuelabs.dev/go/oci/ociregistry/ocifilter" |
| 27 |
"cuelabs.dev/go/oci/ociregistry/ocimem" |
| 28 |
"cuelabs.dev/go/oci/ociregistry/ociunify" |
| 29 |
) |
| 30 |
|
| 31 |
var kindToRegistryType = make(map[string]reflect.Type) |
| 32 |
|
| 33 |
func init() { |
| 34 |
for _, r := range []registry{ |
| 35 |
clientRegistry{}, |
| 36 |
selectRegistry{}, |
| 37 |
readOnlyRegistry{}, |
| 38 |
immutableRegistry{}, |
| 39 |
unifyRegistry{}, |
| 40 |
memRegistry{}, |
| 41 |
debugRegistry{}, |
| 42 |
} { |
| 43 |
t := reflect.TypeOf(r) |
| 44 |
name, ok := strings.CutSuffix(t.Name(), "Registry") |
| 45 |
if !ok { |
| 46 |
panic(fmt.Errorf("type %v has malformed name", name)) |
| 47 |
} |
| 48 |
kindToRegistryType[name] = t |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
type registry interface { |
| 53 |
new() (ociregistry.Interface, error) |
| 54 |
} |
| 55 |
|
| 56 |
type clientRegistry struct { |
| 57 |
Host string `json:"host"` |
| 58 |
Insecure bool `json:"insecure"` |
| 59 |
DebugID string `json:"debugID,omitempty"` |
| 60 |
} |
| 61 |
|
| 62 |
func (r clientRegistry) new() (ociregistry.Interface, error) { |
| 63 |
return ociclient.New(r.Host, &ociclient.Options{ |
| 64 |
DebugID: r.DebugID, |
| 65 |
Insecure: r.Insecure, |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
type selectRegistry struct { |
| 70 |
Registry registry `json:"registry"` |
| 71 |
Include *regexp.Regexp `json:"include,omitempty"` |
| 72 |
Exclude *regexp.Regexp `json:"exclude,omitempty"` |
| 73 |
} |
| 74 |
|
| 75 |
func (r selectRegistry) new() (ociregistry.Interface, error) { |
| 76 |
r1, err := r.Registry.new() |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return ocifilter.Select(r1, func(repo string) bool { |
| 81 |
if !r.Include.MatchString(repo) { |
| 82 |
return false |
| 83 |
} |
| 84 |
if r.Exclude.MatchString(repo) { |
| 85 |
return false |
| 86 |
} |
| 87 |
return true |
| 88 |
}), nil |
| 89 |
} |
| 90 |
|
| 91 |
type readOnlyRegistry struct { |
| 92 |
Registry registry `json:"registry"` |
| 93 |
} |
| 94 |
|
| 95 |
func (r readOnlyRegistry) new() (ociregistry.Interface, error) { |
| 96 |
r1, err := r.Registry.new() |
| 97 |
if err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
return ocifilter.ReadOnly(r1), nil |
| 101 |
} |
| 102 |
|
| 103 |
type immutableRegistry struct { |
| 104 |
Registry registry `json:"registry"` |
| 105 |
} |
| 106 |
|
| 107 |
func (r immutableRegistry) new() (ociregistry.Interface, error) { |
| 108 |
r1, err := r.Registry.new() |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
return ocifilter.Immutable(r1), nil |
| 113 |
} |
| 114 |
|
| 115 |
type unifyRegistry struct { |
| 116 |
Registries []registry `json:"registries"` |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
func (r unifyRegistry) new() (ociregistry.Interface, error) { |
| 121 |
if len(r.Registries) != 2 { |
| 122 |
return nil, fmt.Errorf("can currently unify exactly two registries only") |
| 123 |
} |
| 124 |
r1 := make([]ociregistry.Interface, len(r.Registries)) |
| 125 |
for i := range r.Registries { |
| 126 |
ri, err := r.Registries[i].new() |
| 127 |
if err != nil { |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
r1[i] = ri |
| 131 |
} |
| 132 |
return ociunify.New(r1[0], r1[1], nil), nil |
| 133 |
} |
| 134 |
|
| 135 |
type memRegistry struct{} |
| 136 |
|
| 137 |
func (r memRegistry) new() (ociregistry.Interface, error) { |
| 138 |
return ocimem.New(), nil |
| 139 |
} |
| 140 |
|
| 141 |
type debugRegistry struct { |
| 142 |
Registry registry `json:"registry"` |
| 143 |
} |
| 144 |
|
| 145 |
func (r debugRegistry) new() (ociregistry.Interface, error) { |
| 146 |
r1, err := r.Registry.new() |
| 147 |
if err != nil { |
| 148 |
return nil, err |
| 149 |
} |
| 150 |
return ocidebug.New(r1, nil), nil |
| 151 |
} |