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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/buildpacks/pack/internal/config"
    10  	"github.com/buildpacks/pack/internal/style"
    11  	"github.com/buildpacks/pack/pkg/image"
    12  	"github.com/buildpacks/pack/pkg/logging"
    13  )
    14  
    15  func ConfigPullPolicy(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
    16  	var unset bool
    17  
    18  	cmd := &cobra.Command{
    19  		Use:   "pull-policy <always | if-not-present | never>",
    20  		Args:  cobra.MaximumNArgs(1),
    21  		Short: "List, set and unset the global pull policy used by other commands",
    22  		Long: "You can use this command to list, set, and unset the default pull policy that will be used when working with containers:\n" +
    23  			"* To list your pull policy, run `pack config pull-policy`.\n" +
    24  			"* To set your pull policy, run `pack config pull-policy <always | if-not-present | never>`.\n" +
    25  			"* To unset your pull policy, run `pack config pull-policy --unset`.\n" +
    26  			fmt.Sprintf("Unsetting the pull policy will reset the policy to the default, which is %s", style.Symbol("always")),
    27  		RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
    28  			switch {
    29  			case unset:
    30  				if len(args) > 0 {
    31  					return errors.Errorf("pull policy and --unset cannot be specified simultaneously")
    32  				}
    33  				oldPullPolicy := cfg.PullPolicy
    34  				cfg.PullPolicy = ""
    35  				if err := config.Write(cfg, cfgPath); err != nil {
    36  					return errors.Wrapf(err, "writing config to %s", cfgPath)
    37  				}
    38  
    39  				pullPolicy, err := image.ParsePullPolicy(cfg.PullPolicy)
    40  				if err != nil {
    41  					return err
    42  				}
    43  
    44  				logger.Infof("Successfully unset pull policy %s", style.Symbol(oldPullPolicy))
    45  				logger.Infof("Pull policy has been set to %s", style.Symbol(pullPolicy.String()))
    46  			case len(args) == 0: // list
    47  				pullPolicy, err := image.ParsePullPolicy(cfg.PullPolicy)
    48  				if err != nil {
    49  					return err
    50  				}
    51  
    52  				logger.Infof("The current pull policy is %s", style.Symbol(pullPolicy.String()))
    53  			default: // set
    54  				newPullPolicy := args[0]
    55  
    56  				if newPullPolicy == cfg.PullPolicy {
    57  					logger.Infof("Pull policy is already set to %s", style.Symbol(newPullPolicy))
    58  					return nil
    59  				}
    60  
    61  				pullPolicy, err := image.ParsePullPolicy(newPullPolicy)
    62  				if err != nil {
    63  					return err
    64  				}
    65  
    66  				cfg.PullPolicy = newPullPolicy
    67  				if err := config.Write(cfg, cfgPath); err != nil {
    68  					return errors.Wrapf(err, "writing config to %s", cfgPath)
    69  				}
    70  
    71  				logger.Infof("Successfully set %s as the pull policy", style.Symbol(pullPolicy.String()))
    72  			}
    73  
    74  			return nil
    75  		}),
    76  	}
    77  
    78  	cmd.Flags().BoolVarP(&unset, "unset", "u", false, "Unset pull policy, and set it back to the default pull-policy, which is "+style.Symbol("always"))
    79  	AddHelpFlag(cmd, "pull-policy")
    80  	return cmd
    81  }