github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/tree.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/containers/libpod/cmd/podman/cliconfig"
     7  	"github.com/containers/libpod/pkg/adapter"
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	treeCommand cliconfig.TreeValues
    14  
    15  	treeDescription = "Prints layer hierarchy of an image in a tree format"
    16  	_treeCommand    = &cobra.Command{
    17  		Use:   "tree [flags] IMAGE",
    18  		Short: treeDescription,
    19  		Long:  treeDescription,
    20  		RunE: func(cmd *cobra.Command, args []string) error {
    21  			treeCommand.InputArgs = args
    22  			treeCommand.GlobalFlags = MainGlobalOpts
    23  			treeCommand.Remote = remoteclient
    24  			return treeCmd(&treeCommand)
    25  		},
    26  		Example: "podman image tree alpine:latest",
    27  	}
    28  )
    29  
    30  func init() {
    31  	treeCommand.Command = _treeCommand
    32  	treeCommand.SetUsageTemplate(UsageTemplate())
    33  	treeCommand.Flags().BoolVar(&treeCommand.WhatRequires, "whatrequires", false, "Show all child images and layers of the specified image")
    34  }
    35  
    36  func treeCmd(c *cliconfig.TreeValues) error {
    37  	args := c.InputArgs
    38  	if len(args) == 0 {
    39  		return errors.Errorf("an image name must be specified")
    40  	}
    41  	if len(args) > 1 {
    42  		return errors.Errorf("you must provide at most 1 argument")
    43  	}
    44  
    45  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    46  	if err != nil {
    47  		return errors.Wrapf(err, "error creating libpod runtime")
    48  	}
    49  	defer runtime.DeferredShutdown(false)
    50  
    51  	tree, err := runtime.ImageTree(c.InputArgs[0], c.WhatRequires)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	fmt.Print(tree)
    56  	return nil
    57  }