From 81efcea60391813bc5e550dd5f82b2616307212d Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Tue, 2 Jun 2020 15:33:15 -0700 Subject: [PATCH 1/2] feat(influx): add version to influx CLI closes: #14821 --- Makefile | 2 ++ cmd/influx/main.go | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6ef7868c220..983f0b9dd8b 100644 --- a/Makefile +++ b/Makefile @@ -84,6 +84,8 @@ $(CMDS): $(SOURCES) # Ease of use build for just the go binary influxd: bin/$(GOOS)/influxd +influx: bin/$(GOOS)/influx + # # Define targets for the web ui # diff --git a/cmd/influx/main.go b/cmd/influx/main.go index e6af77e6661..d0f0fa5daef 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "sync" + "time" "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/bolt" @@ -27,6 +28,12 @@ import ( const maxTCPConnections = 10 +var ( + version = "dev" + commit = "none" + date = time.Now().UTC().Format(time.RFC3339) +) + func main() { influxCmd := influxCmd() if err := influxCmd.Execute(); err != nil { @@ -218,10 +225,23 @@ func (b *cmdInfluxBuilder) cmd(childCmdFns ...func(f *globalFlags, opt genericCL // completion command goes last, after the walk, so that all // commands have every flag listed in the bash|zsh completions. - cmd.AddCommand(completionCmd(cmd)) + cmd.AddCommand( + completionCmd(cmd), + cmdVersion(), + ) return cmd } +func cmdVersion() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Print the influx CLI version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Influx CLI %s (git: %s) build_date: %s\n", version, commit, date) + }, + } +} + func influxCmd(opts ...genericCLIOptFn) *cobra.Command { builder := newInfluxCmdBuilder(opts...) return builder.cmd( From 3aa8f1cfad9a03c834e458caf1bb636159128d61 Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Tue, 2 Jun 2020 15:43:08 -0700 Subject: [PATCH 2/2] feat(influx): add User-Agent header to the CLI http calls closes: #18336 --- CHANGELOG.md | 1 + cmd/influx/main.go | 13 ++++++++++++- pkg/httpc/options.go | 42 ++++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df21b2e59b9..180f5caec57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ 1. [18279](https://github.com/influxdata/influxdb/pull/18279): Make all pkg applications stateful via stacks 1. [18322](https://github.com/influxdata/influxdb/pull/18322): Add ability to export a stack's existing (as they are in the platform) resource state as a pkg 1. [18334](https://github.com/influxdata/influxdb/pull/18334): Update influx pkg commands with improved usage and examples in long form. +1. [18344](https://github.com/influxdata/influxdb/pull/18344): Extend influx CLI with version and User-Agent. ### Bug Fixes diff --git a/cmd/influx/main.go b/cmd/influx/main.go index d0f0fa5daef..4153c1adebc 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "strings" "sync" "time" @@ -51,7 +52,17 @@ func newHTTPClient() (*httpc.Client, error) { return httpClient, nil } - c, err := http.NewHTTPClient(flags.Host, flags.Token, flags.skipVerify) + userAgent := fmt.Sprintf( + "influx/%s (%s) Sha/%s Date/%s", + version, runtime.GOOS, commit, date, + ) + + c, err := http.NewHTTPClient( + flags.Host, + flags.Token, + flags.skipVerify, + httpc.WithUserAgentHeader(userAgent), + ) if err != nil { return nil, err } diff --git a/pkg/httpc/options.go b/pkg/httpc/options.go index bfc142a15c7..fe4544706bc 100644 --- a/pkg/httpc/options.go +++ b/pkg/httpc/options.go @@ -42,34 +42,26 @@ func WithAuth(fn func(r *http.Request)) ClientOptFn { // WithAuthToken provides token auth for requests. func WithAuthToken(token string) ClientOptFn { - return func(opts *clientOpt) error { - fn := func(r *http.Request) { - r.Header.Set("Authorization", "Token "+token) - } - return WithAuth(fn)(opts) - } + return WithAuth(func(r *http.Request) { + r.Header.Set("Authorization", "Token "+token) + }) } // WithSessionCookie provides cookie auth for requests to mimic the browser. // Typically, session is influxdb.Session.Key. func WithSessionCookie(session string) ClientOptFn { - return func(opts *clientOpt) error { - fn := func(r *http.Request) { - r.AddCookie(&http.Cookie{ - Name: "session", - Value: session, - }) - } - return WithAuth(fn)(opts) - } + return WithAuth(func(r *http.Request) { + r.AddCookie(&http.Cookie{ + Name: "session", + Value: session, + }) + }) } // WithContentType sets the content type that will be applied to the requests created // by the Client. func WithContentType(ct string) ClientOptFn { - return func(opt *clientOpt) error { - return WithHeader(headerContentType, ct)(opt) - } + return WithHeader(headerContentType, ct) } func withDoer(d doer) ClientOptFn { @@ -91,6 +83,11 @@ func WithHeader(header, val string) ClientOptFn { } } +// WithUserAgentHeader sets the user agent for the http client requests. +func WithUserAgentHeader(userAgent string) ClientOptFn { + return WithHeader("User-Agent", userAgent) +} + // WithHTTPClient sets the raw http client on the httpc Client. func WithHTTPClient(c *http.Client) ClientOptFn { return func(opt *clientOpt) error { @@ -136,12 +133,9 @@ func WithWriterFn(fn WriteCloserFn) ClientOptFn { // WithWriterGZIP gzips the request body generated from this client. func WithWriterGZIP() ClientOptFn { - return func(opt *clientOpt) error { - fn := func(w io.WriteCloser) (string, string, io.WriteCloser) { - return headerContentEncoding, "gzip", gzip.NewWriter(w) - } - return WithWriterFn(fn)(opt) - } + return WithWriterFn(func(w io.WriteCloser) (string, string, io.WriteCloser) { + return headerContentEncoding, "gzip", gzip.NewWriter(w) + }) } func defaultHTTPClient(scheme string, insecure bool) *http.Client {