Does the <pathExists> config file function accept ...
# help
n
Does the pathExists config file function accept wildcards? For example, something like
Copy code
pathExists $project._path "*.tf"
l
@narrow-dinner-45042 unfortunately not right now, but it’s something I’ve been thinking off. We’ve got a new
.DetectedProjects
variable coming soon that could help with this use case. It currently works with Infracost Cloud, but isn’t released in a CLI version yet. If you send me your full config file I can have a look and see if it would help.
n
Heres the config file I have so far
Copy code
projects:
{{- range $project := matchPaths "deploy/terraform/:name(.*)" }}
  {{- if isDir $project._path }}
    {{- if pathExists $project._path "*.tf" }}
    - path: {{ $project._path }}
      name: {{ $project.name }}
    {{- end}}
  {{- end}}
{{- end }}
I was trying to match every directory in
deploy/terraform
that contains a terraform file. Originally I did not have the
pathExists
check, but that causes some errors because some directories are being matched that only contain subdirectories, no tf files
alternatively, something like this works
Copy code
projects:
{{- range $project := matchPaths "deploy/terraform/:name(.*)/:file.tf" }}
  - path: {{ $project._dir }}
    name: {{ $project.name }}
{{- end }}
but creates many duplicate projects since there are many directories with multiple files ending in
.tf
. Perhaps thats fine, but was looking for a cleaner solution if possible
l
@narrow-dinner-45042 you could try this:
Copy code
version: 0.1

projects:
{{- range $project := .DetectedProjects }}
  - path: {{ $project.Path }}
    name: {{ $project.Name }}
{{- end }}
It will only work in Infracost Cloud and using the latest master build of Infracost though just now.
n
will this still work for the CI/CD github integration if my infracost trial runs out?
l
Yep
It will be in the next release of the Infracost CLI as well.
n
got it, thank you!
@little-author-61621 how does the
.DetectedProjects
determine which paths are project and which arent? Ive noticed some paths not appearing in the infracost cloud repo view, but its unclear to me why
l
It should be any directory that has a tf file with either a provider block or a Terraform backend block. We're still iterating on this though so would be good to know what the directory has to see if we need to adjust it.
n
Hm I dont have any exact examples in front of me since for now Ive found a workaround (see below). Dont want to mess around with the config too much since its actively being used in our PR comments now. I can play around with
.DetectedProjects
when the next CLI release comes out and provide additional feedback
Copy code
# See <https://www.infracost.io/docs/features/config_file/>

version: 0.1

# We match every subdirectory within `deploy/terraform` that contains a *.tf file, excluding modules/
projects:
{{ $lastProject := "" }} # keep track of most recent project to avoid duplicate entries
{{- range $project := matchPaths "deploy/terraform/:subdir(.*)/:<http://file.tf|file.tf>" }}
  {{- if not (startsWith $project.subdir "modules/") }}
    {{- if ne $project.subdir $lastProject }}
    {{ $lastProject = $project.subdir }}
    - path: {{ $project._dir }}
      name: {{ $project.subdir }}
    {{- end}}
  {{- end}}
{{- end }}