Skip to content

Commit

Permalink
fix: --path flag parsing (knative#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkingland authored and lance committed Feb 15, 2023
1 parent 334d77d commit 1618f38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
12 changes: 4 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"flag"
"fmt"
"io"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})

Expand Down

0 comments on commit 1618f38

Please sign in to comment.