diff --git a/lib/useragent.go b/lib/useragent.go new file mode 100644 index 0000000000..84e76d4dfb --- /dev/null +++ b/lib/useragent.go @@ -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) +} diff --git a/lib/useragent_test.go b/lib/useragent_test.go new file mode 100644 index 0000000000..3ebaa9ecb7 --- /dev/null +++ b/lib/useragent_test.go @@ -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) + } +}