Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show latest command that prints latest version that conform to contraints #536

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,27 @@ func validMinorVersionFormat(version string) bool {
return semverRegex.MatchString(version)
}

// ShowLatestVersion show install latest stable tf version
// ShowLatestVersion show latest stable tf version
func ShowLatestVersion(mirrorURL string) {
tfversion, _ := getTFLatest(mirrorURL)
fmt.Printf("%s\n", tfversion)
}

// ShowLatestRequiredVersion show latest version using constraints from TF
func ShowLatestRequiredVersion(mirrorURL string, version string) {
Comment on lines +214 to +215
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ShowLatestRequiredVersion show latest version using constraints from TF
func ShowLatestRequiredVersion(mirrorURL string, version string) {
// ShowRequiredVersion show latest version using constraints from TF
func ShowRequiredVersion(mirrorURL string, version string) {

if version == "" {
ShowLatestVersion(mirrorURL)
return
}
// Ensure version passed in valid and didn't come from an argument
tflist, _ := getTFList(mirrorURL, true)
if versionExist(version, tflist) {
fmt.Printf("%s\n", version)
return
}
logger.Fatal("The provided terraform version does not exist.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.Fatal("The provided terraform version does not exist.")
logger.Fatalf("The requested version (%q) does not exist.", version)

}

// ShowLatestImplicitVersion show latest - argument (version) must be provided
func ShowLatestImplicitVersion(requestedVersion, mirrorURL string, preRelease bool) {
if validMinorVersionFormat(requestedVersion) {
Expand Down
45 changes: 24 additions & 21 deletions lib/param_parsing/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@ import (
)

type Params struct {
Arch string
ChDirPath string
CustomBinaryPath string
DefaultVersion string
DryRun bool
HelpFlag bool
InstallPath string
LatestFlag bool
LatestPre string
LatestStable string
ListAllFlag bool
LogLevel string
MirrorURL string
ShowLatestFlag bool
ShowLatestPre string
ShowLatestStable string
Product string
ProductEntity lib.Product
TomlDir string
Version string
VersionFlag bool
Arch string
ChDirPath string
CustomBinaryPath string
DefaultVersion string
DryRun bool
HelpFlag bool
InstallPath string
LatestFlag bool
LatestPre string
LatestStable string
ListAllFlag bool
LogLevel string
MirrorURL string
ShowLatestFlag bool
ShowLatestPre string
ShowLatestRequiredFlag bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Flag suffix looks pointless to me. Not sure what its value is and not sure why it was added to a few others in past. I guess the only exception are Version and VersionFlag (where both could have a more clear names though).
Would you mind removing this suffix so this new flag is in consistency with similar ones nearby?
Thanks.

Copy link
Collaborator

@yermulnik yermulnik Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ShowLatestRequiredFlag bool
ShowRequired bool

ShowLatestStable string
Product string
ProductEntity lib.Product
TomlDir string
Version string
VersionFlag bool
}

var logger *slog.Logger
Expand Down Expand Up @@ -66,6 +67,7 @@ func populateParams(params Params) Params {
getopt.StringVarLong(&params.LogLevel, "log-level", 'g', "Set loglevel for tfswitch. One of (ERROR, INFO, NOTICE, DEBUG, TRACE)")
getopt.StringVarLong(&params.MirrorURL, "mirror", 'm', "install from a remote API other than the default. Default (based on product):\n"+strings.Join(defaultMirrors, "\n"))
getopt.BoolVarLong(&params.ShowLatestFlag, "show-latest", 'U', "Show latest stable version")
getopt.BoolVarLong(&params.ShowLatestRequiredFlag, "show-latest-required", 'R', "Show latest stable version, which complies to constraints set by Terraform/Terragrunt")
Copy link
Collaborator

@yermulnik yermulnik Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform/Terragrunt and OpenTofu and by other allowed means like TOML file =)

Suggested change
getopt.BoolVarLong(&params.ShowLatestRequiredFlag, "show-latest-required", 'R', "Show latest stable version, which complies to constraints set by Terraform/Terragrunt")
getopt.BoolVarLong(&params.ShowRequired, "show-required", 'R', "Show version, which complies to version requested by configuration")

getopt.StringVarLong(&params.ShowLatestPre, "show-latest-pre", 'P', "Show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 (latest)")
getopt.StringVarLong(&params.ShowLatestStable, "show-latest-stable", 'S', "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.13.7 (latest)")
getopt.StringVarLong(&params.Product, "product", 't', fmt.Sprintf("Specifies which product to use. Ex: `tfswitch --product opentofu` will install OpenTofu. Options: (%s). Default: %s", strings.Join(productIds, ", "), lib.DefaultProductId))
Expand Down Expand Up @@ -177,6 +179,7 @@ func initParams(params Params) Params {
params.MirrorURL = ""
params.ShowLatestFlag = false
params.ShowLatestPre = lib.DefaultLatest
params.ShowLatestRequiredFlag = false
Copy link
Collaborator

@yermulnik yermulnik Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
params.ShowLatestRequiredFlag = false
params.ShowRequired = false

params.ShowLatestStable = lib.DefaultLatest
params.TomlDir = lib.GetHomeDirectory()
params.Version = lib.DefaultLatest
Expand Down
2 changes: 1 addition & 1 deletion lib/param_parsing/versiontf.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ func isTerraformModule(params Params) bool {
if len(module.RequiredCore) == 0 {
logger.Debugf("No required version constraints defined by Terraform module at %q", params.ChDirPath)
}
return err == nil && len(module.RequiredCore) > 0
return len(module.RequiredCore) > 0
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func main() {
case parameters.ShowLatestFlag:
/* show latest stable version */
lib.ShowLatestVersion(parameters.MirrorURL)
case parameters.ShowLatestRequiredFlag:
/* show latest stable version within constraints */
lib.ShowLatestRequiredVersion(parameters.MirrorURL, parameters.Version)
Comment on lines +68 to +70
Copy link
Collaborator

@yermulnik yermulnik Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case parameters.ShowLatestRequiredFlag:
/* show latest stable version within constraints */
lib.ShowLatestRequiredVersion(parameters.MirrorURL, parameters.Version)
case parameters.ShowRequired:
/* show version requested by configuration */
lib.ShowRequiredVersion(parameters.MirrorURL, parameters.Version)

case parameters.Version != "":
err = lib.InstallProductVersion(parameters.ProductEntity, parameters.DryRun, parameters.Version, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, parameters.Arch)
case parameters.DefaultVersion != "":
Expand Down
Loading