diff options
author | Jhon Honce <jhonce@redhat.com> | 2020-03-23 10:27:32 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2020-03-24 11:03:13 -0700 |
commit | 77a2af8817284d134ea1f30f0b06291a5b05c5a0 (patch) | |
tree | f6ed82f9c435c30b44d5c820502607ba60e89282 /cmd/podmanV2 | |
parent | c29a4c69044c02eee564fc72c64144b2a76d1d73 (diff) | |
download | podman-77a2af8817284d134ea1f30f0b06291a5b05c5a0.tar.gz podman-77a2af8817284d134ea1f30f0b06291a5b05c5a0.tar.bz2 podman-77a2af8817284d134ea1f30f0b06291a5b05c5a0.zip |
Combine GlobalFlags and EngineFlags into EngineOptions
* EngineOptions obtained in command via
`opt, err := registry.Options(cmd)`
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd/podmanV2')
-rw-r--r-- | cmd/podmanV2/containers/kill.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/pause.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/restart.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/rm.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/stop.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/unpause.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/containers/wait.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/images/inspect.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/images/list.go | 1 | ||||
-rw-r--r-- | cmd/podmanV2/main.go | 8 | ||||
-rw-r--r-- | cmd/podmanV2/registry/registry.go | 28 | ||||
-rw-r--r-- | cmd/podmanV2/registry/remote.go | 9 | ||||
-rw-r--r-- | cmd/podmanV2/root.go | 7 | ||||
-rw-r--r-- | cmd/podmanV2/utils/remote.go | 10 |
14 files changed, 48 insertions, 31 deletions
diff --git a/cmd/podmanV2/containers/kill.go b/cmd/podmanV2/containers/kill.go index b02dcf9da..36c4f2528 100644 --- a/cmd/podmanV2/containers/kill.go +++ b/cmd/podmanV2/containers/kill.go @@ -41,7 +41,7 @@ func init() { flags.BoolVarP(&killOptions.All, "all", "a", false, "Signal all running containers") flags.StringVarP(&killOptions.Signal, "signal", "s", "KILL", "Signal to send to the container") flags.BoolVarP(&killOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") - if utils.IsRemote() { + if registry.IsRemote() { _ = flags.MarkHidden("latest") } } diff --git a/cmd/podmanV2/containers/pause.go b/cmd/podmanV2/containers/pause.go index 3f3e7c38d..99ca34d5c 100644 --- a/cmd/podmanV2/containers/pause.go +++ b/cmd/podmanV2/containers/pause.go @@ -42,7 +42,7 @@ func pause(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) - if rootless.IsRootless() && !utils.IsRemote() { + if rootless.IsRootless() && !registry.IsRemote() { return errors.New("pause is not supported for rootless containers") } if len(args) < 1 && !pauseOpts.All { diff --git a/cmd/podmanV2/containers/restart.go b/cmd/podmanV2/containers/restart.go index ee9c34361..216824389 100644 --- a/cmd/podmanV2/containers/restart.go +++ b/cmd/podmanV2/containers/restart.go @@ -47,7 +47,7 @@ func init() { flags.BoolVar(&restartOptions.Running, "running", false, "Restart only running containers when --all is used") flags.UintVarP(&restartTimeout, "timeout", "t", define.CtrRemoveTimeout, "Seconds to wait for stop before killing the container") flags.UintVar(&restartTimeout, "time", define.CtrRemoveTimeout, "Seconds to wait for stop before killing the container") - if utils.IsRemote() { + if registry.IsRemote() { _ = flags.MarkHidden("latest") } } diff --git a/cmd/podmanV2/containers/rm.go b/cmd/podmanV2/containers/rm.go index af7a38fb1..916dd2577 100644 --- a/cmd/podmanV2/containers/rm.go +++ b/cmd/podmanV2/containers/rm.go @@ -50,7 +50,7 @@ func init() { flags.BoolVar(&rmOptions.Storage, "storage", false, "Remove container from storage library") flags.BoolVarP(&rmOptions.Volumes, "volumes", "v", false, "Remove anonymous volumes associated with the container") flags.StringArrayVarP(&rmOptions.CIDFiles, "cidfile", "", nil, "Read the container ID from the file") - if utils.IsRemote() { + if registry.IsRemote() { _ = flags.MarkHidden("latest") _ = flags.MarkHidden("ignore") _ = flags.MarkHidden("cidfile") diff --git a/cmd/podmanV2/containers/stop.go b/cmd/podmanV2/containers/stop.go index 066202298..b6d70784d 100644 --- a/cmd/podmanV2/containers/stop.go +++ b/cmd/podmanV2/containers/stop.go @@ -48,7 +48,7 @@ func init() { flags.BoolVarP(&stopOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") flags.UintVar(&stopTimeout, "time", define.CtrRemoveTimeout, "Seconds to wait for stop before killing the container") flags.UintVarP(&stopTimeout, "timeout", "t", define.CtrRemoveTimeout, "Seconds to wait for stop before killing the container") - if registry.EngineOpts.EngineMode == entities.ABIMode { + if registry.EngineOptions.EngineMode == entities.ABIMode { _ = flags.MarkHidden("latest") _ = flags.MarkHidden("cidfile") _ = flags.MarkHidden("ignore") diff --git a/cmd/podmanV2/containers/unpause.go b/cmd/podmanV2/containers/unpause.go index 697132a53..8b8cde9b2 100644 --- a/cmd/podmanV2/containers/unpause.go +++ b/cmd/podmanV2/containers/unpause.go @@ -39,7 +39,7 @@ func unpause(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) - if rootless.IsRootless() && !utils.IsRemote() { + if rootless.IsRootless() && !registry.IsRemote() { return errors.New("unpause is not supported for rootless containers") } if len(args) < 1 && !unPauseOptions.All { diff --git a/cmd/podmanV2/containers/wait.go b/cmd/podmanV2/containers/wait.go index cded8e571..4367fd9d9 100644 --- a/cmd/podmanV2/containers/wait.go +++ b/cmd/podmanV2/containers/wait.go @@ -46,7 +46,7 @@ func init() { flags.DurationVarP(&waitOptions.Interval, "interval", "i", time.Duration(250), "Milliseconds to wait before polling for completion") flags.BoolVarP(&waitOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") flags.StringVar(&waitCondition, "condition", "stopped", "Condition to wait on") - if registry.EngineOpts.EngineMode == entities.ABIMode { + if registry.EngineOptions.EngineMode == entities.ABIMode { // TODO: This is the same as V1. We could skip creating the flag altogether in V2... _ = flags.MarkHidden("latest") } diff --git a/cmd/podmanV2/images/inspect.go b/cmd/podmanV2/images/inspect.go index 2ecbbb201..f8fd44571 100644 --- a/cmd/podmanV2/images/inspect.go +++ b/cmd/podmanV2/images/inspect.go @@ -52,7 +52,7 @@ func init() { flags.BoolVarP(&inspectOpts.Size, "size", "s", false, "Display total file size") flags.StringVarP(&inspectOpts.Format, "format", "f", "", "Change the output format to a Go template") - if registry.EngineOpts.EngineMode == entities.ABIMode { + if registry.EngineOptions.EngineMode == entities.ABIMode { // TODO: This is the same as V1. We could skip creating the flag altogether in V2... _ = flags.MarkHidden("latest") } diff --git a/cmd/podmanV2/images/list.go b/cmd/podmanV2/images/list.go index cfdfaaed2..0441f8fd8 100644 --- a/cmd/podmanV2/images/list.go +++ b/cmd/podmanV2/images/list.go @@ -29,5 +29,6 @@ func init() { } func images(cmd *cobra.Command, args []string) error { + _, _ = registry.Options(cmd) return nil } diff --git a/cmd/podmanV2/main.go b/cmd/podmanV2/main.go index 24f21d804..dc96c26d0 100644 --- a/cmd/podmanV2/main.go +++ b/cmd/podmanV2/main.go @@ -32,9 +32,9 @@ func initCobra() { case "darwin": fallthrough case "windows": - registry.EngineOpts.EngineMode = entities.TunnelMode + registry.EngineOptions.EngineMode = entities.TunnelMode case "linux": - registry.EngineOpts.EngineMode = entities.ABIMode + registry.EngineOptions.EngineMode = entities.ABIMode default: logrus.Errorf("%s is not a supported OS", runtime.GOOS) os.Exit(1) @@ -43,7 +43,7 @@ func initCobra() { // TODO: Is there a Cobra way to "peek" at os.Args? for _, v := range os.Args { if strings.HasPrefix(v, "--remote") { - registry.EngineOpts.EngineMode = entities.TunnelMode + registry.EngineOptions.EngineMode = entities.TunnelMode } } @@ -53,7 +53,7 @@ func initCobra() { func main() { fmt.Fprintf(os.Stderr, "Number of commands: %d\n", len(registry.Commands)) for _, c := range registry.Commands { - if Contains(registry.EngineOpts.EngineMode, c.Mode) { + if Contains(registry.EngineOptions.EngineMode, c.Mode) { parent := rootCmd if c.Parent != nil { parent = c.Parent diff --git a/cmd/podmanV2/registry/registry.go b/cmd/podmanV2/registry/registry.go index b0c11ba04..9c12b2456 100644 --- a/cmd/podmanV2/registry/registry.go +++ b/cmd/podmanV2/registry/registry.go @@ -1,6 +1,8 @@ package registry import ( + "context" + "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/infra" @@ -20,8 +22,7 @@ var ( imageEngine entities.ImageEngine containerEngine entities.ContainerEngine - EngineOpts entities.EngineOptions - GlobalFlags entities.EngineFlags + EngineOptions entities.EngineOptions ExitCode = define.ExecErrorCodeGeneric ) @@ -76,8 +77,8 @@ func ImageEngine() entities.ImageEngine { // NewImageEngine is a wrapper for building an ImageEngine to be used for PreRunE functions func NewImageEngine(cmd *cobra.Command, args []string) (entities.ImageEngine, error) { if imageEngine == nil { - EngineOpts.FlagSet = cmd.Flags() - engine, err := infra.NewImageEngine(EngineOpts) + EngineOptions.FlagSet = cmd.Flags() + engine, err := infra.NewImageEngine(EngineOptions) if err != nil { return nil, err } @@ -93,8 +94,8 @@ func ContainerEngine() entities.ContainerEngine { // NewContainerEngine is a wrapper for building an ContainerEngine to be used for PreRunE functions func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEngine, error) { if containerEngine == nil { - EngineOpts.FlagSet = cmd.Flags() - engine, err := infra.NewContainerEngine(EngineOpts) + EngineOptions.FlagSet = cmd.Flags() + engine, err := infra.NewContainerEngine(EngineOptions) if err != nil { return nil, err } @@ -109,3 +110,18 @@ func SubCommandExists(cmd *cobra.Command, args []string) error { } return errors.Errorf("missing command '%[1]s COMMAND'\nTry '%[1]s --help' for more information.", cmd.CommandPath()) } + +type podmanContextKey string + +var podmanFactsKey = podmanContextKey("engineOptions") + +func NewOptions(ctx context.Context, facts *entities.EngineOptions) context.Context { + return context.WithValue(ctx, podmanFactsKey, facts) +} + +func Options(cmd *cobra.Command) (*entities.EngineOptions, error) { + if f, ok := cmd.Context().Value(podmanFactsKey).(*entities.EngineOptions); ok { + return f, errors.New("Command Context ") + } + return nil, nil +} diff --git a/cmd/podmanV2/registry/remote.go b/cmd/podmanV2/registry/remote.go new file mode 100644 index 000000000..32a231ac4 --- /dev/null +++ b/cmd/podmanV2/registry/remote.go @@ -0,0 +1,9 @@ +package registry + +import ( + "github.com/containers/libpod/pkg/domain/entities" +) + +func IsRemote() bool { + return EngineOptions.EngineMode == entities.TunnelMode +} diff --git a/cmd/podmanV2/root.go b/cmd/podmanV2/root.go index 2becd126d..68e8b4531 100644 --- a/cmd/podmanV2/root.go +++ b/cmd/podmanV2/root.go @@ -26,12 +26,13 @@ func init() { var dummyVersion bool // TODO had to disable shorthand -v for version due to -v rm with volume rootCmd.PersistentFlags().BoolVar(&dummyVersion, "version", false, "Version of Podman") - rootCmd.PersistentFlags().StringVarP(®istry.EngineOpts.Uri, "remote", "r", "", "URL to access Podman service") - rootCmd.PersistentFlags().StringSliceVar(®istry.EngineOpts.Identities, "identity", []string{}, "path to SSH identity file") + rootCmd.PersistentFlags().StringVarP(®istry.EngineOptions.Uri, "remote", "r", "", "URL to access Podman service") + rootCmd.PersistentFlags().StringSliceVar(®istry.EngineOptions.Identities, "identity", []string{}, "path to SSH identity file") } func Execute() { - if err := rootCmd.Execute(); err != nil { + o := registry.NewOptions(rootCmd.Context(), ®istry.EngineOptions) + if err := rootCmd.ExecuteContext(o); err != nil { fmt.Fprintln(os.Stderr, "Error:", err.Error()) } else if registry.GetExitCode() == define.ExecErrorCodeGeneric { // The exitCode modified from define.ExecErrorCodeGeneric, diff --git a/cmd/podmanV2/utils/remote.go b/cmd/podmanV2/utils/remote.go deleted file mode 100644 index d0c8a272d..000000000 --- a/cmd/podmanV2/utils/remote.go +++ /dev/null @@ -1,10 +0,0 @@ -package utils - -import ( - "github.com/containers/libpod/cmd/podmanV2/registry" - "github.com/containers/libpod/pkg/domain/entities" -) - -func IsRemote() bool { - return registry.EngineOpts.EngineMode == entities.TunnelMode -} |