Skip to content

Commit

Permalink
Merge pull request #34 from ivanilves/docker-include
Browse files Browse the repository at this point in the history
feat: add navigation over Dockerfile-based projects
  • Loading branch information
ivanilves committed Nov 17, 2022
2 parents 8f95996 + 3fa97e2 commit da9d7f6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# travelgrunt

Travel **[Terragrunt](https://terragrunt.gruntwork.io/)** or **[Terraform](https://www.terraform.io/)** directory tree as a first class passenger! :airplane:
Travel **[Terragrunt](https://terragrunt.gruntwork.io/)** or **[Terraform](https://www.terraform.io/)** or ... ANY directory tree as a first class passenger! :airplane:

## How to use?

Expand Down Expand Up @@ -32,6 +32,11 @@ mode: terraform_or_terragrunt
```
:arrow_up: this will navigate through **both** Terraform and Terragrunt projects inside the repo.

```
mode: dockerfile
```
:arrow_up: with this `travelgrunt` will navigate across Dockerfiles or Dockerfile templates.

## Shell aliases

It is **absolutely required** to use `bash` (or `zsh`) aliases. Start from something like this:
Expand Down
2 changes: 2 additions & 0 deletions fixtures/config/include/dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
# this is a dummy fixture Dockerfile
1 change: 1 addition & 0 deletions fixtures/config/travelgrunt.yml.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mode: dockerfile
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewConfig(path string) (cfg Config, err error) {
}

func validate(cfg Config) error {
allowedModes := []string{"terragrunt", "terraform", "terraform_or_terragrunt"}
allowedModes := []string{"terragrunt", "terraform", "terraform_or_terragrunt", "dockerfile"}

for _, mode := range allowedModes {
if cfg.Mode == mode {
Expand All @@ -62,6 +62,8 @@ func (cfg Config) IncludeFn() (fn func(os.DirEntry) bool) {
fn = include.IsTerraform
case "terraform_or_terragrunt":
fn = include.IsTerraformOrTerragrunt
case "dockerfile":
fn = include.IsDockerfile
default:
fn = nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestNewConfig(t *testing.T) {
"travelgrunt.yml.terragrunt": {cfg: getConfig("terragrunt", false), success: true, includeFn: include.IsTerragrunt},
"travelgrunt.yml.terraform": {cfg: getConfig("terraform", false), success: true, includeFn: include.IsTerraform},
"travelgrunt.yml.terraform_or_terragrunt": {cfg: getConfig("terraform_or_terragrunt", false), success: true, includeFn: include.IsTerraformOrTerragrunt},
"travelgrunt.yml.dockerfile": {cfg: getConfig("dockerfile", false), success: true, includeFn: include.IsDockerfile},
"travelgrunt.yml.invalid": {cfg: getConfig("", false), success: false, includeFn: nil},
"travelgrunt.yml.illegal": {cfg: getConfig("bogus", false), success: false, includeFn: nil},
"travelgrunt.yml.nonexistent": {cfg: getConfig("terragrunt", true), success: true, includeFn: include.IsTerragrunt},
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/include/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ func IsTerraform(d os.DirEntry) bool {
func IsTerraformOrTerragrunt(d os.DirEntry) bool {
return IsTerraform(d) || IsTerragrunt(d)
}

// IsDockerfile tells us if we operate on Dockerfile(s) or Dockerfile template(s)
func IsDockerfile(d os.DirEntry) bool {
return fileOrSymlink(d) && strings.Contains(strings.ToLower(d.Name()), "dockerfile")
}
9 changes: 6 additions & 3 deletions pkg/config/include/include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ func TestIncludeFn(t *testing.T) {
IsTerragrunt bool
IsTerraform bool
IsTerraformOrTerragrunt bool
IsDockerfile bool
}

testCases := map[string]testCase{
"../../../fixtures/config/include/terragrunt/terragrunt.hcl": testCase{true, false, true},
"../../../fixtures/config/include/terraform/main.tf": testCase{false, true, true},
"../../../fixtures/config/include/nothing/foo.bar": testCase{false, false, false},
"../../../fixtures/config/include/terragrunt/terragrunt.hcl": testCase{true, false, true, false},
"../../../fixtures/config/include/terraform/main.tf": testCase{false, true, true, false},
"../../../fixtures/config/include/dockerfile/Dockerfile": testCase{false, false, false, true},
"../../../fixtures/config/include/nothing/foo.bar": testCase{false, false, false, false},
}

err := filepath.WalkDir(fixturePath,
Expand All @@ -37,6 +39,7 @@ func TestIncludeFn(t *testing.T) {
assert.Equal(expected.IsTerragrunt, IsTerragrunt(d))
assert.Equal(expected.IsTerraform, IsTerraform(d))
assert.Equal(expected.IsTerraformOrTerragrunt, IsTerraformOrTerragrunt(d))
assert.Equal(expected.IsDockerfile, IsDockerfile(d))
}
}

Expand Down

0 comments on commit da9d7f6

Please sign in to comment.