From 19c9b28f19de974cd671bf394183472b0dedf15b Mon Sep 17 00:00:00 2001 From: Ivan Ilves Date: Sat, 22 Oct 2022 08:32:22 +0200 Subject: [PATCH] feat: decommision shell package --- README.md | 17 ++++---- cmd/travelgrunt/main.go | 9 +---- pkg/shell/shell.go | 62 ----------------------------- pkg/shell/shell_test.go | 86 ----------------------------------------- 4 files changed, 10 insertions(+), 164 deletions(-) delete mode 100644 pkg/shell/shell.go delete mode 100644 pkg/shell/shell_test.go diff --git a/README.md b/README.md index 8daee74..10f4d2e 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,27 @@ # travelgrunt -**T**ravel **T**erra**g**runt directory tree as a first class passenger! :sunglasses: +Travel **[Terragrunt](https://terragrunt.gruntwork.io/)** directory tree as a first class passenger! :airplane: ## How to use? * `cd` to the directory of your [locally cloned] Terragrunt/Terraform Git repo; -* run `travelgrunt` command there :rocket: ([optional] arguments are "path filter" matches); -* use arrow keys to navigate the list and `/` key to search for specific projects; +* run **tg** alias there :rocket: ([optional] arguments are "path filter" matches); +* use arrow keys to navigate the list and `/` key to search for specific items; ## Shell aliases -It is **highly** recommended to use `bash` (or `zsh`) aliases. Start from something like this: +It is **absolutelly required** to use `bash` (or `zsh`) aliases. Start from something like this: ``` alias tg='_tg(){ travelgrunt --out-file ~/.tg-path ${@} && cd "$(cat ~/.tg-path)" }; _tg' alias tt='_tt(){ travelgrunt --top --out-file ~/.tg-path && cd "$(cat ~/.tg-path)" }; _tt' ``` -:bulb: `travelgrunt --top` is a "shortcut" that brings you to the top level path of your repository. +:bulb: **tt** is a "convenience alias" that brings you to the top level path of your repository. ### Why aliases? -Core aspect of this program is the ability to change working directory while staying **inside the current shell**. -This can not be done by the program itself, because of obvious security related `POSIX` limitations. Without instrumenting -the shell with aliases `travelgrunt` still can kinda work, but will provide you with much more awkward and second class user -experience, i.e. you will need to exit subshell before you "jump" to the next project. :weary: +Core feature of this program is the ability to change working directory while staying **inside the current shell**. +This **can not** be done by the program itself, because of `POSIX` security limitations. Without instrumenting +the shell with aliases `travelgrunt` will not work! ## How to build? diff --git a/cmd/travelgrunt/main.go b/cmd/travelgrunt/main.go index 947fdbe..23ee0d6 100644 --- a/cmd/travelgrunt/main.go +++ b/cmd/travelgrunt/main.go @@ -11,7 +11,6 @@ import ( "github.com/ivanilves/travelgrunt/pkg/filter" "github.com/ivanilves/travelgrunt/pkg/menu" "github.com/ivanilves/travelgrunt/pkg/scm" - "github.com/ivanilves/travelgrunt/pkg/shell" "github.com/ivanilves/travelgrunt/pkg/terminal" ) @@ -28,7 +27,7 @@ func init() { } func usage() { - println("Usage: " + shell.Name() + " [ ... ]") + println("Usage: " + os.Args[0] + " [ ... ]") println("") println("Options:") flag.PrintDefaults() @@ -43,10 +42,6 @@ func writeFileAndExit(fileName string, data string) { } func main() { - if shell.IsRunningInside() { - log.Fatalf("%s already running (pid: %d), please type \"exit\" to return to the parent shell first", shell.Name(), shell.Getppid()) - } - flag.Usage = usage flag.Parse() @@ -104,5 +99,5 @@ func main() { writeFileAndExit(outFile, entries[selected]) } - shell.Spawn(entries[selected]) + log.Fatal("Please configure shell aliases as described: https://github.com/ivanilves/travelgrunt#shell-aliases") } diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go deleted file mode 100644 index da72507..0000000 --- a/pkg/shell/shell.go +++ /dev/null @@ -1,62 +0,0 @@ -package shell - -import ( - "fmt" - "os" - "os/exec" - "strconv" - "strings" -) - -const defaultShell = "bash" - -func detectShell() string { - shell := os.Getenv("SHELL") - - if len(shell) == 0 { - return defaultShell - } - - if strings.HasSuffix(shell, "/bash") || strings.HasSuffix(shell, "/zsh") { - return shell - } - - if shell == "/bin/true" || shell == "/bin/false" { - return shell - } - - return defaultShell -} - -// Name returns a called binary name -func Name() string { - return os.Args[0] -} - -// IsRunningInside tells us if we try to run inside a shell spawned by travelgrunt -func IsRunningInside() bool { - return os.Getenv("TTG") == "true" -} - -// Getppid returns numerical process ID of the parent travelgrunt process -func Getppid() int { - ppid, _ := strconv.Atoi(os.Getenv("TTG_PID")) - - return ppid -} - -// Spawn creates a shell in the working directory of selected Terragrunt project -func Spawn(path string) error { - cmd := exec.Command(detectShell()) - - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - - cmd.Env = os.Environ() - cmd.Env = append(cmd.Env, "TTG=true", fmt.Sprintf("TTG_PID=%d", os.Getpid())) - - cmd.Dir = path - - return cmd.Run() -} diff --git a/pkg/shell/shell_test.go b/pkg/shell/shell_test.go deleted file mode 100644 index 9f8828d..0000000 --- a/pkg/shell/shell_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package shell - -import ( - "os" - - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestDetectShell(t *testing.T) { - assert := assert.New(t) - - cases := map[string]string{ - "/bin/bash": "/bin/bash", - "/bin/zsh": "/bin/zsh", - "/bin/yolo": defaultShell, - "": defaultShell, - } - - for input, expected := range cases { - os.Setenv("SHELL", input) - - assert.Equal(expected, detectShell()) - } -} - -func TestName(t *testing.T) { - assert := assert.New(t) - - assert.Equal(os.Args[0], Name()) -} - -func TestIsRunningInside(t *testing.T) { - assert := assert.New(t) - - cases := map[string]bool{ - "true": true, - "false": false, - "whatever": false, - } - - for input, expected := range cases { - os.Setenv("TTG", input) - - assert.Equal(expected, IsRunningInside()) - } - - os.Setenv("TTG", "") - - assert.Equal(false, IsRunningInside()) -} - -func TestGetppid(t *testing.T) { - assert := assert.New(t) - - cases := map[string]int{ - "42424242": 42424242, - "xyz": 0, - } - - for input, expected := range cases { - os.Setenv("TTG_PID", input) - - assert.Equal(expected, Getppid()) - } - - os.Setenv("TTG_PID", "") - - assert.Equal(0, Getppid()) -} - -func TestSpawn(t *testing.T) { - assert := assert.New(t) - - cases := map[string]bool{ - "/bin/true": true, - "/bin/false": false, - } - - for input, expected := range cases { - os.Setenv("SHELL", input) - - assert.Equal(expected, Spawn("/") == nil) - } -}