spiegel_podman/cmd/podman
Chetan Giradkar 24d3347eda Use ActiveServiceDestination in ssh remoteConnectionUsername
[NO NEW TESTS NEEDED]

Signed-off-by: Chetan Giradkar <cgiradka@redhat.com>
2023-09-21 11:56:19 +01:00
..
common --env-host: use default from containers.conf 2023-09-18 14:47:43 +02:00
completion
containers run --rmi: "cannot remove" is a warning, not an error 2023-09-07 14:30:22 -06:00
diff
farm e2e: more ExitCleanly(): manual test fixes 2023-09-12 21:07:11 -06:00
generate
healthcheck
images Move podman build opts to common file 2023-09-14 20:48:33 -04:00
inspect
kube kube: add DaemonSet support for generate 2023-09-12 10:30:57 +05:30
machine Use ActiveServiceDestination in ssh remoteConnectionUsername 2023-09-21 11:56:19 +01:00
manifest manifest,push: support add_compression from containers.conf 2023-08-29 20:57:50 +05:30
networks
parse Add support for host-gateway 2023-08-07 21:38:10 +02:00
pods Merge pull request #19888 from giuseppe/fix-pod-lifecycle 2023-09-10 20:36:21 -04:00
registry Merge pull request #20012 from vrothberg/module-parsing 2023-09-18 16:40:49 +02:00
secrets document available secret drivers 2023-08-16 16:23:39 +02:00
system Merge pull request #19917 from afbjorklund/unix-url 2023-09-18 16:38:01 +02:00
utils
validate
volumes
auto-update.go
compose.go Plumbing to run machine tests with hyperv 2023-09-14 15:57:35 -05:00
diff.go
early_init_linux.go
early_init_unsupported.go
inspect.go
login.go
logout.go
main.go Podmansh: use podmansh_timeout 2023-09-12 09:18:24 -04:00
README.md
root.go Merge pull request #20062 from vrothberg/syslog-fix 2023-09-20 11:57:33 -04:00
root_test.go
shell_completion_test.go
syslog_common.go pass --syslog to the cleanup process 2023-09-20 15:37:07 +02:00
syslog_unsupported.go pass --syslog to the cleanup process 2023-09-20 15:37:07 +02:00

Podman CLI

The following is an example of how to add a new primary command (manifest) and a sub-command (inspect) to the Podman CLI. This is example code, the production code has additional error checking and the business logic provided.

See items below for details on building, installing, contributing to Podman:

Adding a new command podman manifest

$ mkdir -p $GOPATH/src/github.com/containers/podman/cmd/podmanV2/manifests

Create the file $GOPATH/src/github.com/containers/podman/cmd/podmanV2/manifests/manifest.go

package manifests

import (
    "github.com/containers/podman/cmd/podman/registry"
    "github.com/containers/podman/cmd/podman/validate"
    "github.com/containers/podman/pkg/domain/entities"
    "github.com/spf13/cobra"
)

var (
    // podman _manifests_
    manifestCmd = &cobra.Command{
        Use:               "manifest",
        Short:             "Manage manifests",
        Args:              cobra.ExactArgs(1),
        Long:              "Manage manifests",
        Example:           "podman manifest IMAGE",
        TraverseChildren:  true,
        RunE:              validate.SubCommandExists, // Report error if there is no sub command given
    }
)
func init() {
    // Subscribe command to podman
    registry.Commands = append(registry.Commands, registry.CliCommand{
        Command: manifestCmd,
    })
}

To "wire" in the manifest command, edit the file $GOPATH/src/github.com/containers/podman/cmd/podmanV2/main.go to add:

package main

import	_ "github.com/containers/podman/cmd/podman/manifests"

Adding a new sub command podman manifests list

Create the file $GOPATH/src/github.com/containers/podman/cmd/podmanV2/manifests/inspect.go

package manifests

import (
    "github.com/containers/podman/cmd/podman/registry"
    "github.com/containers/podman/pkg/domain/entities"
    "github.com/spf13/cobra"
)

var (
    // podman manifests _inspect_
    inspectCmd = &cobra.Command{
        Use:     "inspect IMAGE",
        Short:   "Display manifest from image",
        Long:    "Displays the low-level information on a manifest identified by image name or ID",
        RunE:    inspect,
        Annotations: map[string]string{
            // Add this annotation if this command cannot be run rootless
            // registry.ParentNSRequired: "",
        },
        Example: "podman manifest inspect DEADBEEF",
    }
)

func init() {
    // Subscribe inspect sub command to manifest command
    registry.Commands = append(registry.Commands, registry.CliCommand{
        Command: inspectCmd,
        // The parent command to proceed this command on the CLI
        Parent:  manifestCmd,
    })

    // This is where you would configure the cobra flags using inspectCmd.Flags()
}

// Business logic: cmd is inspectCmd, args is the positional arguments from os.Args
func inspect(cmd *cobra.Command, args []string) error {
    // Business logic using registry.ImageEngine()
    // Do not pull from libpod directly use the domain objects and types
    return nil
}

Helper functions

The complete set can be found in the validate package, here are some examples:

  • cobra.Command{ Args: validate.NoArgs } used when the command does not accept errors
  • cobra.Command{ Args: validate.IdOrLatestArgs } used to ensure either a list of ids given or the --latest flag
  • cobra.Command{ RunE: validate.SubCommandExists } used to validate a subcommand given to a command
  • validate.ChoiceValue used to create a pflag.Value that validate user input against a provided slice of values. For example:
    flags := cobraCommand.Flags()
    created := validate.ChoiceValue(&opts.Sort, "command", "created", "id", "image", "names", "runningfor", "size", "status")
    flags.Var(created, "sort", "Sort output by: "+created.Choices())