github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/README.md (about)

     1  # Adding a podman V2 commands
     2  
     3  ## Build podman V2
     4  
     5  ```shell script
     6  $ cd $GOPATH/src/github.com/containers/libpod/cmd/podmanV2
     7  ```
     8  If you wish to include the libpod library in your program,
     9  ```shell script
    10  $ go build -tags 'ABISupport' .
    11  ```
    12  The `--remote` flag may be used to connect to the Podman service using the API.
    13  Otherwise, direct calls will be made to the Libpod library.
    14  ```shell script
    15  $ go build -tags '!ABISupport' .
    16  ```
    17  The Libpod library is not linked into the executable.
    18  All calls are made via the API and `--remote=False` is an error condition.
    19  
    20  ## Adding a new command `podman manifests`
    21  ```shell script
    22  $ mkdir -p $GOPATH/src/github.com/containers/libpod/cmd/podmanV2/manifests
    23  ```
    24  Create the file ```$GOPATH/src/github.com/containers/libpod/cmd/podmanV2/manifests/manifest.go```
    25  ```go
    26  package manifests
    27  
    28  import (
    29      "github.com/containers/libpod/cmd/podmanV2/registry"
    30      "github.com/containers/libpod/pkg/domain/entities"
    31      "github.com/spf13/cobra"
    32  )
    33  
    34  var (
    35      // podman _manifests_
    36      manifestCmd = &cobra.Command{
    37          Use:               "manifest",
    38          Short:             "Manage manifests",
    39          Long:              "Manage manifests",
    40          Example:           "podman manifests IMAGE",
    41          TraverseChildren:  true,
    42          PersistentPreRunE: preRunE,
    43          RunE:              registry.SubCommandExists, // Report error if there is no sub command given
    44      }
    45  )
    46  func init() {
    47      // Subscribe command to podman
    48      registry.Commands = append(registry.Commands, registry.CliCommand{
    49          // _podman manifest_ will support both ABIMode and TunnelMode
    50          Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    51          // The definition for this command
    52          Command: manifestCmd,
    53      })
    54      // Setup cobra templates, sub commands will inherit
    55      manifestCmd.SetHelpTemplate(registry.HelpTemplate())
    56      manifestCmd.SetUsageTemplate(registry.UsageTemplate())
    57  }
    58  
    59  // preRunE populates the image engine for sub commands
    60  func preRunE(cmd *cobra.Command, args []string) error {
    61      _, err := registry.NewImageEngine(cmd, args)
    62      return err
    63  }
    64  ```
    65  To "wire" in the `manifest` command, edit the file ```$GOPATH/src/github.com/containers/libpod/cmd/podmanV2/main.go``` to add:
    66  ```go
    67  package main
    68  
    69  import	_ "github.com/containers/libpod/cmd/podmanV2/manifests"
    70  ```
    71  
    72  ## Adding a new sub command `podman manifests list`
    73  Create the file ```$GOPATH/src/github.com/containers/libpod/cmd/podmanV2/manifests/inspect.go```
    74  ```go
    75  package manifests
    76  
    77  import (
    78      "github.com/containers/libpod/cmd/podmanV2/registry"
    79      "github.com/containers/libpod/pkg/domain/entities"
    80      "github.com/spf13/cobra"
    81  )
    82  
    83  var (
    84      // podman manifests _inspect_
    85      inspectCmd = &cobra.Command{
    86          Use:     "inspect IMAGE",
    87          Short:   "Display manifest from image",
    88          Long:    "Displays the low-level information on a manifest identified by image name or ID",
    89          RunE:    inspect,
    90          Example: "podman manifest DEADBEEF",
    91      }
    92  )
    93  
    94  func init() {
    95      // Subscribe inspect sub command to manifest command
    96      registry.Commands = append(registry.Commands, registry.CliCommand{
    97          // _podman manifest inspect_ will support both ABIMode and TunnelMode
    98          Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    99          // The definition for this command
   100          Command: inspectCmd,
   101          Parent:  manifestCmd,
   102      })
   103  
   104      // This is where you would configure the cobra flags using inspectCmd.Flags()
   105  }
   106  
   107  // Business logic: cmd is inspectCmd, args is the positional arguments from os.Args
   108  func inspect(cmd *cobra.Command, args []string) error {
   109      // Business logic using registry.ImageEngine
   110      // Do not pull from libpod directly use the domain objects and types
   111      return nil
   112  }
   113  ```