Skip to content

Commit

Permalink
Merge pull request #101 from opentofu/append_module_versions
Browse files Browse the repository at this point in the history
Append module versions
  • Loading branch information
cam72cam authored Dec 12, 2023
2 parents b6ae978 + cf87a18 commit 3fd0154
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/internal/module/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,31 @@ func (m Module) BuildMetadata() (*Metadata, error) {
return nil, err
}

versions := make([]Version, len(tags))
for i, t := range tags {
versions[i] = Version{Version: t}
meta, err := m.ReadMetadata()
if err != nil {
return nil, err
}

// Merge current versions with new versions
for _, t := range tags {
found := false
for _, v := range meta.Versions {
if v.Version == t {
found = true
break
}
}
if !found {
meta.Versions = append(meta.Versions, Version{Version: t})
}
}

semverSortFunc := func(a, b Version) int {
return -semver.Compare(fmt.Sprintf("v%s", internal.TrimTagPrefix(a.Version)), fmt.Sprintf("v%s", internal.TrimTagPrefix(b.Version)))
}
slices.SortFunc(meta.Versions, semverSortFunc)

return &Metadata{Versions: versions}, nil
return &meta, nil
}

func (m Module) getSemverTags() ([]string, error) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/v1api/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m ModuleGenerator) VersionDownloadPath(v module.Version) string {
func (m ModuleGenerator) VersionListing() ModuleVersionListingResponse {
versions := make([]ModuleVersionResponseItem, len(m.Metadata.Versions))
for i, v := range m.Metadata.Versions {
versions[i] = ModuleVersionResponseItem{Version: v.Version}
versions[i] = ModuleVersionResponseItem{Version: internal.TrimTagPrefix(v.Version)}
}
return ModuleVersionListingResponse{[]ModuleVersionListingResponseItem{{versions}}}
}
Expand Down

0 comments on commit 3fd0154

Please sign in to comment.