Skip to content

Commit

Permalink
Truncate pre-release version before comparing
Browse files Browse the repository at this point in the history
The hashicorp/go-version returns false when comparing pre-releases, for
example 1.6.0-rc1 >= 0.13. This is counter-intuitive for determining the
presence or absence of a feature, so remove the pre-release information
before comparing.
  • Loading branch information
minamijoyo committed Oct 3, 2023
1 parent a63bba9 commit 3ecce3e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tfexec/terraform_state_replace_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ func (c *terraformCLI) SupportsStateReplaceProvider(ctx context.Context) (bool,
return false, constraints, err
}

if !constraints.Check(v) {
ver, err := truncatePreReleaseVersion(v)
if err != nil {
return false, constraints, err
}

if !constraints.Check(ver) {
return false, constraints, nil
}

Expand Down
22 changes: 22 additions & 0 deletions tfexec/terraform_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"regexp"
"strings"

"github.com/hashicorp/go-version"
)
Expand All @@ -29,3 +30,24 @@ func (c *terraformCLI) Version(ctx context.Context) (*version.Version, error) {

return version, nil
}

// truncatePreReleaseVersion is a helper function that removes
// pre-release information.
// The hashicorp/go-version returns false when comparing pre-releases, for
// example 1.6.0-rc1 >= 0.13. This is counter-intuitive for determining the
// presence or absence of a feature, so remove the pre-release information
// before comparing.
func truncatePreReleaseVersion(v *version.Version) (*version.Version, error) {
if v.Prerelease() == "" {
return v, nil
}

vs, _, _ := strings.Cut(v.String(), "-")

ver, err := version.NewVersion(vs)
if err != nil {
return nil, err
}

return ver, nil
}

0 comments on commit 3ecce3e

Please sign in to comment.