Skip to content

Commit

Permalink
version: limit core version to 3 segments
Browse files Browse the repository at this point in the history
The go-version library we use for parsing versions from the plugin
supports 4-segmented versions.
This may not be ideal for us, as we want to limit the sprawling nature
of plugin installations, which if we start accepting sub-patch version
bumps, may become quite strange.
The release workflows we offer as template does not take that into
account, and I'm not sure our docs do as well.

Since there are many unknowns here, 4-segmented version numbers are not
semver-valid, and we do not know how tooling will react, we ultimately
decide not to allow those in the SDK.

If a developer tries to define a 4-segmented version number, the plugin
will crash instantly, at least giving users a message quickly that the
version number is invalid, and that they need to limit themselves to a
3-segment version number.
  • Loading branch information
lbajolet-hashicorp committed Apr 15, 2024
1 parent ddbda65 commit 92242be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 6 additions & 10 deletions version/version.go
Expand Up @@ -47,6 +47,11 @@ func InitializePluginVersion(vers, versionPrerelease string) *PluginVersion {
// As NewRawVersion, if the version is invalid, it will panic.
func NewRawVersion(rawSemVer string) *PluginVersion {
vers := version.Must(version.NewVersion(rawSemVer))

if len(vers.Segments()) != 3 {
panic(fmt.Sprintf("versions should only have 3 segments, %q had %d", rawSemVer, len(vers.Segments())))
}

return &PluginVersion{
version: vers.Core().String(),
versionPrerelease: vers.Prerelease(),
Expand Down Expand Up @@ -81,16 +86,7 @@ func NewPluginVersion(vers, versionPrerelease, versionMetadata string) *PluginVe
versionRawString = fmt.Sprintf("%s+%s", versionRawString, versionMetadata)
}

// This call initializes the SemVer to make sure that if Packer crashes due
// to an invalid SemVer it's at the very beginning of the Packer run.
semVer := version.Must(version.NewVersion(versionRawString))

return &PluginVersion{
version: semVer.Core().String(),
versionPrerelease: semVer.Prerelease(),
versionMetadata: semVer.Metadata(),
semVer: semVer,
}
return NewRawVersion(versionRawString)
}

type PluginVersion struct {
Expand Down
8 changes: 8 additions & 0 deletions version/version_test.go
Expand Up @@ -62,6 +62,14 @@ func Test_PluginVersionCreate(t *testing.T) {
true,
"",
},
{
"4-parts version, should not be accepted",
"1.1.1.1",
"",
"",
true,
"",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 92242be

Please sign in to comment.