/
repository.go
111 lines (93 loc) · 2.39 KB
/
repository.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
package model
import (
"encoding/json"
"time"
api "github.com/flightctl/flightctl/api/v1alpha1"
"github.com/flightctl/flightctl/internal/util"
)
var (
RepositoryAPI = "v1alpha1"
RepositoryKind = "Repository"
RepositoryListKind = "RepositoryList"
)
type Repository struct {
Resource
// The desired state, stored as opaque JSON object.
Spec *JSONField[api.RepositorySpec]
// The last reported state, stored as opaque JSON object.
Status *JSONField[api.RepositoryStatus]
}
type RepositoryList []Repository
func (d Repository) String() string {
val, _ := json.Marshal(d)
return string(val)
}
func NewRepositoryFromApiResource(resource *api.Repository) *Repository {
if resource == nil || resource.Metadata.Name == nil {
return &Repository{}
}
var status api.RepositoryStatus
if resource.Status != nil {
status = *resource.Status
}
return &Repository{
Resource: Resource{
Name: *resource.Metadata.Name,
Labels: util.LabelMapToArray(resource.Metadata.Labels),
},
Spec: MakeJSONField(resource.Spec),
Status: MakeJSONField(status),
}
}
func (f *Repository) ToApiResource() api.Repository {
if f == nil {
return api.Repository{}
}
var spec api.RepositorySpec
if f.Spec != nil {
spec = f.Spec.Data
}
var status api.RepositoryStatus
if f.Status != nil {
status = f.Status.Data
}
if spec.Password != nil {
spec.Password = util.StrToPtr("*****")
}
metadataLabels := util.LabelArrayToMap(f.Resource.Labels)
return api.Repository{
ApiVersion: RepositoryAPI,
Kind: RepositoryKind,
Metadata: api.ObjectMeta{
Name: util.StrToPtr(f.Name),
CreationTimestamp: util.StrToPtr(f.CreatedAt.UTC().Format(time.RFC3339)),
Labels: &metadataLabels,
},
Spec: spec,
Status: &status,
}
}
func (dl RepositoryList) ToApiResource(cont *string, numRemaining *int64) api.RepositoryList {
if dl == nil {
return api.RepositoryList{
ApiVersion: RepositoryAPI,
Kind: RepositoryListKind,
Items: []api.Repository{},
}
}
repositoryList := make([]api.Repository, len(dl))
for i, repository := range dl {
repositoryList[i] = repository.ToApiResource()
}
ret := api.RepositoryList{
ApiVersion: RepositoryAPI,
Kind: RepositoryListKind,
Items: repositoryList,
Metadata: api.ListMeta{},
}
if cont != nil {
ret.Metadata.Continue = cont
ret.Metadata.RemainingItemCount = numRemaining
}
return ret
}