Skip to content

Commit

Permalink
Merge pull request #14 from ivanilves/menu-flow
Browse files Browse the repository at this point in the history
feat: better menu flow
  • Loading branch information
vonrabbe authored Oct 22, 2022
2 parents 8a86a4c + 842b406 commit 4b577bd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
55 changes: 31 additions & 24 deletions cmd/travelgrunt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -51,8 +78,6 @@ func main() {
os.Exit(0)
}

matches := flag.Args()

rootPath, err := scm.RootPath()

if err != nil {
Expand All @@ -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])
Expand Down
6 changes: 5 additions & 1 deletion pkg/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4b577bd

Please sign in to comment.