-
Notifications
You must be signed in to change notification settings - Fork 785
/
version_data.go
178 lines (159 loc) · 5.49 KB
/
version_data.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
package version
import (
"fmt"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Callback a callback function for processing version information. Return true to continue processing
// or false to terminate the loop
type Callback func(kind VersionKind, name string, version *StableVersion) (bool, error)
// VersionKind represents the kind of version
type VersionKind string
const (
// KindChart represents a chart version
KindChart VersionKind = "charts"
// KindPackage represents a package version
KindPackage VersionKind = "packages"
// KindDocker represents a docker image version
KindDocker VersionKind = "docker"
)
var (
// Kinds all the version kinds
Kinds = []VersionKind{
KindChart,
KindPackage,
KindDocker,
}
// KindStrings all the kinds as strings for validating CLI arguments
KindStrings = []string{
string(KindChart),
string(KindPackage),
string(KindDocker),
}
)
// StableVersion stores the stable version information
type StableVersion struct {
Version string `json:"version,omitempty"`
GitURL string `json:"gitUrl,omitempty"`
URL string `json:"url,omitempty"`
}
// LoadStableVersion loads the stable version data from the version configuration directory returning an empty object if there is
// no specific stable version configuration available
func LoadStableVersion(wrkDir string, kind VersionKind, name string) (*StableVersion, error) {
path := filepath.Join(wrkDir, string(kind), name+".yml")
return LoadStableVersionFile(path)
}
// LoadStableVersionFile loads the stable version data from the given file name
func LoadStableVersionFile(path string) (*StableVersion, error) {
version := &StableVersion{}
exists, err := util.FileExists(path)
if err != nil {
return version, errors.Wrapf(err, "failed to check if file exists %s", path)
}
if !exists {
return version, nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return version, errors.Wrapf(err, "failed to load YAML file %s", path)
}
version, err = LoadStableVersionFromData(data)
if err != nil {
return version, errors.Wrapf(err, "failed to unmarshal YAML for file %s", path)
}
return version, err
}
// LoadStableVersionFromData loads the stable version data from the given the data
func LoadStableVersionFromData(data []byte) (*StableVersion, error) {
version := &StableVersion{}
err := yaml.Unmarshal(data, version)
if err != nil {
return version, errors.Wrapf(err, "failed to unmarshal YAML")
}
return version, err
}
// LoadStableVersionNumber loads just the stable version number for the given kind and name
func LoadStableVersionNumber(wrkDir string, kind VersionKind, name string) (string, error) {
data, err := LoadStableVersion(wrkDir, kind, name)
if err != nil {
return "", err
}
version := data.Version
if version != "" {
log.Infof("using stable version %s from %s of %s from %s\n", util.ColorInfo(version), string(kind), util.ColorInfo(name), wrkDir)
} else {
log.Warnf("could not find a stable version from %s of %s from %s\nFor background see: https://jenkins-x.io/architecture/version-stream/\n", string(kind), name, wrkDir)
log.Infof("Please lock this version down via the command: %s\n", util.ColorInfo(fmt.Sprintf("jx step create version pr -k %s -n %s\n", string(kind), name)))
}
return version, err
}
// SaveStableVersion saves the version file
func SaveStableVersion(wrkDir string, kind VersionKind, name string, stableVersion *StableVersion) error {
path := filepath.Join(wrkDir, string(kind), name+".yml")
return SaveStableVersionFile(path, stableVersion)
}
// SaveStableVersionFile saves the stabe version to the given file name
func SaveStableVersionFile(path string, stableVersion *StableVersion) error {
data, err := yaml.Marshal(stableVersion)
if err != nil {
return errors.Wrapf(err, "failed to marshal data to YAML %#v", stableVersion)
}
dir, _ := filepath.Split(path)
err = os.MkdirAll(dir, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to create directory %s", dir)
}
err = ioutil.WriteFile(path, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to write file %s", path)
}
return nil
}
// ForEachVersion processes all of the versions in the wrkDir using the given callback function.
func ForEachVersion(wrkDir string, callback Callback) error {
for _, kind := range Kinds {
err := ForEachKindVersion(wrkDir, kind, callback)
if err != nil {
return err
}
}
return nil
}
// ForEachKindVersion processes all of the versions in the wrkDir and kind using the given callback function.
func ForEachKindVersion(wrkDir string, kind VersionKind, callback Callback) error {
kindString := string(kind)
kindDir := filepath.Join(wrkDir, kindString)
glob := filepath.Join(kindDir, "*", "*.yml")
paths, err := filepath.Glob(glob)
if err != nil {
return errors.Wrapf(err, "failed to find glob: %s", glob)
}
for _, path := range paths {
versionData, err := LoadStableVersionFile(path)
if err != nil {
return errors.Wrapf(err, "failed to load VersionData for file: %s", path)
}
name, err := filepath.Rel(kindDir, path)
if err != nil {
return errors.Wrapf(err, "failed to extract base path from %s", path)
}
ext := filepath.Ext(name)
if ext != "" {
name = strings.TrimSuffix(name, ext)
}
ok, err := callback(kind, name, versionData)
if err != nil {
return errors.Wrapf(err, "failed to process kind %s name %s", kindString, name)
}
if !ok {
break
}
}
return nil
}