-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate.go
More file actions
408 lines (346 loc) · 10.2 KB
/
Copy pathupdate.go
File metadata and controls
408 lines (346 loc) · 10.2 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package cmd
import (
"context"
"encoding/json"
"fmt"
"log"
"maps"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v62/github"
"github.com/spf13/cobra"
)
var (
owner string
repo string
versionsPath string
templatesPath string
minVersionStr string
)
type Versions struct {
Releases map[semver.Version]Release `json:"releases"`
Aliases map[Alias]semver.Version `json:"aliases"`
}
type Release struct {
Hash string `json:"hash"`
VendorHash string `json:"vendorHash"`
}
type Alias struct {
semver.Version
}
func (a Alias) MarshalText() ([]byte, error) {
return fmt.Appendf(nil, "%d.%d", a.Major(), a.Minor()), nil
}
func (a Alias) GreaterThan(b *Alias) bool {
if b == nil {
return true
}
return a.Version.GreaterThan(&b.Version)
}
func (a Alias) String() string {
return fmt.Sprintf("%d.%d", a.Major(), a.Minor())
}
type Changes struct {
newVersions []*semver.Version
aliases []Alias
}
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update versions file",
Long: "Look up the most recent Terraform releases and calculate the needed hashes for new versions",
RunE: func(cmd *cobra.Command, args []string) error {
nixPath, err := exec.LookPath("nix")
if err != nil {
return fmt.Errorf("unable to find 'nix' executable: %w", err)
}
token := os.Getenv("CLI_GITHUB_TOKEN")
if token == "" {
log.Println("Warning: CLI_GITHUB_TOKEN is not set. Requests to GitHub API may be rate limited.")
}
versionsPath, err := filepath.Abs(versionsPath)
if err != nil {
return fmt.Errorf("unable to find versions.json file: %w", err)
}
templatesPath, err := filepath.Abs(templatesPath)
if err != nil {
return fmt.Errorf("unable to find templates directory: %w", err)
}
templatesInfo, err := os.Stat(templatesPath)
if err != nil {
return fmt.Errorf("path does not exist or cannot be accessed: %w", err)
}
if !templatesInfo.IsDir() {
return fmt.Errorf("path exists but is not a directory: %s", templatesPath)
}
minVersion, err := semver.NewVersion(minVersionStr)
if err != nil {
return fmt.Errorf("invalid min-version: %w", err)
}
changes, err := updateVersions(
nixPath,
token,
versionsPath,
minVersion,
owner,
repo,
)
if err != nil {
return fmt.Errorf("unable to update versions: %w", err)
}
var messages []string
if len(changes.newVersions) > 0 {
var formattedVersions []string
for _, newVersion := range changes.newVersions {
formattedVersions = append(formattedVersions, newVersion.String())
}
versions := strings.Join(formattedVersions, ", ")
messages = append(messages, fmt.Sprintf("Add Terraform version(s) %s", versions))
}
if templatesPath != "" {
latestAlias, err := getLatestAlias(changes.aliases)
if err != nil {
return err
}
err = updateTemplatesVersions(templatesPath, latestAlias)
if err != nil {
return err
}
messages = append(messages, fmt.Sprintf("Update template to use version %s", latestAlias))
}
if len(messages) > 0 {
fmt.Printf("feat: %s\n", strings.Join(messages, " / "))
}
return nil
},
}
func updateVersions(
nixPath string,
token string,
versionsPath string,
minVersion *semver.Version,
owner string,
repo string,
) (*Changes, error) {
versions, err := readVersions(versionsPath)
if err != nil {
return nil, fmt.Errorf("unable to read versions: %w", err)
}
var newVersions []*semver.Version
err = withRepoReleases(token, owner, repo, minVersion, func(version *semver.Version, release *github.RepositoryRelease) error {
if release, ok := versions.Releases[*version]; ok {
if release.VendorHash != "" {
return nil
}
log.Printf("versionHash for %s release is empty\n", version)
}
log.Printf("Computing hash for %s release\n", version)
hash, err := computeHash(nixPath, release.GetTagName(), owner, repo)
if err != nil {
return fmt.Errorf("unable to compute hash: %w", err)
}
log.Printf("Computed hash for %s release: %s\n", version, hash)
versions.Releases[*version] = Release{Hash: hash, VendorHash: ""}
newVersions = append(newVersions, version)
return nil
})
if err != nil {
return nil, err
}
if len(newVersions) > 0 {
log.Println("Writing versions.json with new versions to compute vendor hashes")
content, err := json.MarshalIndent(versions, "", " ")
if err != nil {
return nil, fmt.Errorf("unable to marshal versions: %w", err)
}
if err := os.WriteFile(versionsPath, content, 0644); err != nil {
return nil, fmt.Errorf("unable to write file: %w", err)
}
for _, version := range newVersions {
log.Printf("Computing vendorHash for %s release\n", version)
vendorHash, err := computeVendorHash(nixPath, version)
if err != nil {
return nil, fmt.Errorf("unable to compute vendor hash for %s: %w", version, err)
}
log.Printf("Computed vendorHash for %s release: %s\n", version, vendorHash)
release := versions.Releases[*version]
release.VendorHash = vendorHash
versions.Releases[*version] = release
}
}
// Always update aliases and write the file, even if no new versions,
// to ensure aliases are correct.
versions.Aliases = make(map[Alias]semver.Version)
for version := range versions.Releases {
alias := Alias{*semver.New(version.Major(), version.Minor(), 0, "", "")}
if latest, ok := versions.Aliases[alias]; !ok || version.GreaterThan(&latest) {
versions.Aliases[alias] = version
}
}
log.Println("Writing final versions.json")
content, err := json.MarshalIndent(versions, "", " ")
if err != nil {
return nil, fmt.Errorf("unable to marshal versions: %w", err)
}
if err := os.WriteFile(versionsPath, content, 0644); err != nil {
return nil, fmt.Errorf("unable to write file: %w", err)
}
return &Changes{
newVersions: newVersions,
aliases: slices.Collect(maps.Keys(versions.Aliases)),
}, nil
}
func getLatestAlias(aliases []Alias) (*Alias, error) {
var latestAlias *Alias
for _, alias := range aliases {
if alias.GreaterThan(latestAlias) {
latestAlias = &alias
}
}
if latestAlias == nil {
return nil, fmt.Errorf("no latest version found")
}
return latestAlias, nil
}
func updateTemplatesVersions(templatesPath string, latestAlias *Alias) error {
files, err := filepath.Glob(fmt.Sprintf("%s/*/flake.nix", templatesPath))
if err != nil {
return fmt.Errorf("unable to find flake.nix files: %w", err)
}
re := regexp.MustCompile(`"(\d+\.\d+(\.\d+)?)"`)
for _, file := range files {
content, err := os.ReadFile(file)
if err != nil {
log.Printf("Unable to read file %s\n", file)
continue
}
updatedContent := re.ReplaceAllString(
string(content),
fmt.Sprintf(`"%s"`, latestAlias),
)
if string(content) == updatedContent {
log.Printf("No changes needed for %s\n", file)
continue
}
err = os.WriteFile(file, []byte(updatedContent), 0644)
if err != nil {
return fmt.Errorf("unable to write file %s: %w", file, err)
}
log.Printf("Updated %s to version %s\n", file, latestAlias)
}
return nil
}
func readVersions(versionsPath string) (*Versions, error) {
content, err := os.ReadFile(versionsPath)
if err != nil {
return nil, err
}
var versions *Versions
if err = json.Unmarshal(content, &versions); err != nil {
return nil, err
}
return versions, nil
}
func withRepoReleases(
token string,
owner string,
repo string,
minVersion *semver.Version,
callback func(version *semver.Version, release *github.RepositoryRelease) error,
) error {
client := github.NewClient(nil)
if token != "" {
client = client.WithAuthToken(token)
}
opt := &github.ListOptions{Page: 1}
for {
releases, resp, err := client.Repositories.ListReleases(
context.Background(),
owner,
repo,
opt,
)
if err != nil {
return err
}
for _, release := range releases {
if release.GetPrerelease() {
continue
}
tagName := release.GetTagName()
version, err := semver.NewVersion(strings.TrimLeft(tagName, "v"))
if err != nil {
log.Printf("Skipping invalid tag '%s': %v\n", tagName, err)
continue
}
if version.LessThan(minVersion) {
continue
}
if err := callback(version, release); err != nil {
return err
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return nil
}
func computeHash(nixPath string, tagName string, owner string, repo string) (string, error) {
cmd := exec.Command(
nixPath, "flake", "prefetch",
"--extra-experimental-features", "nix-command flakes",
"--json", fmt.Sprintf("github:%s/%s?ref=%s", owner, repo, tagName),
)
// Redirect stderr to the standard logger
cmd.Stderr = log.Writer()
// Get the output
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("command execution failed: %w", err)
}
// Parse JSON output to get hash
var result struct {
Hash string `json:"hash"`
}
if err := json.Unmarshal(output, &result); err != nil {
return "", fmt.Errorf("failed to parse JSON output: %w", err)
}
return result.Hash, nil
}
func computeVendorHash(nixPath string, version *semver.Version) (string, error) {
cmd := exec.Command(
nixPath, "build",
"--extra-experimental-features", "nix-command flakes",
"--no-link",
fmt.Sprintf(".#\"%s-%s\"", repo, version.String()),
)
output, err := cmd.CombinedOutput()
if err == nil {
return "", fmt.Errorf("nix build succeeded unexpectedly for version %s with an empty vendor hash", version.String())
}
re := regexp.MustCompile(`got:\s+(sha256-[a-zA-Z0-9+/=]+)`)
matches := re.FindStringSubmatch(string(output))
if len(matches) < 2 {
return "", fmt.Errorf("could not find vendor hash in nix build output for version %s: %s", version.String(), string(output))
}
return matches[1], nil
}
func init() {
updateCmd.Flags().
StringVarP(&versionsPath, "versions", "", "terraform.json", "The file to be updated")
updateCmd.Flags().
StringVarP(&templatesPath, "templates-dir", "", "", "Directory containing templates to update versions")
updateCmd.Flags().
StringVarP(&minVersionStr, "min-version", "", "1.0.0", "Min release version")
updateCmd.Flags().
StringVarP(&owner, "owner", "", "hashicorp", "GitHub repository owner")
updateCmd.Flags().
StringVarP(&repo, "repo", "", "terraform", "GitHub repository name")
rootCmd.AddCommand(updateCmd)
}