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

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"os/signal"
     8  	"syscall"
     9  
    10  	"github.com/google/go-containerregistry/pkg/v1/types"
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/buildpacks/pack/internal/config"
    15  	"github.com/buildpacks/pack/internal/style"
    16  	"github.com/buildpacks/pack/pkg/client"
    17  	"github.com/buildpacks/pack/pkg/logging"
    18  )
    19  
    20  //go:generate mockgen -package testmocks -destination testmocks/mock_pack_client.go github.com/buildpacks/pack/internal/commands PackClient
    21  type PackClient interface {
    22  	InspectBuilder(string, bool, ...client.BuilderInspectionModifier) (*client.BuilderInfo, error)
    23  	InspectImage(string, bool) (*client.ImageInfo, error)
    24  	Rebase(context.Context, client.RebaseOptions) error
    25  	CreateBuilder(context.Context, client.CreateBuilderOptions) error
    26  	NewBuildpack(context.Context, client.NewBuildpackOptions) error
    27  	PackageBuildpack(ctx context.Context, opts client.PackageBuildpackOptions) error
    28  	PackageExtension(ctx context.Context, opts client.PackageBuildpackOptions) error
    29  	Build(context.Context, client.BuildOptions) error
    30  	RegisterBuildpack(context.Context, client.RegisterBuildpackOptions) error
    31  	YankBuildpack(client.YankBuildpackOptions) error
    32  	InspectBuildpack(client.InspectBuildpackOptions) (*client.BuildpackInfo, error)
    33  	InspectExtension(client.InspectExtensionOptions) (*client.ExtensionInfo, error)
    34  	PullBuildpack(context.Context, client.PullBuildpackOptions) error
    35  	DownloadSBOM(name string, options client.DownloadSBOMOptions) error
    36  	CreateManifest(ctx context.Context, opts client.CreateManifestOptions) error
    37  	AnnotateManifest(ctx context.Context, opts client.ManifestAnnotateOptions) error
    38  	AddManifest(ctx context.Context, opts client.ManifestAddOptions) error
    39  	DeleteManifest(name []string) error
    40  	RemoveManifest(name string, images []string) error
    41  	PushManifest(client.PushManifestOptions) error
    42  	InspectManifest(string) error
    43  }
    44  
    45  func AddHelpFlag(cmd *cobra.Command, commandName string) {
    46  	cmd.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for '%s'", commandName))
    47  }
    48  
    49  func CreateCancellableContext() context.Context {
    50  	signals := make(chan os.Signal, 1)
    51  	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
    52  	ctx, cancel := context.WithCancel(context.Background())
    53  
    54  	go func() {
    55  		<-signals
    56  		cancel()
    57  	}()
    58  
    59  	return ctx
    60  }
    61  
    62  func logError(logger logging.Logger, f func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
    63  	return func(cmd *cobra.Command, args []string) error {
    64  		cmd.SilenceErrors = true
    65  		cmd.SilenceUsage = true
    66  		err := f(cmd, args)
    67  		if err != nil {
    68  			if _, isSoftError := errors.Cause(err).(client.SoftError); !isSoftError {
    69  				logger.Error(err.Error())
    70  			}
    71  
    72  			if _, isExpError := errors.Cause(err).(client.ExperimentError); isExpError {
    73  				configPath, err := config.DefaultConfigPath()
    74  				if err != nil {
    75  					return err
    76  				}
    77  				enableExperimentalTip(logger, configPath)
    78  			}
    79  			return err
    80  		}
    81  		return nil
    82  	}
    83  }
    84  
    85  func enableExperimentalTip(logger logging.Logger, configPath string) {
    86  	logging.Tip(logger, "To enable experimental features, run `pack config experimental true` to add %s to %s.", style.Symbol("experimental = true"), style.Symbol(configPath))
    87  }
    88  
    89  func stringArrayHelp(name string) string {
    90  	return fmt.Sprintf("\nRepeat for each %s in order (comma-separated lists not accepted)", name)
    91  }
    92  
    93  func stringSliceHelp(name string) string {
    94  	return fmt.Sprintf("\nRepeat for each %s in order, or supply once by comma-separated list", name)
    95  }
    96  
    97  func getMirrors(config config.Config) map[string][]string {
    98  	mirrors := map[string][]string{}
    99  	for _, ri := range config.RunImages {
   100  		mirrors[ri.Image] = ri.Mirrors
   101  	}
   102  	return mirrors
   103  }
   104  
   105  func isTrustedBuilder(cfg config.Config, builder string) bool {
   106  	for _, trustedBuilder := range cfg.TrustedBuilders {
   107  		if builder == trustedBuilder.Name {
   108  			return true
   109  		}
   110  	}
   111  
   112  	return isSuggestedBuilder(builder)
   113  }
   114  
   115  func deprecationWarning(logger logging.Logger, oldCmd, replacementCmd string) {
   116  	logger.Warnf("Command %s has been deprecated, please use %s instead", style.Symbol("pack "+oldCmd), style.Symbol("pack "+replacementCmd))
   117  }
   118  
   119  func parseFormatFlag(value string) (types.MediaType, error) {
   120  	var format types.MediaType
   121  	switch value {
   122  	case "oci":
   123  		format = types.OCIImageIndex
   124  	case "docker":
   125  		format = types.DockerManifestList
   126  	default:
   127  		return format, errors.Errorf("%s invalid media type format", value)
   128  	}
   129  	return format, nil
   130  }