github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/image/inspect.go (about)

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/docker/app/internal/packager"
     9  
    10  	"github.com/deislabs/cnab-go/action"
    11  	"github.com/docker/app/internal"
    12  	"github.com/docker/app/internal/cliopts"
    13  	"github.com/docker/app/internal/cnab"
    14  	"github.com/docker/app/internal/inspect"
    15  	appstore "github.com/docker/app/internal/store"
    16  	"github.com/docker/cli/cli"
    17  	"github.com/docker/cli/cli/command"
    18  	"github.com/docker/cli/cli/config"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  const inspectExample = `- $ docker app image inspect myapp
    23  - $ docker app image inspect myapp:1.0.0
    24  - $ docker app image inspect myrepo/myapp:1.0.0
    25  - $ docker app image inspect 34be4a0c5f50`
    26  
    27  type inspectOptions struct {
    28  	pretty bool
    29  }
    30  
    31  func muteDockerCli(dockerCli command.Cli) func() {
    32  	stdout := dockerCli.Out()
    33  	stderr := dockerCli.Err()
    34  	dockerCli.Apply(command.WithCombinedStreams(ioutil.Discard)) //nolint:errcheck // WithCombinedStreams cannot error
    35  	return func() {
    36  		dockerCli.Apply(command.WithOutputStream(stdout), command.WithErrorStream(stderr)) //nolint:errcheck // as above
    37  	}
    38  }
    39  
    40  func inspectCmd(dockerCli command.Cli, installerContext *cliopts.InstallerContextOptions) *cobra.Command {
    41  	var opts inspectOptions
    42  	cmd := &cobra.Command{
    43  		Use:     "inspect [OPTIONS] APP_IMAGE",
    44  		Short:   "Display detailed information about an App image",
    45  		Example: inspectExample,
    46  		Args:    cli.ExactArgs(1),
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			return runInspect(dockerCli, args[0], opts, installerContext)
    49  		},
    50  	}
    51  	cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
    52  
    53  	return cmd
    54  }
    55  
    56  func runInspect(dockerCli command.Cli, appname string, opts inspectOptions, installerContext *cliopts.InstallerContextOptions) error {
    57  	s, err := appstore.NewApplicationStore(config.Dir())
    58  	if err != nil {
    59  		return err
    60  	}
    61  	imageStore, err := s.ImageStore()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	bndl, ref, err := cnab.GetBundle(dockerCli, imageStore, appname)
    66  
    67  	if err != nil {
    68  		return err
    69  	}
    70  	if err := packager.CheckAppVersion(dockerCli.Err(), bndl.Bundle); err != nil {
    71  		return err
    72  	}
    73  
    74  	format := "json"
    75  	if opts.pretty {
    76  		format = "pretty"
    77  	}
    78  
    79  	installation, err := appstore.NewInstallation("custom-action", ref.String(), bndl)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	defer muteDockerCli(dockerCli)()
    85  	if _, hasAction := installation.Bundle.Actions[internal.ActionInspectName]; hasAction {
    86  		driverImpl, errBuf, err := cnab.SetupDriver(installation, dockerCli, installerContext, os.Stdout)
    87  		if err != nil {
    88  			return err
    89  		}
    90  		a := &action.RunCustom{
    91  			Action: internal.ActionInspectName,
    92  			Driver: driverImpl,
    93  		}
    94  
    95  		installation.SetParameter(internal.ParameterInspectFormatName, format)
    96  		if err = a.Run(&installation.Claim, nil, cnab.WithRelocationMap(installation)); err != nil {
    97  			return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
    98  		}
    99  	} else {
   100  		if err = inspect.ImageInspectCNAB(os.Stdout, bndl.Bundle, format); err != nil {
   101  			return fmt.Errorf("inspect failed: %s", err)
   102  		}
   103  	}
   104  	return nil
   105  }