Add VersionString

We conditionally format version with VersionPrerelease in a number of
places. Add a package-level function where we can unify the version
format. Replace most of version formatting in terraform, but leave th
few instances set from the top-level package to make sure we don't break
anything before release.
This commit is contained in:
James Bardin 2016-07-21 14:07:16 -04:00
parent bd99c7a904
commit f66d1a10a4
5 changed files with 13 additions and 14 deletions

View File

@ -52,7 +52,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, err
}
}
client.DefaultHeader.Set(terraform.VersionHeader, terraform.Version)
client.DefaultHeader.Set(terraform.VersionHeader, terraform.VersionString())
client.Token = d.Get("token").(string)
return client, nil

View File

@ -397,7 +397,7 @@ func (c *Config) ValidateAccountId(accountId string) error {
var addTerraformVersionToUserAgent = request.NamedHandler{
Name: "terraform.TerraformVersionUserAgentHandler",
Fn: request.MakeAddToUserAgentHandler(
"terraform", terraform.Version, terraform.VersionPrerelease),
"terraform", terraform.VersionString()),
}
type awsLogger struct{}

View File

@ -92,13 +92,7 @@ func withRequestLogging() autorest.SendDecorator {
}
func setUserAgent(client *autorest.Client) {
var version string
if terraform.VersionPrerelease != "" {
version = fmt.Sprintf("%s-%s", terraform.Version, terraform.VersionPrerelease)
} else {
version = terraform.Version
}
version := terraform.VersionString()
client.UserAgent = fmt.Sprintf("HashiCorp-Terraform-v%s", version)
}

View File

@ -85,11 +85,7 @@ func (c *Config) loadAndValidate() error {
}
}
versionString := terraform.Version
prerelease := terraform.VersionPrerelease
if len(prerelease) > 0 {
versionString = fmt.Sprintf("%s-%s", versionString, prerelease)
}
versionString := terraform.VersionString()
userAgent := fmt.Sprintf(
"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)

View File

@ -1,6 +1,8 @@
package terraform
import (
"fmt"
"github.com/hashicorp/go-version"
)
@ -20,3 +22,10 @@ var SemVersion = version.Must(version.NewVersion(Version))
// VersionHeader is the header name used to send the current terraform version
// in http requests.
const VersionHeader = "Terraform-Version"
func VersionString() string {
if VersionPrerelease != "" {
return fmt.Sprintf("%s-%s", Version, VersionPrerelease)
}
return Version
}