-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_update.go
91 lines (82 loc) · 2.07 KB
/
module_update.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
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
package libregistry
import (
"context"
"errors"
"github.com/opentofu/libregistry/metadata"
"github.com/opentofu/libregistry/types/module"
"github.com/opentofu/libregistry/vcs"
)
func (m api) UpdateModule(ctx context.Context, moduleAddr module.Addr) error {
if err := moduleAddr.Validate(); err != nil {
return &ModuleUpdateFailedError{
moduleAddr,
err,
}
}
moduleMetadata, err := m.dataAPI.GetModule(ctx, moduleAddr)
if err != nil {
var notFoundError *metadata.ModuleNotFoundError
if !errors.As(err, ¬FoundError) {
return &ModuleUpdateFailedError{
moduleAddr,
err,
}
}
moduleMetadata = module.Metadata{}
}
previousSize := len(moduleMetadata.Versions)
tags, err := m.vcsClient.ListLatestTags(ctx, getModuleRepo(moduleAddr))
if err != nil {
return &ModuleUpdateFailedError{
moduleAddr,
err,
}
}
var newVersions module.VersionList
for _, tag := range tags {
ver, err := module.VersionFromVCS(tag.VersionNumber)
if err != nil {
continue
}
newVersions = append(newVersions, module.Version{
Version: ver,
})
}
moduleMetadata.Versions = moduleMetadata.Versions.Merge(newVersions)
if len(moduleMetadata.Versions) == previousSize+len(tags) {
// No overlap found, do the full query:
tags, err = m.vcsClient.ListAllTags(ctx, getModuleRepo(moduleAddr))
if err != nil {
return &ModuleUpdateFailedError{
moduleAddr,
err,
}
}
newVersions = nil
for _, tag := range tags {
ver, err := module.VersionFromVCS(tag.VersionNumber)
if err != nil {
continue
}
newVersions = append(newVersions, module.Version{
Version: ver,
})
}
moduleMetadata.Versions = newVersions
}
if err := m.dataAPI.PutModule(ctx, moduleAddr, moduleMetadata); err != nil {
return &ModuleAddFailedError{
moduleAddr,
err,
}
}
return nil
}
func getModuleRepo(module module.Addr) vcs.RepositoryAddr {
return vcs.RepositoryAddr{
Org: vcs.OrganizationAddr(module.Namespace),
Name: "terraform-" + module.TargetSystem + "-" + module.Name,
}
}