forked from docker/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
104 lines (90 loc) · 2.78 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
package metadata
import (
"strings"
)
// Maintainer represents one of the apps's maintainers
type Maintainer struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
}
// Maintainers is a list of maintainers
type Maintainers []Maintainer
// String gives a string representation of a list of maintainers
func (ms Maintainers) String() string {
res := make([]string, len(ms))
for i, m := range ms {
res[i] = m.String()
}
return strings.Join(res, ", ")
}
// String gives a string representation of a maintainer
func (m Maintainer) String() string {
s := m.Name
if m.Email != "" {
s += " <" + m.Email + ">"
}
return s
}
// AppMetadata is the format of the data found inside the metadata.yml file
type AppMetadata struct {
Version string `json:"version"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Namespace string `json:"namespace,omitempty"`
Maintainers Maintainers `json:"maintainers,omitempty"`
Parents Parents `yaml:",omitempty" json:"parents,omitempty"`
}
// Parents is a list of ParentMetadata items
type Parents []ParentMetadata
// ParentMetadata contains historical data of forked packages
type ParentMetadata struct {
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Version string `json:"version"`
Maintainers Maintainers `json:"maintainers,omitempty"`
}
// Modifier is a function signature that takes and returns an AppMetadata object
type Modifier func(AppMetadata) AppMetadata
// From returns an AppMetadata instance based on the provided AppMetadata
// and applicable modifier functions
func From(orig AppMetadata, modifiers ...Modifier) AppMetadata {
parent := ParentMetadata{
Name: orig.Name,
Namespace: orig.Namespace,
Version: orig.Version,
Maintainers: orig.Maintainers,
}
result := AppMetadata{
Version: orig.Version,
Name: orig.Name,
Namespace: orig.Namespace,
Description: orig.Description,
Maintainers: orig.Maintainers,
Parents: append(orig.Parents, parent),
}
for _, f := range modifiers {
result = f(result)
}
return result
}
// WithMaintainers returns a modified AppMetadata with updated maintainers field
func WithMaintainers(maintainers Maintainers) Modifier {
return func(parent AppMetadata) AppMetadata {
parent.Maintainers = maintainers
return parent
}
}
// WithName returns a modified AppMetadata with updated name field
func WithName(name string) Modifier {
return func(parent AppMetadata) AppMetadata {
parent.Name = name
return parent
}
}
// WithNamespace returns a modified AppMetadata with updated namespace field
func WithNamespace(namespace string) Modifier {
return func(parent AppMetadata) AppMetadata {
parent.Namespace = namespace
return parent
}
}