Giter Site home page Giter Site logo

Comments (20)

MichaelSmyth0184 avatar MichaelSmyth0184 commented on September 26, 2024 36

I just gave my current user owner on the /usr/local/bin folder and worked like magic.

sudo chown -R $USER /usr/local/bin

Might not be great from a security perspective, but works none the less.

from terraform-switcher.

sbuckpesch avatar sbuckpesch commented on September 26, 2024 8

For me the .tfswitch.toml solution works perfectly on WSL2 without changing any permissions on /usr/local/bin

# Go to user home
cd 

# Install tfswitch
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | sudo bash
mkdir bin
export BIN_PATH=$(echo $PWD)/bin

# Create .tfswitch.toml
cat >> .tfswitch.toml << EOF
bin = "${BIN_PATH}/terraform"
EOF

# Install your first terraform version
tfswitch

# Put your new binary path to .zshrc or .bashrc
cat >> .zshrc << EOF
export PATH=${BIN_PATH}:\$PATH
EOF

# Restart your console. Done 🎉
terraform --version

from terraform-switcher.

bezerker avatar bezerker commented on September 26, 2024 6

I'd suggest having tfswitch just install the terraform binaries into the directory that you specify with -b. I keep mine in /home/$USER/bin and put that in my path. Be great if this worked with that. This is kind of broken on linux otherwise

from terraform-switcher.

mickmorseMB avatar mickmorseMB commented on September 26, 2024 6

Just create an alias in your bashrc called tfs

alias tfs="tfswitch -b /home/user/bin"

that way your custom directory alwats persists.

from terraform-switcher.

weyderfs avatar weyderfs commented on September 26, 2024 5

Guys I fixed here adding sudo after |
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | sudo bash
But to use tfswitch and Terraform I needed continuous using SUDO
sudo tfswtich
sudo terraform

But works here

Ubuntu 18.04.3 LTS \n \l

from terraform-switcher.

evansj avatar evansj commented on September 26, 2024 4

I'm also having issues on Mac because (due to company rules ☚ī¸) my main user account isn't an admin account. It would be great if I could configure it to use ~/bin/terraform instead of /usr/local/bin/terraform. Better still if it could read its config from something like ~/.tfswitch.conf so I could set the directory in there instead of passing a command line argument every time.

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024 3

@evansj @ajjl
I released a pre-released version with the option to install tfswitch to a user specifed directory.

You can download the pre-release version here (based on your OS): https://github.com/warrensbox/terraform-switcher/releases/tag/0.7.0

  1. Create bin path directory. Ex: mkdir /Users/warrenveerasingam/bin
  2. Add custom bin path to PATH env: export PATH=$PATH:/Users/warrenveerasingam/bin
  3. Test pre-release version ./tfswitch -b /Users/warrenveerasingam/bin/terraform. Install any version.
  4. Validate terraform version. terraform -v

Otherwise, you can git pull and compile from the source.

  1. To compile from the source, you must checkout the feature/unprivilege-user branch (https://github.com/warrensbox/terraform-switcher/tree/feature/unprivilege-user)
  2. dep ensure or go get -v -t -d ./...
  3. go build -v -o tfswitch
  4. Create bin path directory. Ex: mkdir /Users/warrenveerasingam/bin
  5. Add custom bin path to PATH env: export PATH=$PATH:/Users/warrenveerasingam/bin
  6. Test pre-release version ./tfswitch -b /Users/warrenveerasingam/bin/terraform. Install any version.
  7. Validate terraform version. terraform -v

I will merge feature/unprivilege-user branch to the master branch and release it once we can verify it's working as expected.

I will release an option with the ~/.tfswitch.conf soon but not for this release.

Please feel free to contribute and comment. Thanks for helping me test it out.

from terraform-switcher.

apr-1985 avatar apr-1985 commented on September 26, 2024 1

Also seeing this on Mac High Sierra when installed via homebrew
✔ 0.11.7 Downloading https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_darwin_amd64.zip to terraform_0.11.7_darwin_amd64.zip Downloading ... 17754416 bytes downloaded. 2019/02/11 10:14:25 Unable to create symlink. You must have SUDO privileges

EDIT:
Turns out this was because I already had terraform installed in /usr/local/bin which meant the symlink couldnt be created. Error message is very misleading and a check for Terraform installs should probably be added.

from terraform-switcher.

evansj avatar evansj commented on September 26, 2024 1

@warrensbox I've been using the pre-release version today and it works perfectly - thanks!

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024 1

Released : 0.7.737
https://github.com/warrensbox/terraform-switcher/releases/tag/0.7.737
Features:
Option to use -b parameter to specify custom terraform installtion location:
tfswitch -b /Users/warrenveerasingam/bin/terraform 0.10.8
Option to use .tfswitch.toml file. For example:

bin = "/Users/warrenveerasingam/bin/terraform"
version = "0.11.3"

After that, you can simply run tfswitch and it should automatically update your terraform version according to the configuration provided.

drawing

drawing

You can also automate the switching by adding the following to your profile:

Automatically switch with bash

Add the following to the end of your ~/.bashrc file: (Use either .tfswitchrc or .tfswitch.toml)

cdtfswitch(){
  builtin cd "$@";
  cdir=$PWD;
  if [ -f "$cdir/.tfswitchrc" ]; then
    tfswitch
  fi
}
alias cd='cdtfswitch'

Automatically switch with zsh

Add the following to the end of your ~/.zshrc file:

load-tfswitch() {
  local tfswitchrc_path=".tfswitch.toml"

  if [ -f "$tfswitchrc_path" ]; then
    tfswitch
  fi
}

add-zsh-hook chpwd load-tfswitch
load-tfswitch

NOTE: if you see an error like this: command not found: add-zsh-hook, then you might be on an older version of zsh (see below), or you simply need to load add-zsh-hook by adding this to your .zshrc:

autoload -U add-zsh-hook

older version of zsh

cd(){
  builtin cd "$@";
  cdir=$PWD;
  if [ -f "$cdir/.tfswitch.toml" ]; then
    tfswitch
  fi
}

To get the latest version of tfswitch:
With Brew:

brew upgrade warrensbox/tap/tfswitch

Other upgrades:

curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | bash

Please open or comment on this issue if you see any bug!

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

@ajjl I will look at this

from terraform-switcher.

warrensbox avatar warrensbox commented on September 26, 2024

I have updated the symlink error message to be more intuitive. The previous error message was misleading. Merge request #38 :

log.Fatalf(`
	Unable to create new symlink.
	Maybe symlink already exist. Try removing existing symlink manually.
	Try running "unlink %s" to remove existing symlink.
	If error persist, you may not have the permission to create a symlink at %s.
	Error: %s`, dir, dir, err)

@ajjl requested a feature to allow users without admin privilege to install tfswitch. This feature is still not supported at the moment. However, please feel free to contribute and open a merge request to add this feature.

from terraform-switcher.

evansj avatar evansj commented on September 26, 2024

@MichaelSmyth0184 that would work great. Unfortunately I can't do that, due to the aforementioned company rules...

from terraform-switcher.

MichaelSmyth0184 avatar MichaelSmyth0184 commented on September 26, 2024

@evansj Yeah I guess having sudo permissions is a luxery at some companies...

Have you tried downloading from source and modifying destination folder to something you have ownership over? Shouldn't be too difficult I would imagine.

from terraform-switcher.

evansj avatar evansj commented on September 26, 2024

@MichaelSmyth0184 I intend to do that, and hopefully create a PR

from terraform-switcher.

alok15ee avatar alok15ee commented on September 26, 2024

@evansj @ajjl
I released a pre-released version with the option to install tfswitch to a user specifed directory.

You can download the pre-release version here (based on your OS): https://github.com/warrensbox/terraform-switcher/releases/tag/0.7.0

  1. Create bin path directory. Ex: mkdir /Users/warrenveerasingam/bin
  2. Add custom bin path to PATH env: export PATH=$PATH:/Users/warrenveerasingam/bin
  3. Test pre-release version ./tfswitch -b /Users/warrenveerasingam/bin/terraform. Install any version.
  4. Validate terraform version. terraform -v

Otherwise, you can git pull and compile from the source.

  1. To compile from the source, you must checkout the feature/unprivilege-user branch (https://github.com/warrensbox/terraform-switcher/tree/feature/unprivilege-user)
  2. dep ensure or go get -v -t -d ./...
  3. go build -v -o tfswitch
  4. Create bin path directory. Ex: mkdir /Users/warrenveerasingam/bin
  5. Add custom bin path to PATH env: export PATH=$PATH:/Users/warrenveerasingam/bin
  6. Test pre-release version ./tfswitch -b /Users/warrenveerasingam/bin/terraform. Install any version.
  7. Validate terraform version. terraform -v

I will merge feature/unprivilege-user branch to the master branch and release it once we can verify it's working as expected.

I will release an option with the ~/.tfswitch.conf soon but not for this release.

Please feel free to contribute and comment. Thanks for helping me test it out.

Thanks, This works for mac!!

from terraform-switcher.

andremacdowell avatar andremacdowell commented on September 26, 2024

I just gave my current user owner on the /usr/local/bin folder and worked like magic.

sudo chown -R $USER /usr/local/bin

Might not be great from a security perspective, but works none the less.

This actually works well if you just want the solution for local wsl2 usage.

from terraform-switcher.

troy-mac avatar troy-mac commented on September 26, 2024

@warrensbox any chance you will be fixing this on WSL Ubuntu? I have tried everything above, even chown of /usr/local/bin and nothing works. Not sure why there is a need to put everything in /usr/local/bin why can't a user just install to their home directory. BTW it worked great a couple years ago on my MAC and its a great concept, but it leaves a lot of us users out if it does not work in WSL or WSL2

from terraform-switcher.

troy-mac avatar troy-mac commented on September 26, 2024

Seems I got it to work with using snap install. BUT all that said when I open another terminal I get the same issue I have to sudo to use it, if I stay in the terminal that I installed it on I do not have to use sudo... FYI snap on WSL would not work until I ran these commands.
sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

https://github.com/microsoft/WSL/issues/5126

from terraform-switcher.

lauchlan105 avatar lauchlan105 commented on September 26, 2024

For anyone coming to this thread in 2022...

This script;

  • Works on WSL2 ✔ī¸
  • doesn't require sudo ✔ī¸
  • installs it only for the current user

Note: I chose ~/bin for TFSWITCH_DIR as that is also where tfswitch installs the terraform binaries. And, I've confirmed that changing terraform versions will not result in the tfswitch binary being removed.

# Location of where you'd like to save the binary
# This is not important to tfswitch, only important to this here script
TFSWITCH_DIR=~/bin

# go to home dir
cd ~

# download the install script
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh >> install.sh

# ensure the directory for the `tfswitch` bin exists
mkdir -p $TFSWITCH_DIR

# run install script (with -b to indicate desired path to save binary)
sh install.sh -b $TFSWITCH_DIR

# remove the install script as it is no longer needed
rm install.sh

# add the TFSWITCH_DIR to your path so the binaries can be executed from anywhere
# NOTE: this may be ~/.bashrc, or ~/.zshrc, or others, but if you don't know, safest not to change it 
echo 'export PATH="$PATH:'$TFSWITCH_DIR'"' >> ~/.bash_profile

If you cannot use tfswitch after restarting your terminal session...

  1. check the tfswitch binary exists at the path you specified for $TFSWITCH_DIR
    a. if it does exist, the last step is where things went wrong - it likely added the export to a
    file that is not included in your bash profile - The "Where to put it" section in this may help you.
  2. if you ran into issues at the installation step, try replacing sh with bash

from terraform-switcher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.