metio / terraform-provider-git

Terraform provider for local Git operations
https://registry.terraform.io/providers/metio/git/
BSD Zero Clause License
6 stars 1 forks source link

add repo 'name' property to 'git_repository' #304

Open cringdahl opened 2 days ago

cringdahl commented 2 days ago

I'd like to get the name of a repository from the 'git' provider. The path basename is not guaranteed to be the repo name, so I'd rather not rely on it.

The closest thing git gives to a canonical repo name is via the remote origin url:

basename -s .git $(git config --get remote.origin.url)

It seems like a fairly straightforward datum to report back, but I'm no Go coder.

Thanks!

sebhoss commented 1 day ago

Hey, thanks for opening this!

It's currently possible to get what you want using some built-in terraform/tofu functions. I've pushed an example to https://github.com/metio/terraform-provider-git/blob/main/terratest/data-sources/git_remote/single_remote/outputs.tf#L20-L29 which uses the git_remote data source. Although be aware that remotes can have more than one URL and Git allows to use aliases, thus a remote URL might not even contain a '/'.

Does the example code work for you? Otherwise I would probably add a provider defined function to this provider instead of adding a property to an existing resource, since extracting the basename of a remote URL might be useful in various scenarios.