Skip to content

Instantly share code, notes, and snippets.

@robzr
Created October 1, 2021 17:34
Show Gist options
  • Save robzr/75c193d73dddeed7e59a542c5365d0a4 to your computer and use it in GitHub Desktop.
Save robzr/75c193d73dddeed7e59a542c5365d0a4 to your computer and use it in GitHub Desktop.
Terraform block to dynamically determine project name & workspace from path
locals {
project = reverse([
for potential in [
for i in range(2, length(local.project_path_segments)) : {
git_root = join("/", slice(local.project_path_segments, 0, i + 1))
name = local.project_path_segments[i]
workspace = join(
"--",
slice(
local.project_path_segments,
i,
length(local.project_path_segments),
)
)
}
] : potential
if fileexists("${potential.git_root}/.git/HEAD")
])[0]
project_path_segments = split("/", abspath(path.root))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment