diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-03-20 22:22:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-20 22:22:20 +0100 |
commit | 7a095af92a6a39845b1c362222b23632f3e17001 (patch) | |
tree | 52a8692894690a8f09e025f90d6d2b5045259e4f /cmd/podmanV2 | |
parent | 6df1d2043bd3cd2252ea3d737cf542e332106074 (diff) | |
parent | c81e065149da73ae904aa19ee46a23d1ab8ce55c (diff) | |
download | podman-7a095af92a6a39845b1c362222b23632f3e17001.tar.gz podman-7a095af92a6a39845b1c362222b23632f3e17001.tar.bz2 podman-7a095af92a6a39845b1c362222b23632f3e17001.zip |
Merge pull request #5571 from baude/v2exists
podmanv2 container exists|wait
Diffstat (limited to 'cmd/podmanV2')
-rw-r--r-- | cmd/podmanV2/containers/exists.go | 42 | ||||
-rw-r--r-- | cmd/podmanV2/containers/wait.go | 82 | ||||
-rw-r--r-- | cmd/podmanV2/images/inspect.go | 2 | ||||
-rw-r--r-- | cmd/podmanV2/main.go | 13 | ||||
-rw-r--r-- | cmd/podmanV2/registry/registry.go | 14 | ||||
-rw-r--r-- | cmd/podmanV2/root.go | 3 |
6 files changed, 144 insertions, 12 deletions
diff --git a/cmd/podmanV2/containers/exists.go b/cmd/podmanV2/containers/exists.go new file mode 100644 index 000000000..3aff150be --- /dev/null +++ b/cmd/podmanV2/containers/exists.go @@ -0,0 +1,42 @@ +package containers + +import ( + "context" + "os" + + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/spf13/cobra" +) + +var ( + containerExistsDescription = `If the named container exists in local storage, podman container exists exits with 0, otherwise the exit code will be 1.` + + existsCommand = &cobra.Command{ + Use: "exists CONTAINER", + Short: "Check if a container exists in local storage", + Long: containerExistsDescription, + Example: `podman container exists containerID + podman container exists myctr || podman run --name myctr [etc...]`, + RunE: exists, + } +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: existsCommand, + Parent: containerCmd, + }) +} + +func exists(cmd *cobra.Command, args []string) error { + response, err := registry.ContainerEngine().ContainerExists(context.Background(), args[0]) + if err != nil { + return err + } + if !response.Value { + os.Exit(1) + } + return nil +} diff --git a/cmd/podmanV2/containers/wait.go b/cmd/podmanV2/containers/wait.go new file mode 100644 index 000000000..27acb3348 --- /dev/null +++ b/cmd/podmanV2/containers/wait.go @@ -0,0 +1,82 @@ +package containers + +import ( + "context" + "fmt" + "time" + + "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + waitDescription = `Block until one or more containers stop and then print their exit codes. +` + waitCommand = &cobra.Command{ + Use: "wait [flags] CONTAINER [CONTAINER...]", + Short: "Block on one or more containers", + Long: waitDescription, + RunE: wait, + Example: `podman wait --latest + podman wait --interval 5000 ctrID + podman wait ctrID1 ctrID2`, + } +) + +var ( + waitFlags = entities.WaitOptions{} + waitCondition string +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: waitCommand, + Parent: containerCmd, + }) + + flags := waitCommand.Flags() + flags.DurationVarP(&waitFlags.Interval, "interval", "i", time.Duration(250), "Milliseconds to wait before polling for completion") + flags.BoolVarP(&waitFlags.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 { + // TODO: This is the same as V1. We could skip creating the flag altogether in V2... + _ = flags.MarkHidden("latest") + } +} + +func wait(cmd *cobra.Command, args []string) error { + var ( + err error + ) + if waitFlags.Latest && len(args) > 0 { + return errors.New("cannot combine latest flag and arguments") + } + if waitFlags.Interval == 0 { + return errors.New("interval must be greater then 0") + } + + waitFlags.Condition, err = define.StringToContainerStatus(waitCondition) + if err != nil { + return err + } + + responses, err := registry.ContainerEngine().ContainerWait(context.Background(), args, waitFlags) + if err != nil { + return err + } + for _, r := range responses { + if r.Error == nil { + fmt.Println(r.Id) + } + } + for _, r := range responses { + if r.Error != nil { + fmt.Println(err) + } + } + return nil +} diff --git a/cmd/podmanV2/images/inspect.go b/cmd/podmanV2/images/inspect.go index 9c44cea35..2ecbbb201 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.GlobalFlags.EngineMode == entities.ABIMode { + if registry.EngineOpts.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/main.go b/cmd/podmanV2/main.go index 0df086352..24f21d804 100644 --- a/cmd/podmanV2/main.go +++ b/cmd/podmanV2/main.go @@ -5,6 +5,7 @@ import ( "os" "reflect" "runtime" + "strings" _ "github.com/containers/libpod/cmd/podmanV2/containers" _ "github.com/containers/libpod/cmd/podmanV2/images" @@ -31,17 +32,19 @@ func initCobra() { case "darwin": fallthrough case "windows": - registry.GlobalFlags.EngineMode = entities.TunnelMode + registry.EngineOpts.EngineMode = entities.TunnelMode case "linux": - registry.GlobalFlags.EngineMode = entities.ABIMode + registry.EngineOpts.EngineMode = entities.ABIMode default: logrus.Errorf("%s is not a supported OS", runtime.GOOS) os.Exit(1) } // TODO: Is there a Cobra way to "peek" at os.Args? - if ok := Contains("--remote", os.Args); ok { - registry.GlobalFlags.EngineMode = entities.TunnelMode + for _, v := range os.Args { + if strings.HasPrefix(v, "--remote") { + registry.EngineOpts.EngineMode = entities.TunnelMode + } } cobra.OnInitialize(func() {}) @@ -50,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.GlobalFlags.EngineMode, c.Mode) { + if Contains(registry.EngineOpts.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 fa51d6535..793d520a8 100644 --- a/cmd/podmanV2/registry/registry.go +++ b/cmd/podmanV2/registry/registry.go @@ -14,11 +14,13 @@ type CliCommand struct { } var ( - Commands []CliCommand - GlobalFlags entities.EngineFlags + Commands []CliCommand + imageEngine entities.ImageEngine containerEngine entities.ContainerEngine - PodmanTunnel bool + + EngineOpts entities.EngineOptions + GlobalFlags entities.EngineFlags ) // HelpTemplate returns the help template for podman commands @@ -63,7 +65,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 { - engine, err := infra.NewImageEngine(GlobalFlags.EngineMode, entities.EngineOptions{}) + EngineOpts.FlagSet = cmd.Flags() + engine, err := infra.NewImageEngine(EngineOpts) if err != nil { return nil, err } @@ -79,7 +82,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 { - engine, err := infra.NewContainerEngine(GlobalFlags.EngineMode, entities.EngineOptions{}) + EngineOpts.FlagSet = cmd.Flags() + engine, err := infra.NewContainerEngine(EngineOpts) if err != nil { return nil, err } diff --git a/cmd/podmanV2/root.go b/cmd/podmanV2/root.go index 778184f28..24b083b9f 100644 --- a/cmd/podmanV2/root.go +++ b/cmd/podmanV2/root.go @@ -24,7 +24,8 @@ func init() { // Override default --help information of `--version` global flag} var dummyVersion bool rootCmd.PersistentFlags().BoolVarP(&dummyVersion, "version", "v", false, "Version of podman") - rootCmd.PersistentFlags().BoolVarP(®istry.PodmanTunnel, "remote", "r", false, "Access service via SSH tunnel") + 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") } func Execute() { |