warrensbox/terraform-switcher

/usr/local/bin is hardcoded as default installation directory

Closed this issue · 3 comments

Running tfswitch on MacOS Ventura 13.3.1 results in the following message:

$ tfswitch
Creating directory for terraform binary at: /Users/xxxxxx/.terraform.versions
✔ 1.3.7
Unable to write to: /usr/local/bin/terraform
Creating bin directory at: /Users/xxxxxx/bin
Creating directory for terraform binary at: /Users/xxxxxx/bin
RUN `export PATH=$PATH:/Users/xxxxxx/bin` to append bin to $PATH
Downloading to: /Users/xxxxxx/.terraform.versions
19774151 bytes downloaded
Switched terraform to version "1.3.7"

Here is the offending line: Unable to write to: /usr/local/bin/terraform

On MacOS Ventura 13.3.1 /usr/local directory is owned by root/wheel and is not writable.

To overcome this, brew has moved its installation to /opt/homebrew and made all directories under that path owned by the current user

In order to help with these changes there is a new command brew shellenv. Here is the output of that command

$ brew shellenv
export HOMEBREW_PREFIX="/opt/homebrew";
export HOMEBREW_CELLAR="/opt/homebrew/Cellar";
export HOMEBREW_REPOSITORY="/opt/homebrew/Homebrew";
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}";
export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:";
export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}";

However I can see /usr/local/bin/terraform hardcoded in these places:

I do not see the code that checks for homebrew's alternative installation path.

Could to fix defaultBin to ${HOMEBREW_PREFIX}/bin/terraform

Thanks!

I do not see the code that checks for homebrew's alternative installation path.

Homebrew is just yet another package manager, which is not installed by default and may not be available on some installations. Hence defaulting to its custom installation path isn't correct. Whereas /usr/local/bin covers most of the *nixish OSes, while though most of the time is not writable by a regular user — and this is why tfswitch falls back to user's bin/ dir inside $HOME. Although obviously this requires user to ensure that $HOME/bin is added into the $PATH env var (if it is not like that by default on their OS).
For those who prefer another installation destination, there's a respective command line parameter and config file key — please see https://github.com/warrensbox/terraform-switcher#use-tfswitchtoml-file--for-non-admin---users-with-limited-privilege-on-their-computers

Could to fix defaultBin to ${HOMEBREW_PREFIX}/bin/terraform

Also if you prefer to use tfswitch to manage Terraform versions instead of e.g. Homebrew, then you better off do not install Terraform with Homebrew.

Please feel free to re-open this issue if you feel this is still worth a discussion. Thanks.

Homebrew is just yet another package manager, which is not installed by default and may not be available on some installations. Hence defaulting to its custom installation path isn't correct. Whereas /usr/local/bin covers most of the *nixish OSes, while though most of the time is not writable by a regular user — and this is why tfswitch falls back to user's bin/ dir inside $HOME. Although obviously this requires user to ensure that $HOME/bin is added into the $PATH env var (if it is not like that by default on their OS). For those who prefer another installation destination, there's a respective command line parameter and config file key — please see https://github.com/warrensbox/terraform-switcher#use-tfswitchtoml-file--for-non-admin---users-with-limited-privilege-on-their-computers

I understand that for majority of systems, the path works, however when installed with homebrew, it knows that it is installing it in the homebrew's environment, so it can fix the configuration in here -> https://github.com/warrensbox/homebrew-tap/blob/master/Formula/tfswitch.rb

Also if you prefer to use tfswitch to manage Terraform versions instead of e.g. Homebrew, then you better off do not install Terraform with Homebrew.

I am not installing installing standalone terraform.

tfswitch.toml approach works, but it feels like a hack.

On the other hand I understand if you want to close this issue due to limited resources.

Not sure I'm following your point, though as of look of it what you're suggesting is to add a code snippet into tfswitch sources to make it analyze its invocation path at runtime (this has nothing to do installation approach btw) and if it belongs in one of Homebrew's bin paths (they differ across different OSes and seemingly can be customized: https://docs.brew.sh/Installation#installation), then default the Terraform binary installation to a Homebrew specific installation path (which in its turn may clash with e.g. Homebrew's Terraform formulae or else). Alongside Homebrew's custom bits we then would need to add logic for installation paths of other package managers in the wild. I don't think this is good direction for a simple and fast tool. Especially given that tfswitch allows users to customize installation path.

tfswitch.toml approach works, but it feels like a hack.

What makes you feel like that? Many tools have configuration files and this is not counted in as "hackery" to customize/adjust tool's default config per user's use case.

On the other hand I understand if you want to close this issue due to limited resources.

Yep, you're right — we're limited with resources =( Though this is not the reason to close out this issue. I just don't see a background for it. You may though still try and convince me =)
We really appreciate any assistance in form of PRs. So if you're up to, please don't hesitate to create PR. Thank you.

ps: I'll re-open and close this again as I chose wrong reason for that yesterday. Apologies for that.