fix: --path flag parsing (#1519)

This commit is contained in:
Luke Kingland 2023-01-24 21:58:38 +09:00 committed by GitHub
parent 154dd138e2
commit 218cc15d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 11 deletions

View File

@ -1,7 +1,6 @@
package cmd
import (
"flag"
"fmt"
"io"
"os"
@ -16,6 +15,7 @@ import (
"github.com/ory/viper"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/term"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/client/pkg/util"
@ -143,19 +143,15 @@ func registry() string {
// definition (prior to parsing).
func effectivePath() (path string) {
var (
env = os.Getenv("FUNC_PATH")
fs = flag.NewFlagSet("", flag.ContinueOnError)
long = fs.String("path", "", "")
short = fs.String("p", "", "")
env = os.Getenv("FUNC_PATH")
fs = pflag.NewFlagSet("", pflag.ContinueOnError)
long = fs.StringP("path", "p", "", "")
)
fs.SetOutput(io.Discard)
_ = fs.Parse(os.Args[1:])
if env != "" {
path = env
}
if *short != "" {
path = *short
}
if *long != "" {
path = *long
}

View File

@ -312,11 +312,11 @@ func TestRoot_effectivePath(t *testing.T) {
}
})
t.Run("--path highest precedence", func(t *testing.T) {
t.Run("-p highest precedence", func(t *testing.T) {
t.Setenv("FUNC_PATH", "p1")
os.Args = []string{"test", "--path=p2", "-p=p3"}
if effectivePath() != "p2" {
t.Fatalf("the effective path did not take --path with highest precedence over -p and FUNC_PATH. Expected 'p2', got '%v'", effectivePath())
if effectivePath() != "p3" {
t.Fatalf("the effective path did not take -p with highest precedence over --path and FUNC_PATH. Expected 'p3', got '%v'", effectivePath())
}
})