From 0603cda5eeb8e7caaaf2cfb99b02fe4d3c5ba72d Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 4 Apr 2018 19:13:54 -0700 Subject: [PATCH] Add a helper for generating Consul's user-agent string --- lib/useragent.go | 29 +++++++++++++++++++++++++++++ lib/useragent_test.go | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lib/useragent.go create mode 100644 lib/useragent_test.go 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) + } +}