Skip to content

Commit

Permalink
feat: add support of .tool-versions (#303)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Sharov <[email protected]>
  • Loading branch information
kvendingoldo authored Dec 22, 2024
1 parent e63817f commit 50b181f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,7 @@ The version resolution order is :

- TOFUENV_TOFU_VERSION environment variable
- `.opentofu-version` file
- `.tool-versions` [file](https://asdf-vm.com/manage/configuration.html#tool-versions)
- `terraform_version_constraint` from `terragrunt.hcl` file
- `terraform_version_constraint` from `terragrunt.hcl.json` file
- TOFUENV_TOFU_DEFAULT_VERSION environment variable
Expand All @@ -1426,6 +1427,7 @@ The version resolution order is :
- TFENV_TERRAFORM_VERSION environment variable
- `.terraform-version` file
- `.tfswitchrc` file
- `.tool-versions` [file](https://asdf-vm.com/manage/configuration.html#tool-versions)
- `terraform_version_constraint` from `terragrunt.hcl` file
- `terraform_version_constraint` from `terragrunt.hcl.json` file
- TFENV_TERRAFORM_DEFAULT_VERSION environment variable
Expand All @@ -1447,6 +1449,7 @@ The version resolution order is :
- `.terragrunt-version` file
- `.tgswitchrc` file
- `version` from `tgswitch.toml` file
- `.tool-versions` [file](https://asdf-vm.com/manage/configuration.html#tool-versions)
- `terragrunt_version_constraint` from `terragrunt.hcl` file
- `terragrunt_version_constraint` from `terragrunt.hcl.json` file
- TG_DEFAULT_VERSION environment variable
Expand All @@ -1465,6 +1468,7 @@ The version resolution order is :

- ATMOS_VERSION environment variable
- `.atmos-version` file
- `.tool-versions` [file](https://asdf-vm.com/manage/configuration.html#tool-versions)
- ATMOS_DEFAULT_VERSION environment variable
- `${TENV_ROOT}/Atmos/version` file (can be written with `tenv atmos use`)
- `latest-allowed`
Expand Down
5 changes: 5 additions & 0 deletions versionmanager/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
terraformretriever "github.com/tofuutils/tenv/v3/versionmanager/retriever/terraform"
terragruntretriever "github.com/tofuutils/tenv/v3/versionmanager/retriever/terragrunt"
tofuretriever "github.com/tofuutils/tenv/v3/versionmanager/retriever/tofu"
asdfparser "github.com/tofuutils/tenv/v3/versionmanager/semantic/parser/asdf"
flatparser "github.com/tofuutils/tenv/v3/versionmanager/semantic/parser/flat"
iacparser "github.com/tofuutils/tenv/v3/versionmanager/semantic/parser/iac"
terragruntparser "github.com/tofuutils/tenv/v3/versionmanager/semantic/parser/terragrunt"
Expand All @@ -48,6 +49,7 @@ func BuildAtmosManager(conf *config.Config, _ *hclparse.Parser) versionmanager.V
atmosRetriever := atmosretriever.Make(conf)
versionFiles := []types.VersionFile{
{Name: ".atmos-version", Parser: flatparser.RetrieveVersion},
{Name: ".tool-versions", Parser: asdfparser.RetrieveAtmosVersion},
}

return versionmanager.Make(conf, config.AtmosPrefix, "Atmos", nil, atmosRetriever, versionFiles)
Expand All @@ -59,6 +61,7 @@ func BuildTfManager(conf *config.Config, hclParser *hclparse.Parser) versionmana
versionFiles := []types.VersionFile{
{Name: ".terraform-version", Parser: flatparser.RetrieveVersion},
{Name: ".tfswitchrc", Parser: flatparser.RetrieveVersion},
{Name: ".tool-versions", Parser: asdfparser.RetrieveTfVersion},
{Name: terragruntparser.HCLName, Parser: gruntParser.RetrieveTerraformVersionConstraintFromHCL},
{Name: terragruntparser.JSONName, Parser: gruntParser.RetrieveTerraformVersionConstraintFromJSON},
}
Expand All @@ -78,6 +81,7 @@ func BuildTgManager(conf *config.Config, hclParser *hclparse.Parser) versionmana
{Name: ".terragrunt-version", Parser: flatparser.RetrieveVersion},
{Name: ".tgswitchrc", Parser: flatparser.RetrieveVersion},
{Name: ".tgswitch.toml", Parser: tomlparser.RetrieveVersion},
{Name: ".tool-versions", Parser: asdfparser.RetrieveTgVersion},
{Name: terragruntparser.HCLName, Parser: gruntParser.RetrieveTerragruntVersionConstraintFromHCL},
{Name: terragruntparser.JSONName, Parser: gruntParser.RetrieveTerragruntVersionConstraintFromJSON},
}
Expand All @@ -90,6 +94,7 @@ func BuildTofuManager(conf *config.Config, hclParser *hclparse.Parser) versionma
gruntParser := terragruntparser.Make(hclParser)
versionFiles := []types.VersionFile{
{Name: ".opentofu-version", Parser: flatparser.RetrieveVersion},
{Name: ".tool-versions", Parser: asdfparser.RetrieveTofuVersion},
{Name: terragruntparser.HCLName, Parser: gruntParser.RetrieveTerraformVersionConstraintFromHCL},
{Name: terragruntparser.JSONName, Parser: gruntParser.RetrieveTerraformVersionConstraintFromJSON},
}
Expand Down
89 changes: 89 additions & 0 deletions versionmanager/semantic/parser/asdf/asdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package asdfparser

import (
"bufio"

"errors"
"io/fs"
"os"
"strings"

"github.com/tofuutils/tenv/v3/config"
"github.com/tofuutils/tenv/v3/pkg/loghelper"
"github.com/tofuutils/tenv/v3/versionmanager/semantic/types"
)

func NoMsg(_ loghelper.Displayer, value string, _ string) string {
return value
}

func Retrieve(filePath, toolName string, conf *config.Config, displayMsg func(loghelper.Displayer, string, string) string) (string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
conf.Displayer.Log(loghelper.LevelWarnOrDebug(errors.Is(err, fs.ErrNotExist)), "Failed to read file", loghelper.Error, err)

return "", nil
}

resolvedVersion := ""

scanner := bufio.NewScanner(strings.NewReader(string(data)))
for scanner.Scan() {
line := scanner.Text()

if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") {
continue
}

parts := strings.Fields(line)
if len(parts) >= 2 && parts[0] == toolName {
resolvedVersion = parts[1]
}
}

if err := scanner.Err(); err != nil {
conf.Displayer.Log(loghelper.LevelWarnOrDebug(errors.Is(err, fs.ErrNotExist)), "Failed to read file", loghelper.Error, err)

return "", nil
}

if resolvedVersion == "" {
return "", nil
}

return displayMsg(conf.Displayer, resolvedVersion, filePath), nil
}

func RetrieveTfVersion(filePath string, conf *config.Config) (string, error) {
return Retrieve(filePath, "terraform", conf, types.DisplayDetectionInfo)
}

func RetrieveTofuVersion(filePath string, conf *config.Config) (string, error) {
return Retrieve(filePath, "tofu", conf, types.DisplayDetectionInfo)
}

func RetrieveAtmosVersion(filePath string, conf *config.Config) (string, error) {
return Retrieve(filePath, "atmos", conf, types.DisplayDetectionInfo)
}

func RetrieveTgVersion(filePath string, conf *config.Config) (string, error) {
return Retrieve(filePath, "terragrunt", conf, types.DisplayDetectionInfo)
}

0 comments on commit 50b181f

Please sign in to comment.