spiegel_podman/cmd/podman/containers/attach.go
Paul Holzinger b5d1d89a37 Add shell completion with cobra
Allow automatic generation for shell completion scripts
with the internal cobra functions (requires v1.0.0+).

This should replace the handwritten completion scripts
and even adds support for fish. With this approach it is
less likley that completions and code are out of sync.

We can now create the scripts with
- podman completion bash
- podman completion zsh
- podman completion fish

To test the completion run:
source <(podman completion bash)

The same works for podman-remote and podman --remote and
it will complete your remote containers/images with
the correct endpoints values from --url/--connection.

The completion logic is written in go and provided by the
cobra library. The completion functions lives in
`cmd/podman/completion/completion.go`.

The unit test at cmd/podman/shell_completion_test.go checks
if each command and flag has an autocompletion function set.
This prevents that commands and flags have no shell completion set.

This commit does not replace the current autocompletion scripts.

Closes #6440

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2020-11-12 11:38:31 +01:00

90 lines
3.1 KiB
Go

package containers
import (
"os"
"github.com/containers/podman/v2/cmd/podman/common"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/cmd/podman/validate"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
attachDescription = "The podman attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively."
attachCommand = &cobra.Command{
Use: "attach [options] CONTAINER",
Short: "Attach to a running container",
Long: attachDescription,
RunE: attach,
Args: validate.IDOrLatestArgs,
ValidArgsFunction: common.AutocompleteContainersRunning,
Example: `podman attach ctrID
podman attach 1234
podman attach --no-stdin foobar`,
}
containerAttachCommand = &cobra.Command{
Use: attachCommand.Use,
Short: attachCommand.Short,
Long: attachCommand.Long,
RunE: attachCommand.RunE,
Args: validate.IDOrLatestArgs,
ValidArgsFunction: attachCommand.ValidArgsFunction,
Example: `podman container attach ctrID
podman container attach 1234
podman container attach --no-stdin foobar`,
}
)
var (
attachOpts entities.AttachOptions
)
func attachFlags(cmd *cobra.Command) {
flags := cmd.Flags()
detachKeysFlagName := "detach-keys"
flags.StringVar(&attachOpts.DetachKeys, detachKeysFlagName, 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 `_`")
_ = cmd.RegisterFlagCompletionFunc(detachKeysFlagName, common.AutocompleteDetachKeys)
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")
}
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: attachCommand,
})
attachFlags(attachCommand)
validate.AddLatestFlag(attachCommand, &attachOpts.Latest)
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: containerAttachCommand,
Parent: containerCmd,
})
attachFlags(containerAttachCommand)
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]
}
attachOpts.Stdin = os.Stdin
if attachOpts.NoStdin {
attachOpts.Stdin = nil
}
attachOpts.Stdout = os.Stdout
attachOpts.Stderr = os.Stderr
return registry.ContainerEngine().ContainerAttach(registry.GetContext(), name, attachOpts)
}