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

     1  package commands
     2  
     3  import (
     4  	"github.com/google/go-containerregistry/pkg/name"
     5  	"github.com/pkg/errors"
     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/logging"
    11  )
    12  
    13  func ConfigLifecycleImage(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
    14  	var unset bool
    15  
    16  	cmd := &cobra.Command{
    17  		Use:   "lifecycle-image <lifecycle-image>",
    18  		Args:  cobra.MaximumNArgs(1),
    19  		Short: "Configure a custom container image for the lifecycle",
    20  		Long: "You can use this command to set a custom image to fetch the lifecycle from." +
    21  			"This will be used for untrusted builders. If unset, defaults to: " + config.DefaultLifecycleImageRepo +
    22  			"For more on trusted builders, and when to trust or untrust a builder, " +
    23  			"check out our docs here: https://buildpacks.io/docs/tools/pack/concepts/trusted_builders/",
    24  		RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
    25  			switch {
    26  			case unset:
    27  				if len(args) > 0 {
    28  					return errors.Errorf("lifecycle image and --unset cannot be specified simultaneously")
    29  				}
    30  
    31  				if cfg.LifecycleImage == "" {
    32  					logger.Info("No custom lifecycle image was set.")
    33  				} else {
    34  					oldImage := cfg.LifecycleImage
    35  					cfg.LifecycleImage = ""
    36  					if err := config.Write(cfg, cfgPath); err != nil {
    37  						return errors.Wrapf(err, "failed to write to config at %s", cfgPath)
    38  					}
    39  					logger.Infof("Successfully unset custom lifecycle image %s", style.Symbol(oldImage))
    40  				}
    41  			case len(args) == 0:
    42  				if cfg.LifecycleImage != "" {
    43  					logger.Infof("The current custom lifecycle image is %s", style.Symbol(cfg.LifecycleImage))
    44  				} else {
    45  					logger.Infof("No custom lifecycle image is set. Lifecycle images from %s repo will be used.", style.Symbol(config.DefaultLifecycleImageRepo))
    46  				}
    47  				return nil
    48  			default:
    49  				imageName := args[0]
    50  				_, err := name.ParseReference(imageName)
    51  
    52  				if err != nil {
    53  					return errors.Wrapf(err, "Invalid image name %s provided", style.Symbol(imageName))
    54  				}
    55  				if imageName == cfg.LifecycleImage {
    56  					logger.Infof("Custom lifecycle image is already set to %s", style.Symbol(imageName))
    57  					return nil
    58  				}
    59  
    60  				cfg.LifecycleImage = imageName
    61  				if err := config.Write(cfg, cfgPath); err != nil {
    62  					return errors.Wrapf(err, "failed to write to config at %s", cfgPath)
    63  				}
    64  				logger.Infof("Image %s will now be used as the lifecycle image", style.Symbol(imageName))
    65  			}
    66  
    67  			return nil
    68  		}),
    69  	}
    70  
    71  	cmd.Flags().BoolVarP(&unset, "unset", "u", false, "Unset custom lifecycle image, and use the lifecycle images from "+style.Symbol(config.DefaultLifecycleImageRepo))
    72  	AddHelpFlag(cmd, "lifecycle-image")
    73  	return cmd
    74  }