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

Check version sources based on whether verison has been set #429

Merged
merged 30 commits into from
Jun 7, 2024

Conversation

MatthewJohn
Copy link
Collaborator

Update logic for reading sources for obtaining version to read from next source explicitly if version has not been set. This handles situations where a source exists (e.g. TOML file, terraform files), but a version constraint cannot be found from the source. This avoids duplicate checks for existance of sources (such as Terragrunt file), as this was performed in GetParamters and also in the methods to read the config from the source.

Issue #420

Update logic for reading sources for obtaining version to read from next source explicitly if version has not been set.
This handles situations where a source exists (e.g. TOML file, terraform files), but a version constraint cannot be found from the source.
This avoids duplicate checks for existance of sources (such as Terragrunt file), as this was performed in GetParamters and also in the methods to read the config from the source.

Issue #420
@MatthewJohn MatthewJohn force-pushed the 420-how-to-deal-with-toml-file branch from 383fff9 to 627ef90 Compare May 9, 2024 07:00
…uations where no version is available when config files are present

Issue #420
@MatthewJohn MatthewJohn marked this pull request as draft May 9, 2024 07:03
@MatthewJohn MatthewJohn changed the title DRAFT: Check version sources based on whether verison has been set Check version sources based on whether verison has been set May 9, 2024
…form_version, as these are handled directly from the GetVersionFromX methods"

This reverts commit 90433e0.
@MatthewJohn
Copy link
Collaborator Author

I initially started looking at the, now, failing tests...
The first one I spotted was due to the TOML file failing to override the env variable for version - this is obviously testing the opposite of what was agreed in #420 (env variables have highest priority), so I'm going to leave these failing for now to get some feedback on the suggestion :)

@warrensbox
Copy link
Owner

Still reviewing...

… overriding params Version with environment variable
Previously, the highest priority was the TOML file, however, the new ordering means that terragrunt files have the highest precedence: #420
…suring each configuring overrides the previous
…y no longer contains any indicators of a valid version

Issue #420
@MatthewJohn MatthewJohn force-pushed the 420-how-to-deal-with-toml-file branch from 36ebed9 to 0ef7b9b Compare May 26, 2024 11:36
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters.go Outdated Show resolved Hide resolved
lib/param_parsing/parameters_test.go Outdated Show resolved Hide resolved
Only call GetTomlParams once and update methods to unify getting base directory

Issue #420
Update getTomlParams to use TOML directory attribute of Params
Update tests to set TOML directory to set custom location for test TOML file.

Issue #420
…ams methods.

This allowstests to inject custom default params, by calling the two child methods seperately, instead of calling GetParameters

Issue #420
@MatthewJohn MatthewJohn force-pushed the 420-how-to-deal-with-toml-file branch from 9ccdee9 to b9c3259 Compare June 3, 2024 06:45
@MatthewJohn MatthewJohn force-pushed the 420-how-to-deal-with-toml-file branch from b9c3259 to 3630353 Compare June 3, 2024 06:45
Co-authored-by: George L. Yermulnik <yz@yz.kiev.ua>
Comment on lines +71 to +76
if test -f "${TEST_PATH}/.tfswitch.toml"
then
cp "${TEST_PATH}/.tfswitch.toml" ~/
else
{ test -f ~/.tfswitch.toml && rm ~/.tfswitch.toml; } || true
fi
Copy link
Collaborator

@yermulnik yermulnik Jun 4, 2024

Choose a reason for hiding this comment

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

What this if-conditional's goal is?
If test TOML exists, then copy it to HOME, otherwise delete TOML from HOME 😲

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Exactly that - it's handled the fact that a test has a TOML file, but we ignore ChDiR (CWD) now, but I wanted to keep the test... so copied to HOMEDIR so it continued to work

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I lean to think I got it =)
You should be good to replace { test -f ~/.tfswitch.toml && rm ~/.tfswitch.toml; } || true with a simpler rm -f ~/.tfswitch.toml

Comment on lines 68 to 96
logger.Fatalf("Failed to obtain settings from TOML config in home directory: %v", err)
}
}

if tfSwitchFileExists(params) {
params, err = GetParamsFromTfSwitch(params)
} else if terraformVersionFileExists(params) {
if err != nil {
logger.Fatalf("Failed to obtain settings from \".tfswitch\" file: %v", err)
}
}

if terraformVersionFileExists(params) {
params, err = GetParamsFromTerraformVersion(params)
} else if isTerraformModule(params) {
if err != nil {
logger.Fatalf("Failed to obtain settings from \".terraform-version\" file: %v", err)
}
}

if isTerraformModule(params) {
params, err = GetVersionFromVersionsTF(params)
} else if terraGruntFileExists(params) {
params, err = GetVersionFromTerragrunt(params)
} else {
params = GetParamsFromEnvironment(params)
if err != nil {
logger.Fatalf("Failed to obtain settings from Terraform module: %v", err)
}
}
if err != nil {
logger.Fatalf("Error parsing configuration file: %q", err)

if terraGruntFileExists(params) {
params, err = GetVersionFromTerragrunt(params)
if err != nil {
logger.Fatalf("Failed to obtain settings from Terragrunt configuration: %v", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of hardcoding and wording version sources, could we probably move those from lib/param_parsing/*.go files to a more global place and re-use?

@warrensbox warrensbox merged commit fc5cd6b into master Jun 7, 2024
3 checks passed
@warrensbox warrensbox deleted the 420-how-to-deal-with-toml-file branch June 7, 2024 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants