diff --git a/cmd/goss/goss.go b/cmd/goss/goss.go index 670094248..e7ff90540 100644 --- a/cmd/goss/goss.go +++ b/cmd/goss/goss.go @@ -18,8 +18,6 @@ import ( "github.com/urfave/cli" ) -var version string - // converts a cli context into a goss Config func newRuntimeConfigFromCLI(c *cli.Context) *util.Config { cfg := &util.Config{ @@ -69,7 +67,7 @@ func timeoutFlag(value time.Duration) cli.DurationFlag { func main() { app := cli.NewApp() app.EnableBashCompletion = true - app.Version = version + app.Version = util.Version app.Name = "goss" app.Usage = "Quick and Easy server validation" app.Flags = []cli.Flag{ diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index 7966a7167..220ec4e8c 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -204,7 +204,9 @@ http: request-headers: - "Foo: bar" headers: ["Content-Type: application/json"] - body: ['"Foo": "bar"'] + body: + - '"Foo": "bar"' + - '"User-Agent": "goss/0.0.0"' http://httpbin/headers?host: status: 200 timeout: 60000 diff --git a/release-build.sh b/release-build.sh index 0e1f7bfbb..2922fdac9 100755 --- a/release-build.sh +++ b/release-build.sh @@ -22,7 +22,7 @@ fi output="${output_dir}/${output_fname}" GOOS="${os}" GOARCH="${arch}" CGO_ENABLED=0 go build \ - -ldflags "-X main.version=${version_stamp} -s -w" \ + -ldflags "-X github.com/goss-org/goss/util.Version=${version_stamp} -s -w" \ -o "${output}" \ github.com/goss-org/goss/cmd/goss diff --git a/system/http.go b/system/http.go index cdbf589cf..d5e5abec7 100644 --- a/system/http.go +++ b/system/http.go @@ -16,6 +16,9 @@ import ( "github.com/goss-org/goss/util" ) +const USER_AGENT_HEADER_PREFIX = "user-agent:" +const DEFAULT_USER_AGENT_PREFIX = "goss/" + type HTTP interface { HTTP() string Status() (int, error) @@ -47,6 +50,11 @@ type DefHTTP struct { func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { headers := http.Header{} + + if !hasUserAgentHeader(config.RequestHeader) { + config.RequestHeader = append(config.RequestHeader, fmt.Sprintf("%s %s%s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT_PREFIX, util.Version)) + } + for _, r := range config.RequestHeader { str := strings.SplitN(r, ": ", 2) headers.Add(str[0], str[1]) @@ -209,3 +217,12 @@ func (u *DefHTTP) Body() (io.Reader, error) { return u.resp.Body, nil } + +func hasUserAgentHeader(headers []string) bool { + for _, header := range headers { + if strings.HasPrefix(strings.ToLower(header), USER_AGENT_HEADER_PREFIX) { + return true + } + } + return false +} diff --git a/util/build.go b/util/build.go new file mode 100644 index 000000000..9deff372a --- /dev/null +++ b/util/build.go @@ -0,0 +1,3 @@ +package util + +var Version string