Skip to content

Commit

Permalink
Refactor parsing to not require --remote to be first
Browse files Browse the repository at this point in the history
Use cobra.Command.FParseErrWhitelist to no longer require --remote to be
the first argument in flags when using CLI

Signed-off-by: Jhon Honce <[email protected]>
  • Loading branch information
jwhonce committed Aug 5, 2020
1 parent d1aaf33 commit 98da2fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 14 additions & 12 deletions cmd/podman/registry/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import (
"sync"

"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var (
// Was --remote given on command line
remoteOverride bool
remoteSync sync.Once
)
// Value for --remote given on command line
var remoteFromCLI = struct {
Value bool
sync sync.Once
}{}

// IsRemote returns true if podman was built to run remote
// IsRemote returns true if podman was built to run remote or --remote flag given on CLI
// Use in init() functions as a initialization check
func IsRemote() bool {
remoteSync.Do(func() {
remote := &cobra.Command{}
remote.Flags().BoolVarP(&remoteOverride, "remote", "r", false, "")
_ = remote.ParseFlags(os.Args)
remoteFromCLI.sync.Do(func() {
fs := pflag.NewFlagSet("remote", pflag.ContinueOnError)
fs.BoolVarP(&remoteFromCLI.Value, "remote", "r", false, "")
fs.ParseErrorsWhitelist.UnknownFlags = true
fs.SetInterspersed(false)
_ = fs.Parse(os.Args[1:])
})
return podmanOptions.EngineMode == entities.TunnelMode || remoteOverride
return podmanOptions.EngineMode == entities.TunnelMode || remoteFromCLI.Value
}
4 changes: 4 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ func WaitContainerReady(p PodmanTestCommon, id string, expStr string, timeout in

// OutputToString formats session output to string
func (s *PodmanSession) OutputToString() string {
if s == nil || s.Out == nil || s.Out.Contents() == nil {
return ""
}

fields := strings.Fields(string(s.Out.Contents()))
return strings.Join(fields, " ")
}
Expand Down

0 comments on commit 98da2fa

Please sign in to comment.