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

Feature request: option to dry-run #154

Closed
yermulnik opened this issue Mar 31, 2021 · 1 comment · Fixed by #416
Closed

Feature request: option to dry-run #154

yermulnik opened this issue Mar 31, 2021 · 1 comment · Fixed by #416
Labels
new feature New feature or request
Milestone

Comments

@yermulnik
Copy link
Collaborator

yermulnik commented Mar 31, 2021

I'd like to ask to implement a feature which would allow me to run tfswitch in a dry-run mode where it only outputs whether it would switch terraform version in current directory or not with different exit codes for the two operations (e.g. 0 for when no switching is required and 1 for when tfswitch would switch versions).

My use case: we're managing different infrastructures from different repositories including legacy setups – most of these are using different versions of terraform and we're not planning to unify them. Most of the time I need to switch terraform version when I work on something in several different setups at the same time but it's not always that I need to actually switch terraform version (e.g. when I only need to cd into dir to check something) hence suggested cdtfswitch() Bash alias is not something I indeed need but would rather want to inject something like tfswitch --dry-run >/dev/null || echo "tfswitch to use correct terraform version" into my PROMPT_COMMAND env var so that I get notified when my Terraform version doesn't match Terraform version constraint every time I hit enter on the command line.

Thanks in advance.

@warrensbox warrensbox self-assigned this Apr 14, 2021
@warrensbox warrensbox added new feature New feature or request question Further information is requested labels Apr 14, 2021
@yermulnik
Copy link
Collaborator Author

As an interim solution (sort of FWIW) here's the ugly bit of Go code I came up with:

package main

import (
	"fmt"
	"os"

	"github.com/hashicorp/go-version"
	"github.com/jessevdk/go-flags"
)

var opts struct {
	Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
}

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: %s [-v] <tf version> <tf version constraint>\n", os.Args[0])
	os.Exit(2)
}

func main() {
	args, err := flags.Parse(&opts)
	if err != nil {
		usage()
	}

	if len(args) != 2 {
		usage()
	}

	v1, err := version.NewVersion(args[0])
	if err != nil {
		fmt.Println("ERROR:", err)
		usage()
	}

	constraints, err := version.NewConstraint(args[1])
	if err != nil {
		fmt.Println("ERROR:", err)
		usage()
	}

	if constraints.Check(v1) {
		if len(opts.Verbose) > 0 {
			fmt.Printf("\"%s\" satisfies constraint \"%s\"\n", v1, constraints)
		}
		os.Exit(0)
	} else {
		if len(opts.Verbose) > 0 {
			fmt.Printf("\"%s\" DOES NOT satisfy constraint \"%s\"\n", v1, constraints)
		}
		os.Exit(1)
	}
}
> terraform version | head -1
Terraform v0.13.6

> fgrep required_version *.tf
  required_version = "~> 1.0.0"

> tf_version_check -v "$(terraform version -json | jq -r .terraform_version)" "$(awk '/^[[:space:]]+required_version[[:space:]]+/ {print gensub("^.+= \"(.+)\"^M?$","\\1","g");}' *.tf 2>/dev/null)"
"0.13.6" DOES NOT satisfy constraint "~> 1.0.0"

> tf_version_check "$(terraform version -json | jq -r .terraform_version)" "$(awk '/^[[:space:]]+required_version[[:space:]]+/ {print gensub("^.+= \"(.+)\"^M?$","\\1","g");}' *.tf 2>/dev/null)" && echo GOOD || echo BAD
BAD

> tfswitch
Reading configuration from home directory for .tfswitch.toml
Reading required version from terraform file
Reading required version from constraint: ~> 1.0.0
Matched version: 1.0.11
Switched terraform to version "1.0.11"

> tf_version_check -v "$(terraform version -json | jq -r .terraform_version)" "$(awk '/^[[:space:]]+required_version[[:space:]]+/ {print gensub("^.+= \"(.+)\"^M?$","\\1","g");}' *.tf 2>/dev/null)"
"1.0.11" satisfies constraint "~> 1.0.0"

> tf_version_check "$(terraform version -json | jq -r .terraform_version)" "$(awk '/^[[:space:]]+required_version[[:space:]]+/ {print gensub("^.+= \"(.+)\"^M?$","\\1","g");}' *.tf 2>/dev/null)" && echo GOOD || echo BAD
GOOD

@yermulnik yermulnik removed the question Further information is requested label Apr 24, 2024
MatrixCrawler added a commit that referenced this issue Apr 26, 2024
@MatrixCrawler MatrixCrawler linked a pull request Apr 26, 2024 that will close this issue
@MatrixCrawler MatrixCrawler added this to the Release 1.2.0 milestone Apr 26, 2024
MatrixCrawler added a commit that referenced this issue Apr 26, 2024
* implement dry-run feature from #154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants