Replace token flags with = to prevent bad parsing of leading dash in token (#399)
This commit is contained in:
@ -3,8 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@ -91,9 +93,34 @@ func newApp() cli.App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This creates a new slice and replaces `-t "-FOO-TOKEN"` with `-t=-FOO-TOKEN`
|
||||||
|
// This is necessary to do because the command line arg:
|
||||||
|
// `-t "-FOO-TOKEN"`` will be parsed as two separate flags instead of a flag and token value.
|
||||||
|
func ReplaceTokenArg(args []string) []string {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
newArgs := make([]string, len(args))
|
||||||
|
copy(newArgs, args)
|
||||||
|
for i, arg := range newArgs[:len(newArgs)-1] {
|
||||||
|
switch arg {
|
||||||
|
case "--token", "-t":
|
||||||
|
if strings.HasPrefix(newArgs[i+1], "-") {
|
||||||
|
color.HiYellow("warning: %[1]s %[2]s interpreted as %[1]s=%[2]s, consider using %[1]s=%[2]s syntax when tokens start with a hyphen",
|
||||||
|
newArgs[i], newArgs[i+1],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
newArgs[i] = strings.Join(newArgs[i:i+2], "=")
|
||||||
|
newArgs = append(newArgs[:i+1], newArgs[i+2:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newArgs
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := newApp()
|
app := newApp()
|
||||||
if err := app.Run(os.Args); err != nil {
|
args := ReplaceTokenArg(os.Args)
|
||||||
|
if err := app.Run(args); err != nil {
|
||||||
// Errors will normally be handled by cli.HandleExitCoder via ExitErrHandler set on app. Any error not implementing
|
// Errors will normally be handled by cli.HandleExitCoder via ExitErrHandler set on app. Any error not implementing
|
||||||
// the cli.ExitCoder interface can be handled here.
|
// the cli.ExitCoder interface can be handled here.
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||||
|
@ -148,6 +148,171 @@ func TestApp_HostSpecificErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReplaceTokenArg(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
argsBefore []string
|
||||||
|
argsAfter []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "short token flag second to last arg",
|
||||||
|
argsBefore: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-t",
|
||||||
|
"-therestofmytoken",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-t=-therestofmytoken",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "long token flag second to last arg",
|
||||||
|
argsBefore: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"--token",
|
||||||
|
"-foo_token",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"--token=-foo_token",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "short token flag last arg",
|
||||||
|
argsBefore: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
"-t",
|
||||||
|
"-therestofmytoken",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
"-t=-therestofmytoken",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "long token flag last arg",
|
||||||
|
argsBefore: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
"--token",
|
||||||
|
"-foo_token",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"config",
|
||||||
|
"create",
|
||||||
|
"-n",
|
||||||
|
"targetflux",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
"--token=-foo_token",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no token flag",
|
||||||
|
argsBefore: []string{
|
||||||
|
"v1",
|
||||||
|
"shell",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"v1",
|
||||||
|
"shell",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unprovided token value for flag",
|
||||||
|
argsBefore: []string{
|
||||||
|
"v1",
|
||||||
|
"shell",
|
||||||
|
"--token",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"v1",
|
||||||
|
"shell",
|
||||||
|
"--token",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no token flag, other flags",
|
||||||
|
argsBefore: []string{
|
||||||
|
"v1",
|
||||||
|
"shell",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
},
|
||||||
|
argsAfter: []string{
|
||||||
|
"v1",
|
||||||
|
"shell",
|
||||||
|
"-u",
|
||||||
|
"http://localhost:8087",
|
||||||
|
"-o",
|
||||||
|
"organisation",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no args",
|
||||||
|
argsBefore: []string{},
|
||||||
|
argsAfter: []string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
newArgs := ReplaceTokenArg(tt.argsBefore)
|
||||||
|
require.Equal(t, tt.argsAfter, newArgs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type testWriter struct {
|
type testWriter struct {
|
||||||
b *bytes.Buffer
|
b *bytes.Buffer
|
||||||
written bool
|
written bool
|
||||||
|
Reference in New Issue
Block a user