mirror of
https://github.com/containers/podman.git
synced 2025-05-20 08:36:23 +08: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>
88 lines
2.7 KiB
Go
88 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
_ "github.com/containers/podman/v2/cmd/podman/completion"
|
|
_ "github.com/containers/podman/v2/cmd/podman/containers"
|
|
_ "github.com/containers/podman/v2/cmd/podman/generate"
|
|
_ "github.com/containers/podman/v2/cmd/podman/healthcheck"
|
|
_ "github.com/containers/podman/v2/cmd/podman/images"
|
|
_ "github.com/containers/podman/v2/cmd/podman/manifest"
|
|
_ "github.com/containers/podman/v2/cmd/podman/networks"
|
|
_ "github.com/containers/podman/v2/cmd/podman/play"
|
|
_ "github.com/containers/podman/v2/cmd/podman/pods"
|
|
"github.com/containers/podman/v2/cmd/podman/registry"
|
|
_ "github.com/containers/podman/v2/cmd/podman/system"
|
|
_ "github.com/containers/podman/v2/cmd/podman/system/connection"
|
|
_ "github.com/containers/podman/v2/cmd/podman/volumes"
|
|
"github.com/containers/podman/v2/pkg/rootless"
|
|
"github.com/containers/podman/v2/pkg/terminal"
|
|
"github.com/containers/storage/pkg/reexec"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func main() {
|
|
if reexec.Init() {
|
|
// We were invoked with a different argv[0] indicating that we
|
|
// had a specific job to do as a subprocess, and it's done.
|
|
return
|
|
}
|
|
|
|
// Hard code TMPDIR functions to use /var/tmp, if user did not override
|
|
if _, ok := os.LookupEnv("TMPDIR"); !ok {
|
|
os.Setenv("TMPDIR", "/var/tmp")
|
|
}
|
|
|
|
rootCmd = parseCommands()
|
|
|
|
Execute()
|
|
os.Exit(0)
|
|
}
|
|
|
|
func parseCommands() *cobra.Command {
|
|
cfg := registry.PodmanConfig()
|
|
for _, c := range registry.Commands {
|
|
for _, m := range c.Mode {
|
|
if cfg.EngineMode == m {
|
|
// Command cannot be run rootless
|
|
_, found := c.Command.Annotations[registry.UnshareNSRequired]
|
|
if found {
|
|
if rootless.IsRootless() && found && os.Getuid() != 0 {
|
|
c.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
|
return fmt.Errorf("cannot run command %q in rootless mode, must execute `podman unshare` first", cmd.CommandPath())
|
|
}
|
|
}
|
|
} else {
|
|
_, found = c.Command.Annotations[registry.ParentNSRequired]
|
|
if rootless.IsRootless() && found {
|
|
c.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
|
return fmt.Errorf("cannot run command %q in rootless mode", cmd.CommandPath())
|
|
}
|
|
}
|
|
}
|
|
parent := rootCmd
|
|
if c.Parent != nil {
|
|
parent = c.Parent
|
|
}
|
|
parent.AddCommand(c.Command)
|
|
|
|
// - templates need to be set here, as PersistentPreRunE() is
|
|
// not called when --help is used.
|
|
// - rootCmd uses cobra default template not ours
|
|
c.Command.SetHelpTemplate(helpTemplate)
|
|
c.Command.SetUsageTemplate(usageTemplate)
|
|
c.Command.DisableFlagsInUseLine = true
|
|
}
|
|
}
|
|
}
|
|
if err := terminal.SetConsole(); err != nil {
|
|
logrus.Error(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
return rootCmd
|
|
}
|