Skip to content

Commit

Permalink
Fixes --remote flag issues
Browse files Browse the repository at this point in the history
* --remote, --url and --identity are now anchored to podman command.
  Subcommands should no longer have issues
* TraverseChildren now set to V1 expectations
* Latest flag now has helper function. Now has consistent usage.
* IsRemote() uses cobra parser to determin if --remote is given
* Moved validation functions from parser pkg to validate pkg
*

Fixes containers#6598
Fixes containers#6704

Signed-off-by: Jhon Honce <jhonce@redhat.com>

<MH: Fixed import issues>

Signed-off-by: Matt Heon <matthew.heon@pm.me>
  • Loading branch information
jwhonce authored and mheon committed Jul 6, 2020
1 parent de6a860 commit 8dd2628
Show file tree
Hide file tree
Showing 61 changed files with 405 additions and 460 deletions.
14 changes: 6 additions & 8 deletions cmd/podman/containers/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,31 @@ func attachFlags(flags *pflag.FlagSet) {
flags.StringVar(&attachOpts.DetachKeys, "detach-keys", containerConfig.DetachKeys(), "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
flags.BoolVar(&attachOpts.NoStdin, "no-stdin", false, "Do not attach STDIN. The default is false")
flags.BoolVar(&attachOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process")
flags.BoolVarP(&attachOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
if registry.IsRemote() {
_ = flags.MarkHidden("latest")
}
}

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: attachCommand,
})
flags := attachCommand.Flags()
attachFlags(flags)
attachFlags(attachCommand.Flags())
validate.AddLatestFlag(attachCommand, &attachOpts.Latest)

registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: containerAttachCommand,
Parent: containerCmd,
})
containerAttachFlags := containerAttachCommand.Flags()
attachFlags(containerAttachFlags)
attachFlags(containerAttachCommand.Flags())
validate.AddLatestFlag(containerAttachCommand, &attachOpts.Latest)

}

func attach(cmd *cobra.Command, args []string) error {
if len(args) > 1 || (len(args) == 0 && !attachOpts.Latest) {
return errors.Errorf("attach requires the name or id of one running container or the latest flag")
}

var name string
if len(args) > 0 {
name = args[0]
Expand Down
9 changes: 3 additions & 6 deletions cmd/podman/containers/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/containers/libpod/v2/cmd/podman/parse"
"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/utils"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/containers/libpod/v2/pkg/rootless"
"github.com/pkg/errors"
Expand All @@ -25,7 +25,7 @@ var (
Long: checkpointDescription,
RunE: checkpoint,
Args: func(cmd *cobra.Command, args []string) error {
return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false)
},
Example: `podman container checkpoint --keep ctrID
podman container checkpoint --all
Expand All @@ -48,12 +48,9 @@ func init() {
flags.BoolVarP(&checkpointOptions.LeaveRunning, "leave-running", "R", false, "Leave the container running after writing checkpoint to disk")
flags.BoolVar(&checkpointOptions.TCPEstablished, "tcp-established", false, "Checkpoint a container with established TCP connections")
flags.BoolVarP(&checkpointOptions.All, "all", "a", false, "Checkpoint all running containers")
flags.BoolVarP(&checkpointOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.StringVarP(&checkpointOptions.Export, "export", "e", "", "Export the checkpoint image to a tar.gz")
flags.BoolVar(&checkpointOptions.IgnoreRootFS, "ignore-rootfs", false, "Do not include root file-system changes when exporting")
if registry.IsRemote() {
_ = flags.MarkHidden("latest")
}
validate.AddLatestFlag(checkpointCommand, &checkpointOptions.Latest)
}

func checkpoint(cmd *cobra.Command, args []string) error {
Expand Down
7 changes: 3 additions & 4 deletions cmd/podman/containers/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package containers
import (
"fmt"

"github.com/containers/libpod/v2/cmd/podman/parse"
"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/utils"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -24,7 +24,7 @@ var (
Long: cleanupDescription,
RunE: cleanup,
Args: func(cmd *cobra.Command, args []string) error {
return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false)
},
Example: `podman container cleanup --latest
podman container cleanup ctrID1 ctrID2 ctrID3
Expand All @@ -44,11 +44,10 @@ func init() {
})
flags := cleanupCommand.Flags()
flags.BoolVarP(&cleanupOptions.All, "all", "a", false, "Cleans up all containers")
flags.BoolVarP(&cleanupOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.StringVar(&cleanupOptions.Exec, "exec", "", "Clean up the given exec session instead of the container")
flags.BoolVar(&cleanupOptions.Remove, "rm", false, "After cleanup, remove the container entirely")
flags.BoolVar(&cleanupOptions.RemoveImage, "rmi", false, "After cleanup, remove the image entirely")

validate.AddLatestFlag(cleanupCommand, &cleanupOptions.Latest)
}

func cleanup(cmd *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func createFlags(flags *pflag.FlagSet) {
flags.AddFlagSet(common.GetCreateFlags(&cliVals))
flags.AddFlagSet(common.GetNetFlags())
flags.SetNormalizeFunc(common.AliasFlags)

if registry.IsRemote() {
_ = flags.MarkHidden("authfile")
_ = flags.MarkHidden("env-host")
Expand Down
9 changes: 3 additions & 6 deletions cmd/podman/containers/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var (
diffCmd = &cobra.Command{
Use: "diff [flags] CONTAINER",
Args: validate.IDOrLatestArgs,
Short: "Inspect changes on container's file systems",
Long: `Displays changes on a container filesystem. The container will be compared to its parent layer.`,
Short: "Inspect changes to the container's file systems",
Long: `Displays changes to the container filesystem's'. The container will be compared to its parent layer.`,
RunE: diff,
Example: `podman container diff myCtr
podman container diff -l --format json myCtr`,
Expand All @@ -35,10 +35,7 @@ func init() {
flags.BoolVar(&diffOpts.Archive, "archive", true, "Save the diff as a tar archive")
_ = flags.MarkHidden("archive")
flags.StringVar(&diffOpts.Format, "format", "", "Change the output format")

if !registry.IsRemote() {
flags.BoolVarP(&diffOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
}
validate.AddLatestFlag(diffCmd, &diffOpts.Latest)
}

func diff(cmd *cobra.Command, args []string) error {
Expand Down
15 changes: 7 additions & 8 deletions cmd/podman/containers/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/libpod/define"
"github.com/containers/libpod/v2/pkg/domain/entities"
envLib "github.com/containers/libpod/v2/pkg/env"
Expand Down Expand Up @@ -53,14 +54,13 @@ func execFlags(flags *pflag.FlagSet) {
flags.StringArrayVarP(&envInput, "env", "e", []string{}, "Set environment variables")
flags.StringSliceVar(&envFile, "env-file", []string{}, "Read in a file of environment variables")
flags.BoolVarP(&execOpts.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
flags.BoolVarP(&execOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.BoolVar(&execOpts.Privileged, "privileged", false, "Give the process extended Linux capabilities inside the container. The default is false")
flags.BoolVarP(&execOpts.Tty, "tty", "t", false, "Allocate a pseudo-TTY. The default is false")
flags.StringVarP(&execOpts.User, "user", "u", "", "Sets the username or UID used and optionally the groupname or GID for the specified command")
flags.UintVar(&execOpts.PreserveFDs, "preserve-fds", 0, "Pass N additional file descriptors to the container")
flags.StringVarP(&execOpts.WorkDir, "workdir", "w", "", "Working directory inside the container")

if registry.IsRemote() {
_ = flags.MarkHidden("latest")
_ = flags.MarkHidden("preserve-fds")
}
}
Expand All @@ -70,20 +70,19 @@ func init() {
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: execCommand,
})
flags := execCommand.Flags()
execFlags(flags)
execFlags(execCommand.Flags())
validate.AddLatestFlag(execCommand, &execOpts.Latest)

registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: containerExecCommand,
Parent: containerCmd,
})

containerExecFlags := containerExecCommand.Flags()
execFlags(containerExecFlags)
execFlags(containerExecCommand.Flags())
validate.AddLatestFlag(containerExecCommand, &execOpts.Latest)
}

func exec(cmd *cobra.Command, args []string) error {
func exec(_ *cobra.Command, args []string) error {
var nameOrID string

if len(args) == 0 && !execOpts.Latest {
Expand Down
11 changes: 4 additions & 7 deletions cmd/podman/containers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package containers
import (
"fmt"

"github.com/containers/libpod/v2/cmd/podman/parse"
"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/utils"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -20,7 +20,7 @@ var (
Long: initDescription,
RunE: initContainer,
Args: func(cmd *cobra.Command, args []string) error {
return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false)
},
Example: `podman init --latest
podman init 3c45ef19d893
Expand All @@ -45,10 +45,6 @@ var (

func initFlags(flags *pflag.FlagSet) {
flags.BoolVarP(&initOptions.All, "all", "a", false, "Initialize all containers")
flags.BoolVarP(&initOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
if registry.IsRemote() {
_ = flags.MarkHidden("latest")
}
}

func init() {
Expand All @@ -58,15 +54,16 @@ func init() {
})
flags := initCommand.Flags()
initFlags(flags)
validate.AddLatestFlag(initCommand, &initOptions.Latest)

registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Parent: containerCmd,
Command: containerInitCommand,
})

containerInitFlags := containerInitCommand.Flags()
initFlags(containerInitFlags)
validate.AddLatestFlag(containerInitCommand, &initOptions.Latest)
}

func initContainer(cmd *cobra.Command, args []string) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/containers/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"github.com/containers/libpod/v2/cmd/podman/inspect"
"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -30,7 +31,7 @@ func init() {
flags := inspectCmd.Flags()
flags.BoolVarP(&inspectOpts.Size, "size", "s", false, "Display total file size")
flags.StringVarP(&inspectOpts.Format, "format", "f", "json", "Format the output to a Go template or json")
flags.BoolVarP(&inspectOpts.Latest, "latest", "l", false, "Act on the latest container Podman is aware of")
validate.AddLatestFlag(inspectCmd, &inspectOpts.Latest)
}

func inspectExec(cmd *cobra.Command, args []string) error {
Expand Down
21 changes: 8 additions & 13 deletions cmd/podman/containers/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"errors"
"fmt"

"github.com/containers/libpod/v2/cmd/podman/parse"
"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/utils"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/containers/libpod/v2/pkg/signal"
"github.com/spf13/cobra"
Expand All @@ -22,7 +22,7 @@ var (
Long: killDescription,
RunE: kill,
Args: func(cmd *cobra.Command, args []string) error {
return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false)
},
Example: `podman kill mywebserver
podman kill 860a4b23
Expand All @@ -31,7 +31,7 @@ var (

containerKillCommand = &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false)
},
Use: killCommand.Use,
Short: killCommand.Short,
Expand All @@ -50,31 +50,26 @@ var (
func killFlags(flags *pflag.FlagSet) {
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 registry.IsRemote() {
_ = flags.MarkHidden("latest")
}
}

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: killCommand,
})
flags := killCommand.Flags()
killFlags(flags)
killFlags(killCommand.Flags())
validate.AddLatestFlag(killCommand, &killOptions.Latest)

registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: containerKillCommand,
Parent: containerCmd,
})

containerKillFlags := containerKillCommand.Flags()
killFlags(containerKillFlags)
killFlags(containerKillCommand.Flags())
validate.AddLatestFlag(containerKillCommand, &killOptions.Latest)
}

func kill(cmd *cobra.Command, args []string) error {
func kill(_ *cobra.Command, args []string) error {
var (
err error
errs utils.OutputErrors
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/containers/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ func init() {
Parent: containerCmd,
})
listFlagSet(listCmd.Flags())
validate.AddLatestFlag(listCmd, &listOpts.Latest)
}
14 changes: 6 additions & 8 deletions cmd/podman/containers/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/containers/libpod/v2/pkg/util"
"github.com/pkg/errors"
Expand Down Expand Up @@ -68,25 +69,22 @@ func init() {
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: logsCommand,
})

flags := logsCommand.Flags()
logsFlags(flags)
logsFlags(logsCommand.Flags())
validate.AddLatestFlag(logsCommand, &logsOptions.Latest)

// container logs
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: containerLogsCommand,
Parent: containerCmd,
})

containerLogsFlags := containerLogsCommand.Flags()
logsFlags(containerLogsFlags)
logsFlags(containerLogsCommand.Flags())
validate.AddLatestFlag(containerLogsCommand, &logsOptions.Latest)
}

func logsFlags(flags *pflag.FlagSet) {
flags.BoolVar(&logsOptions.Details, "details", false, "Show extra details provided to the logs")
flags.BoolVarP(&logsOptions.Follow, "follow", "f", false, "Follow log output. The default is false")
flags.BoolVarP(&logsOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.StringVar(&logsOptions.SinceRaw, "since", "", "Show logs since TIMESTAMP")
flags.Int64Var(&logsOptions.Tail, "tail", -1, "Output the specified number of LINES at the end of the logs. Defaults to -1, which prints all lines")
flags.BoolVarP(&logsOptions.Timestamps, "timestamps", "t", false, "Output the timestamps in the log")
Expand All @@ -95,7 +93,7 @@ func logsFlags(flags *pflag.FlagSet) {
_ = flags.MarkHidden("details")
}

func logs(cmd *cobra.Command, args []string) error {
func logs(_ *cobra.Command, args []string) error {
if logsOptions.SinceRaw != "" {
// parse time, error out if something is wrong
since, err := util.ParseInputTime(logsOptions.SinceRaw)
Expand Down
Loading

0 comments on commit 8dd2628

Please sign in to comment.