-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetadata.go
216 lines (184 loc) · 6.81 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package alzlib
import (
"context"
"fmt"
"io/fs"
"strings"
"github.com/Azure/alzlib/internal/processor"
)
// Metadata is a struct that represents the metadata of a library member.
type Metadata struct {
name string // name of the library member
displayName string // display name of the library member
description string // description of the library member
dependencies LibraryReferences // dependencies of the library member in the form of []LibraryReference
path string // path of the library member within the ALZ Library
ref LibraryReference // reference used to instantiate the library member
}
// LibraryReferences is a slice of LibraryReference.
// This type has methods for convenience.
type LibraryReferences []LibraryReference
// FSs returns the filesystems of the library references, can be used with Alzlib.Init().
func (m LibraryReferences) FSs() []fs.FS {
fss := make([]fs.FS, len(m))
for i, l := range m {
fss[i] = l.FS()
}
return fss
}
// FetchWithDependencies recursively fetches all the library references and their dependencies.
// The destination directory a hash value that will be appended to the `.alzlib` directory in the current working directory unless overridden by the `ALZLIB_DIR` environment variable.
func (m LibraryReferences) FetchWithDependencies(ctx context.Context) (LibraryReferences, error) {
processed := make(map[string]bool)
result := make(LibraryReferences, 0, 5)
for _, lib := range m {
err := fetchLibraryWithDependencies(ctx, processed, lib, &result)
if err != nil {
return nil, err
}
}
return result, nil
}
// LibraryReference is an interface that represents a dependency of a library member.
// It can be fetched form either a custom go-getter URL or from the ALZ Library.
type LibraryReference interface {
fmt.Stringer
Fetch(ctx context.Context, desinationDirectory string) (fs.FS, error) // Fetch fetches the library member to the `.alzlib/destinationDirectory`. Override the base dir using `ALZLIB_DIR` env var.
FetchWithDependencies(ctx context.Context) (LibraryReferences, error) // FetchWithDependencies fetches the library member and its dependencies.
FS() fs.FS // FS returns the filesystem of the library member, can be used in Alzlib.Init()
}
var _ LibraryReference = (*AlzLibraryReference)(nil)
var _ LibraryReference = (*CustomLibraryReference)(nil)
// AlzLibraryReference is a struct that represents a dependency of a library member that is fetched from the ALZ Library.
type AlzLibraryReference struct {
path string
ref string
filesystem fs.FS
}
func NewAlzLibraryReference(path, ref string) *AlzLibraryReference {
return &AlzLibraryReference{
path: path,
ref: ref,
filesystem: nil,
}
}
// Fetch fetches the library member from the ALZ Library.
func (m *AlzLibraryReference) Fetch(ctx context.Context, destinationDirectory string) (fs.FS, error) {
if m.filesystem != nil {
return m.filesystem, nil
}
f, err := FetchAzureLandingZonesLibraryMember(ctx, m.path, m.ref, destinationDirectory)
if err != nil {
return nil, fmt.Errorf("AlzLibraryReference.Fetch: could not fetch library member: %w", err)
}
m.filesystem = f
return f, nil
}
// FS returns the filesystem of the library member.
func (m *AlzLibraryReference) FS() fs.FS {
return m.filesystem
}
// String returns the formatted path and the tag of the library member.
func (m *AlzLibraryReference) String() string {
return strings.Join([]string{m.path, m.ref}, "@")
}
func (m *AlzLibraryReference) Path() string {
return m.path
}
func (m *AlzLibraryReference) Ref() string {
return m.ref
}
// FetchWithDependencies fetches the library member and its dependencies.
// If you have more than one LibraryReference in a LibraryReferences slice, use LibraryReferences.FetchWithDependencies() instead.
func (m *AlzLibraryReference) FetchWithDependencies(ctx context.Context) (LibraryReferences, error) {
processed := make(map[string]bool)
result := make(LibraryReferences, 0, 5)
return result, fetchLibraryWithDependencies(ctx, processed, m, &result)
}
// CustomLibraryReference is a struct that represents a dependency of a library member that is fetched from a custom go-getter URL.
type CustomLibraryReference struct {
url string
filesystem fs.FS
}
func NewCustomLibraryReference(url string) *CustomLibraryReference {
return &CustomLibraryReference{
url: url,
filesystem: nil,
}
}
// Fetch fetches the library member from the custom go-getter URL.
func (m *CustomLibraryReference) Fetch(ctx context.Context, destinationDirectory string) (fs.FS, error) {
if m.filesystem != nil {
return m.filesystem, nil
}
f, err := FetchLibraryByGetterString(ctx, m.url, destinationDirectory)
if err != nil {
return nil, fmt.Errorf("CustomLibraryReference.Fetch: could not fetch library member: %w", err)
}
m.filesystem = f
return f, nil
}
// FS returns the filesystem of the library member.
func (m *CustomLibraryReference) FS() fs.FS {
return m.filesystem
}
// String returns the URL of the custom go-getter.
func (m *CustomLibraryReference) String() string {
return m.url
}
// FetchWithDependencies fetches the library member and its dependencies.
// If you have more than one LibraryReference in a LibraryReferences slice, use LibraryReferences.FetchWithDependencies() instead.
func (m *CustomLibraryReference) FetchWithDependencies(ctx context.Context) (LibraryReferences, error) {
processed := make(map[string]bool)
result := make(LibraryReferences, 0, 5)
return result, fetchLibraryWithDependencies(ctx, processed, m, &result)
}
func NewMetadata(in *processor.LibMetadata, ref LibraryReference) *Metadata {
dependencies := make([]LibraryReference, len(in.Dependencies))
for i, dep := range in.Dependencies {
dependencies[i] = NewMetadataDependencyFromProcessor(dep)
}
return &Metadata{
name: in.Name,
displayName: in.DisplayName,
description: in.Description,
dependencies: dependencies,
path: in.Path,
ref: ref,
}
}
func NewMetadataDependencyFromProcessor(in processor.LibMetadataDependency) LibraryReference {
if in.CustomUrl != "" {
return &CustomLibraryReference{
url: in.CustomUrl,
}
}
return &AlzLibraryReference{
path: in.Path,
ref: in.Ref,
}
}
func (m *Metadata) Name() string {
return m.name
}
func (m *Metadata) DisplayName() string {
return m.displayName
}
func (m *Metadata) Description() string {
return m.description
}
func (m *Metadata) Dependencies() LibraryReferences {
return m.dependencies
}
func (m *Metadata) Path() string {
return m.path
}
func (m *Metadata) IsAlzLibraryRef() bool {
_, ok := m.ref.(*AlzLibraryReference)
return ok
}
func (m *Metadata) Ref() LibraryReference {
return m.ref
}