From aca0033e5939cedaa9b3649ad359c1b72d7e740f Mon Sep 17 00:00:00 2001 From: Tim Krueger Date: Fri, 11 Sep 2020 14:19:54 +0200 Subject: [PATCH] Execute tfswitch after cd in fish shell Add a snippet to execute tfswitch after a `cd` if a `tf` file with an `required_version` is present. This works only in fish shell [1]. [1]: http://fishshell.com --- www/docs/Quick-Start.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/www/docs/Quick-Start.md b/www/docs/Quick-Start.md index 867aec7a..de3bff20 100644 --- a/www/docs/Quick-Start.md +++ b/www/docs/Quick-Start.md @@ -37,6 +37,28 @@ terraform { ``` drawing +**Automatically switch with fish shell** + +Add the following to the end of your `~/.config/fish/config.fish` file: + +```sh +function switch_terraform --on-event fish_postexec + string match --regex '^cd\s' "$argv" > /dev/null + set --local is_command_cd $status + + if test $is_command_cd -eq 0 + if count *.tf > /dev/null + + grep -c "required_version" *.tf > /dev/null + set --local tf_contains_version $status + + if test $tf_contains_version -eq 0 + command tfswitch + end + end + end +end +``` ### Use .tfswitch.toml file (For non-admin - users with limited privilege on their computers) This is similiar to using a .tfswitchrc file, but you can specify a custom binary path for your terraform installation @@ -109,4 +131,4 @@ cd(){ tfswitch fi } -``` \ No newline at end of file +```