feat: Add --extra-http-header flag (#372)

This commit is contained in:
Marko Mikulicic
2022-04-13 19:22:18 +02:00
committed by GitHub
parent 88ba3464cd
commit 30e64c5cc9
3 changed files with 57 additions and 10 deletions

View File

@ -17,15 +17,16 @@ import (
)
const (
tokenFlagName = "token"
hostFlagName = "host"
skipVerifyFlagName = "skip-verify"
traceIdFlagName = "trace-debug-id"
configPathFlagName = "configs-path"
configNameFlagName = "active-config"
httpDebugFlagName = "http-debug"
printJsonFlagName = "json"
hideHeadersFlagName = "hide-headers"
tokenFlagName = "token"
hostFlagName = "host"
skipVerifyFlagName = "skip-verify"
traceIdFlagName = "trace-debug-id"
extraHttpHeaderFlagName = "extra-http-header"
configPathFlagName = "configs-path"
configNameFlagName = "active-config"
httpDebugFlagName = "http-debug"
printJsonFlagName = "json"
hideHeadersFlagName = "hide-headers"
)
// newCli builds a CLI core that reads from stdin, writes to stdout/stderr, manages a local config store,
@ -91,7 +92,19 @@ func newApiClient(ctx *cli.Context, configSvc config.Service, injectToken bool)
configParams.TraceId = api.PtrString(ctx.String(traceIdFlagName))
}
return api.NewAPIClient(api.NewAPIConfig(configParams)), nil
apiConfig := api.NewAPIConfig(configParams)
if ctx.IsSet(extraHttpHeaderFlagName) {
for _, h := range ctx.StringSlice(extraHttpHeaderFlagName) {
k, v, ok := stringsCut(h, ":")
if !ok {
return nil, fmt.Errorf(`header flag syntax "key:value", missing value in %q`, h)
}
apiConfig.AddDefaultHeader(k, v)
}
}
return api.NewAPIClient(apiConfig), nil
}
func withContext() cli.BeforeFunc {
@ -170,6 +183,10 @@ type CommonStringFlag struct {
cli.StringFlag
}
type CommonStringSliceFlag struct {
cli.StringSliceFlag
}
// NOTE: urfave/cli has dedicated support for global flags, but it only parses those flags
// if they're specified before any command names. This is incompatible with the old influx
// CLI, which repeatedly registered common flags on every "leaf" command, forcing the flags
@ -212,6 +229,11 @@ func coreFlags() []cli.Flag {
Hidden: true,
EnvVar: "INFLUX_TRACE_DEBUG_ID",
}},
&CommonStringSliceFlag{cli.StringSliceFlag{
Name: extraHttpHeaderFlagName,
Hidden: true,
EnvVar: "INFLUX_EXTRA_HTTP_HEADER",
}},
&CommonBoolFlag{cli.BoolFlag{
Name: httpDebugFlagName,
}},

9
cmd/influx/util.go Normal file
View File

@ -0,0 +1,9 @@
//go:build go1.18
package main
import "strings"
func stringsCut(s, sep string) (before, after string, found bool) {
return strings.Cut(s, sep)
}

16
cmd/influx/util_go117.go Normal file
View File

@ -0,0 +1,16 @@
//go:build !go1.18
// remove this backward compat shim once we either:
// a) upgrade cross-builder docker image in https://github.com/influxdata/edge/
// b) switch the CI system for this repo away from using cross-builder docker image.
package main
import "strings"
func stringsCut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}