Skip to content

Commit

Permalink
feat(GH-8): travel to the repo root directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Oct 18, 2022
1 parent ae6515f commit 2655ea1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
## How to use?

* `cd` to the directory of your [locally cloned] Terragrunt/Terraform Git repo;
* run `ttg` command there ([optional] arguments are "path filter" matches);
* run `ttg` command there :rocket: ([optional] arguments are "path filter" matches);
* use arrow keys to navigate the list and `/` key to search for specific projects;

## Shell aliases

It is **highly** recommended to use `bash` (or `zsh`) aliases. Start from something like this:
```
alias ttg='_ttg(){ ttg -outFile ~/.ttg-path ${@} && cd "$(cat ~/.ttg-path)" }; _ttg'
alias ttt='ttg --top'
```

:bulb: `ttg --top` is a "shortcut" 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
Expand All @@ -31,7 +34,11 @@ experience, i.e. you will need to exit subshell before you "jump" to the next pr

:bulb: Set `GITHUB_TOKEN` environmental variable.

Run `make full-release` recipe, which is equal to run following, one by one:

* `make clean` - cleanup project tree from previously built artifacts;
* `make dep` - ensure all dependencies are installed;
* `make release` - create release artifacts;
* `make next-version-tag` - tag your HEAD with the incremented version;
* `git push --tags` - push your new tag to Git;
* `make push-tags` - push your new tag to Git;
* `make github` - create a GitHub release using your artifacts;
24 changes: 17 additions & 7 deletions cmd/ttg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (

var appVersion = "default"

var version bool
var outFile string
var top bool
var version bool

func init() {
flag.BoolVar(&version, "version", false, "print application version and exit")
flag.StringVar(&outFile, "outFile", "", "output project path into the file specified instead of spawning a shell")
flag.BoolVar(&top, "top", false, "get to the repository top level (root) path and exit")
flag.BoolVar(&version, "version", false, "print application version and exit")
}

func usage() {
Expand All @@ -31,6 +33,14 @@ func usage() {
flag.PrintDefaults()
}

func writeFileAndExit(fileName string, data string) {
if err := file.Write(fileName, data); err != nil {
log.Fatalf("failed to write file (%s): %s", fileName, err.Error())
}

os.Exit(0)
}

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())
Expand All @@ -53,6 +63,10 @@ func main() {
log.Fatalf("failed to extract top level filesystem path from SCM: %s", err.Error())
}

if top {
writeFileAndExit(outFile, rootPath)
}

entries, err := directory.Collect(rootPath)

if err != nil {
Expand All @@ -70,11 +84,7 @@ func main() {
}

if outFile != "" {
if err := file.Write(outFile, entries[selected]); err != nil {
log.Fatalf("failed to write output file: %s", err.Error())
}

os.Exit(0)
writeFileAndExit(outFile, entries[selected])
}

shell.Spawn(entries[selected])
Expand Down

0 comments on commit 2655ea1

Please sign in to comment.