mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-10-06 14:07:50 +00:00
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>
112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package containers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/containers/common/pkg/completion"
|
|
"github.com/containers/podman/v2/cmd/podman/common"
|
|
"github.com/containers/podman/v2/cmd/podman/registry"
|
|
"github.com/containers/podman/v2/cmd/podman/utils"
|
|
"github.com/containers/podman/v2/cmd/podman/validate"
|
|
"github.com/containers/podman/v2/pkg/domain/entities"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
stopDescription = fmt.Sprintf(`Stops one or more running containers. The container name or ID can be used.
|
|
|
|
A timeout to forcibly stop the container can also be set but defaults to %d seconds otherwise.`, containerConfig.Engine.StopTimeout)
|
|
stopCommand = &cobra.Command{
|
|
Use: "stop [options] CONTAINER [CONTAINER...]",
|
|
Short: "Stop one or more containers",
|
|
Long: stopDescription,
|
|
RunE: stop,
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true)
|
|
},
|
|
ValidArgsFunction: common.AutocompleteContainersRunning,
|
|
Example: `podman stop ctrID
|
|
podman stop --latest
|
|
podman stop --time 2 mywebserver 6e534f14da9d`,
|
|
}
|
|
|
|
containerStopCommand = &cobra.Command{
|
|
Use: stopCommand.Use,
|
|
Short: stopCommand.Short,
|
|
Long: stopCommand.Long,
|
|
RunE: stopCommand.RunE,
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true)
|
|
},
|
|
ValidArgsFunction: stopCommand.ValidArgsFunction,
|
|
Example: `podman container stop ctrID
|
|
podman container stop --latest
|
|
podman container stop --time 2 mywebserver 6e534f14da9d`,
|
|
}
|
|
)
|
|
|
|
var (
|
|
stopOptions = entities.StopOptions{}
|
|
stopTimeout uint
|
|
)
|
|
|
|
func stopFlags(cmd *cobra.Command) {
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&stopOptions.All, "all", "a", false, "Stop all running containers")
|
|
flags.BoolVarP(&stopOptions.Ignore, "ignore", "i", false, "Ignore errors when a specified container is missing")
|
|
|
|
cidfileFlagName := "cidfile"
|
|
flags.StringArrayVarP(&stopOptions.CIDFiles, cidfileFlagName, "", nil, "Read the container ID from the file")
|
|
_ = cmd.RegisterFlagCompletionFunc(cidfileFlagName, completion.AutocompleteDefault)
|
|
|
|
timeFlagName := "time"
|
|
flags.UintVarP(&stopTimeout, timeFlagName, "t", containerConfig.Engine.StopTimeout, "Seconds to wait for stop before killing the container")
|
|
_ = cmd.RegisterFlagCompletionFunc(timeFlagName, completion.AutocompleteNone)
|
|
|
|
if registry.IsRemote() {
|
|
_ = flags.MarkHidden("cidfile")
|
|
_ = flags.MarkHidden("ignore")
|
|
}
|
|
flags.SetNormalizeFunc(utils.AliasFlags)
|
|
}
|
|
|
|
func init() {
|
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
|
Command: stopCommand,
|
|
})
|
|
stopFlags(stopCommand)
|
|
validate.AddLatestFlag(stopCommand, &stopOptions.Latest)
|
|
|
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
|
Command: containerStopCommand,
|
|
Parent: containerCmd,
|
|
})
|
|
stopFlags(containerStopCommand)
|
|
validate.AddLatestFlag(containerStopCommand, &stopOptions.Latest)
|
|
}
|
|
|
|
func stop(cmd *cobra.Command, args []string) error {
|
|
var (
|
|
errs utils.OutputErrors
|
|
)
|
|
if cmd.Flag("time").Changed {
|
|
stopOptions.Timeout = &stopTimeout
|
|
}
|
|
|
|
responses, err := registry.ContainerEngine().ContainerStop(context.Background(), args, stopOptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, r := range responses {
|
|
if r.Err == nil {
|
|
fmt.Println(r.Id)
|
|
} else {
|
|
errs = append(errs, r.Err)
|
|
}
|
|
}
|
|
return errs.PrintErrors()
|
|
}
|