github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/buildpack_inspect.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/buildpacks/pack/internal/config"
     9  	"github.com/buildpacks/pack/internal/style"
    10  	"github.com/buildpacks/pack/pkg/client"
    11  	"github.com/buildpacks/pack/pkg/logging"
    12  )
    13  
    14  type BuildpackInspectFlags struct {
    15  	Depth    int
    16  	Registry string
    17  	Verbose  bool
    18  }
    19  
    20  func BuildpackInspect(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command {
    21  	var flags BuildpackInspectFlags
    22  	cmd := &cobra.Command{
    23  		Use:     "inspect <image-name>",
    24  		Args:    cobra.ExactArgs(1),
    25  		Short:   "Show information about a buildpack",
    26  		Example: "pack buildpack inspect cnbs/sample-package:hello-universe",
    27  		RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
    28  			buildpackName := args[0]
    29  			registry := flags.Registry
    30  			if registry == "" {
    31  				registry = cfg.DefaultRegistryName
    32  			}
    33  
    34  			return buildpackInspect(logger, buildpackName, registry, flags, cfg, client)
    35  		}),
    36  	}
    37  
    38  	cmd.Flags().IntVarP(&flags.Depth, "depth", "d", -1, "Max depth to display for Detection Order.\nOmission of this flag or values < 0 will display the entire tree.")
    39  	cmd.Flags().StringVarP(&flags.Registry, "registry", "r", "", "buildpack registry that may be searched")
    40  	cmd.Flags().BoolVarP(&flags.Verbose, "verbose", "v", false, "show more output")
    41  	AddHelpFlag(cmd, "inspect")
    42  	return cmd
    43  }
    44  
    45  func buildpackInspect(logger logging.Logger, buildpackName, registryName string, flags BuildpackInspectFlags, _ config.Config, pack PackClient) error {
    46  	logger.Infof("Inspecting buildpack: %s\n", style.Symbol(buildpackName))
    47  
    48  	inspectedBuildpacksOutput, err := inspectAllBuildpacks(
    49  		pack,
    50  		flags,
    51  		client.InspectBuildpackOptions{
    52  			BuildpackName: buildpackName,
    53  			Daemon:        true,
    54  			Registry:      registryName,
    55  		},
    56  		client.InspectBuildpackOptions{
    57  			BuildpackName: buildpackName,
    58  			Daemon:        false,
    59  			Registry:      registryName,
    60  		})
    61  	if err != nil {
    62  		return fmt.Errorf("error writing buildpack output: %q", err)
    63  	}
    64  
    65  	logger.Info(inspectedBuildpacksOutput)
    66  	return nil
    67  }