Add a helper for generating Consul's user-agent string
This commit is contained in:
parent
34be4e21d2
commit
523bcd5c1e
|
@ -0,0 +1,29 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/hashicorp/consul/version"
|
||||
)
|
||||
|
||||
var (
|
||||
// projectURL is the project URL.
|
||||
projectURL = "https://www.consul.io/"
|
||||
|
||||
// rt is the runtime - variable for tests.
|
||||
rt = runtime.Version()
|
||||
|
||||
// versionFunc is the func that returns the current version. This is a
|
||||
// function to take into account the different build processes and distinguish
|
||||
// between enterprise and oss builds.
|
||||
versionFunc = func() string {
|
||||
return version.GetHumanVersion()
|
||||
}
|
||||
)
|
||||
|
||||
// UserAgent returns the consistent user-agent string for Consul.
|
||||
func UserAgent() string {
|
||||
return fmt.Sprintf("Consul/%s (+%s; %s)",
|
||||
versionFunc(), projectURL, rt)
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUserAgent(t *testing.T) {
|
||||
projectURL = "https://consul-test.com"
|
||||
rt = "go5.0"
|
||||
versionFunc = func() string { return "1.2.3" }
|
||||
|
||||
act := UserAgent()
|
||||
|
||||
exp := "Consul/1.2.3 (+https://consul-test.com; go5.0)"
|
||||
if exp != act {
|
||||
t.Errorf("expected %q to be %q", act, exp)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue