From 842b406d41801a53b6501a6d3cc9995af8ff38aa Mon Sep 17 00:00:00 2001 From: Ivan Ilves Date: Sat, 22 Oct 2022 09:14:02 +0200 Subject: [PATCH] feat: better menu flow --- Makefile | 4 ++- README.md | 2 +- cmd/travelgrunt/main.go | 55 +++++++++++++++++++++++------------------ pkg/menu/menu.go | 6 ++++- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 064de7a..5f8c017 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,12 @@ NEXT_VERSION := v${API_VERSION}.${NEXT_PATCH} BUILD_PATH := ./cmd/${APP_NAME} RELEASE_PATH := ./release -main: dep build +default: dep build all: dep build install +deploy: build install + dep: go mod tidy go mod vendor diff --git a/README.md b/README.md index 10f4d2e..29d25d3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Travel **[Terragrunt](https://terragrunt.gruntwork.io/)** directory tree as a fi ## Shell aliases -It is **absolutelly required** to use `bash` (or `zsh`) aliases. Start from something like this: +It is **absolutely 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' diff --git a/cmd/travelgrunt/main.go b/cmd/travelgrunt/main.go index 23ee0d6..b36a05c 100644 --- a/cmd/travelgrunt/main.go +++ b/cmd/travelgrunt/main.go @@ -41,6 +41,33 @@ func writeFileAndExit(fileName string, data string) { os.Exit(0) } +func buildMenuFromTree(t tree.Tree) string { + var selected string + var parentID string + + for c := -1; c < t.LevelCount()-1; c++ { + if !t.HasChildren(c, parentID) { + selected = parentID + + break + } + + selected, err := menu.Build(t.ChildNames(c, parentID), terminal.Height(), parentID) + + if err != nil { + if err.Error() == "^C" { + os.Exit(1) + } + + log.Fatalf("failed to build menu: %s", err.Error()) + } + + parentID = t.ChildItems(c, parentID)[selected] + } + + return selected +} + func main() { flag.Usage = usage flag.Parse() @@ -51,8 +78,6 @@ func main() { os.Exit(0) } - matches := flag.Args() - rootPath, err := scm.RootPath() if err != nil { @@ -69,31 +94,13 @@ func main() { log.Fatalf("failed to collect Terragrunt project directories: %s", err.Error()) } - if err := filter.Validate(matches); err != nil { + if err := filter.Validate(flag.Args()); err != nil { log.Fatalf("invalid filter: %s", err.Error()) } - paths = filter.Apply(paths, matches) - - tree := tree.NewTree(paths) - - var selected string - var parentID string - - for c := -1; c < tree.LevelCount()-1; c++ { - if !tree.HasChildren(c, parentID) { - break - } - - selected, err = menu.Build(tree.ChildNames(c, parentID), terminal.Height()) - if err != nil { - log.Fatalf("failed to build menu: %s", err.Error()) - } - - parentID = tree.ChildItems(c, parentID)[selected] - - selected = parentID - } + selected := buildMenuFromTree( + tree.NewTree(filter.Apply(paths, flag.Args())), + ) if outFile != "" { writeFileAndExit(outFile, entries[selected]) diff --git a/pkg/menu/menu.go b/pkg/menu/menu.go index 8245a2f..b549d06 100644 --- a/pkg/menu/menu.go +++ b/pkg/menu/menu.go @@ -28,11 +28,15 @@ func getSize(itemCount int, size int) int { } // Build creates an interactive menu to chose Terragrunt project from -func Build(items []string, maxSize int) (selected string, err error) { +func Build(items []string, maxSize int, previous string) (selected string, err error) { if len(items) == 0 { return "", fmt.Errorf("no items") } + if len(previous) > 0 { + fmt.Printf("=> %s\n", previous) + } + if len(items) == 1 { return items[0], nil }