-
Notifications
You must be signed in to change notification settings - Fork 137
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
Labels
new feature
New feature or request
Milestone
Comments
warrensbox
added
new feature
New feature or request
question
Further information is requested
labels
Apr 14, 2021
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 |
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
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 and1
for whentfswitch
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 liketfswitch --dry-run >/dev/null || echo "tfswitch to use correct terraform version"
into myPROMPT_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.
The text was updated successfully, but these errors were encountered: