-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider: introduce
FindGoModuleVersion
helper for module versions
Before this change, we relied on the plugin libraries to expose their version. Unfortunately, this was incorrectly set in SDKv2[1] and non-existent in the framework[2]. The recommended approach is to instead dig into the build information via`runtime/debug.ReadBuildInfo()`[3] which is what we introduce here in a slightly safer and consistent way. [1]: hashicorp/terraform-plugin-sdk#1257 [2]: hashicorp/terraform-plugin-framework#855 [3]: https://pkg.go.dev/runtime/debug#ReadBuildInfo Signed-off-by: Jacob Bednarz <[email protected]>
- Loading branch information
1 parent
b68b789
commit 39e9d6a
Showing
4 changed files
with
56 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils | ||
|
||
import ( | ||
"runtime/debug" | ||
"strings" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
) | ||
|
||
// FindGoModuleVersion digs into the build information and extracts the version | ||
// of a module for use without the prefixed `v` (should it exist). | ||
func FindGoModuleVersion(modulePath string) *string { | ||
info, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
// shouldn't ever happen but just in case we aren't using modules | ||
return nil | ||
} | ||
|
||
for _, mod := range info.Deps { | ||
if mod.Path != modulePath { | ||
continue | ||
} | ||
|
||
version := mod.Version | ||
if strings.HasPrefix(version, "v") { | ||
version = strings.TrimPrefix(version, "v") | ||
} | ||
|
||
return cloudflare.StringPtr(version) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters