-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_info.go
130 lines (110 loc) · 3.31 KB
/
image_info.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
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package v1
import (
"fmt"
driver "github.com/arangodb/go-driver"
)
// ImageInfo contains an ID of an image and the ArangoDB version inside the image.
type ImageInfo struct {
Image string `json:"image"` // Human provided name of the image
ImageID string `json:"image-id,omitempty"` // Unique ID (with SHA256) of the image
ArangoDBVersion driver.Version `json:"arangodb-version,omitempty"` // ArangoDB version within the image
Enterprise bool `json:"enterprise,omitempty"` // If set, this is an enterprise image
}
func (i *ImageInfo) String() string {
if i == nil {
return "undefined"
}
e := "Community"
if i.Enterprise {
e = "Enterprise"
}
return fmt.Sprintf("ArangoDB %s %s (%s)", e, string(i.ArangoDBVersion), i.Image)
}
// ImageInfoList is a list of image infos
type ImageInfoList []ImageInfo
func (l ImageInfoList) Add(i ...ImageInfo) ImageInfoList {
return append(l, i...)
}
// GetByImage returns the info in the given list for the image with given name.
// If not found, false is returned.
func (l ImageInfoList) GetByImage(image string) (ImageInfo, bool) {
for _, x := range l {
if x.Image == image {
return x, true
}
}
return ImageInfo{}, false
}
// GetByImageID returns the info in the given list for the image with given id.
// If not found, false is returned.
func (l ImageInfoList) GetByImageID(imageID string) (ImageInfo, bool) {
for _, x := range l {
if x.ImageID == imageID {
return x, true
}
}
return ImageInfo{}, false
}
// AddOrUpdate adds the given info to the given list, if its image does not exist
// in the list. If the image does exist in the list, its entry is replaced by the given info.
// If not found, false is returned.
func (l *ImageInfoList) AddOrUpdate(info ImageInfo) {
// Look for existing entry
for i, x := range *l {
if x.Image == info.Image {
(*l)[i] = info
return
}
}
// No existing entry found, add it
*l = append(*l, info)
}
// Equal compares to ImageInfo
func (i *ImageInfo) Equal(other *ImageInfo) bool {
if i == nil && other == nil {
return true
} else if i == nil || other == nil {
return false
} else if i == other {
return true
}
return i.ArangoDBVersion == other.ArangoDBVersion &&
i.Enterprise == other.Enterprise &&
i.Image == other.Image &&
i.ImageID == other.ImageID
}
// Equal compares to ImageInfoList
func (l ImageInfoList) Equal(other ImageInfoList) bool {
if len(l) != len(other) {
return false
}
for i := 0; i < len(l); i++ {
ii, found := l.GetByImageID(l[i].ImageID)
if !found {
return false
}
if !l[i].Equal(&ii) {
return false
}
}
return true
}