Yleisradio / chtf

Terraform version switcher with automatic install
MIT License
21 stars 4 forks source link

Feature request: Support `latest:<regex>` syntax #14

Open smiller171 opened 2 years ago

smiller171 commented 2 years ago

tfenv supports strings like latest:^1. in order to grab the most recent version that matches a regex constraint. This is useful for keeping within a particular major or minor version, while continuing to get patch and security updates without needing to manually change your .terraform-version every time.

I'd love to see chtf support this syntax as well, if only to maintain compatibility with existing .terraform-version files

tmatilai commented 2 years ago

Thanks for the suggestion!

In the spirit of the chruby, the idea has been to keep the helper script as simple and lightweight. So far even the .terraform-version support has been left out and only an example implementation is given the README.

The biggest challenge of implementing "native" support for .terraform-version or even command line wildcards etc., is that how to specify the latest matching version. Currently chtf only knows about already installed versions, but as a user I would probably like to use the latest matching released version instead, and auto-install it if needed. And this would require querying the available versions. And the two installation methods would require different solutions.

Support for selecting only local versions is simpler, but I'm not sure if that would be enough?

All that said, I use myself a Fish config to use the latest installed version by default if the specified version is latest:

if test -f $HOME/.terraform-version
    and type -q chtf
    set -l v (cat $HOME/.terraform-version)
    if test $v = "latest"
        set v (chtf | tr -d ' *' | sort -V | tail -1)
    end
    chtf $v
end

It should be fairly easy to implement similar Bash/ZSH version should, or to add more logic for filtering the matching version.

What do you think? How would you like the whole thing to work? 🙂

As always, I seem to be a tight on free hacking time, but I'm happy to help and improve the functionality and docs up to certain point. And of course I'm open for PRs for those remote version helpers, maybe as separate command(s).

smiller171 commented 2 years ago

I hadn't looked closely enough to realize that the entire logic for parsing the .terraform-version file was in that little example script, with nothing built into the tool itself.

I think even just looking at locally installed versions would be helpful. Maybe I'll find the time to hack away at something myself. If I do I'll open a PR against the Readme.