generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
99 lines (80 loc) · 2.7 KB
/
index.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
package index
import (
"github.com/broadinstitute/thelma/internal/thelma/charts/semver"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
"os"
"sort"
)
// Index represents a Helm repo's index.yaml
type Index interface {
// Versions returns a list of all published versions of the chart
Versions(chartName string) []string
// HasVersion returns true if the index contains the given version for the given chart
HasVersion(chartName string, version string) bool
// MostRecentVersion returns the most recent / highest semantic version of the chart in the index
// returns "" if no version of the chart has been published yet
MostRecentVersion(chartName string) string
}
// Entry is for deserializing chart entries in a Helm repo index.yaml
type Entry struct {
Version string `yaml:"version"`
}
// index implements the Index interface
type index struct {
Entries map[string][]Entry `yaml:"entries"`
}
// LoadFromFile parses an index from a file
func LoadFromFile(filePath string) (*index, error) {
indexContent, err := os.ReadFile(filePath)
if err != nil {
return nil, errors.Errorf("error reading index file %s: %v", filePath, err)
}
var _index index
if err := yaml.Unmarshal(indexContent, &_index); err != nil {
return nil, errors.Errorf("error parsing index file %s: %v", filePath, err)
}
return &_index, nil
}
// HasVersion returns true if the index contains the given version for the given chart
func (index *index) HasVersion(chartName string, version string) bool {
for _, publishedVersion := range index.Versions(chartName) {
if version == publishedVersion {
return true
}
}
return false
}
// Versions returns a list of all published versions of the chart
func (index *index) Versions(chartName string) []string {
var versions []string
if index.Entries == nil || len(index.Entries) == 0 {
log.Warn().Msgf("index is empty, can't look up chart version for %s", chartName)
return versions
}
entries, exists := index.Entries[chartName]
if !exists {
log.Debug().Msgf("index does not have an entry for chart %s", chartName)
return versions
}
for _, entry := range entries {
if !semver.IsValid(entry.Version) {
log.Warn().Msgf("index has invalid semver %q for chart %s, ignoring", entry.Version, chartName)
continue
}
versions = append(versions, entry.Version)
}
return versions
}
// MostRecentVersion returns the most recent / highest semantic version of the chart in the index
func (index *index) MostRecentVersion(chartName string) string {
versions := index.Versions(chartName)
if len(versions) == 0 {
return ""
}
sort.Slice(versions, func(i, j int) bool {
return semver.Compare(versions[i], versions[j]) < 0
})
return versions[len(versions)-1]
}