Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(influx): extend CLI with versoin and user-agent #18344

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
35 changes: 33 additions & 2 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/bolt"
Expand All @@ -27,6 +29,12 @@ import (

const maxTCPConnections = 10

var (
version = "dev"
commit = "none"
date = time.Now().UTC().Format(time.RFC3339)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fly-by comment: this isn't the build date (as noted in the PR description); it's the time that the influx process was started.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. I borrowed this from the influxd binary here:

https://github.com/influxdata/influxdb/blob/master/cmd/influxd/main.go#L27

just noticed I dont' have the fmt.Sprintf, but that shouldn't matter yah?

It looks like the date isn't provided as an LD flag either, so that definitely can change 🤔. What do you think is the best way forward here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed the fmt.Sprint isn't necessary; time.Format already returns a string.

I think setting the date at link time is the correct approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we do set the date as a linker flag in goreleaser after all:

ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}}

ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}}

Any tooling we have to inspect the resulting date would know that version == "dev" indicates the date would be meaningless.

Nothing to change here after all.

)

func main() {
influxCmd := influxCmd()
if err := influxCmd.Execute(); err != nil {
Expand All @@ -44,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
}
Expand Down Expand Up @@ -218,10 +236,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(
Expand Down
42 changes: 18 additions & 24 deletions pkg/httpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down