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

     1  package commands
     2  
     3  import (
     4  	"path/filepath"
     5  	"strconv"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/buildpacks/pack/internal/config"
    11  	"github.com/buildpacks/pack/internal/style"
    12  	"github.com/buildpacks/pack/pkg/logging"
    13  )
    14  
    15  func ConfigExperimental(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   "experimental [<true | false>]",
    18  		Args:  cobra.MaximumNArgs(1),
    19  		Short: "List and set the current 'experimental' value from the config",
    20  		Long: "Experimental features in pack are gated, and require you adding setting `experimental=true` to the Pack Config, either manually, or using this command.\n\n" +
    21  			"* Running `pack config experimental` prints whether experimental features are currently enabled.\n" +
    22  			"* Running `pack config experimental <true | false>` enables or disables experimental features.",
    23  		RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
    24  			switch {
    25  			case len(args) == 0:
    26  				if cfg.Experimental {
    27  					logger.Infof("Experimental features are enabled! To turn them off, run `pack config experimental false`")
    28  				} else {
    29  					logger.Info("Experimental features aren't currently enabled. To enable them, run `pack config experimental true`")
    30  				}
    31  			default:
    32  				val, err := strconv.ParseBool(args[0])
    33  				if err != nil {
    34  					return errors.Wrapf(err, "invalid value %s provided", style.Symbol(args[0]))
    35  				}
    36  				cfg.Experimental = val
    37  				if cfg.Experimental {
    38  					cfg.LayoutRepositoryDir = filepath.Join(filepath.Dir(cfgPath), "layout-repo")
    39  				} else {
    40  					cfg.LayoutRepositoryDir = ""
    41  				}
    42  
    43  				if err = config.Write(cfg, cfgPath); err != nil {
    44  					return errors.Wrap(err, "writing to config")
    45  				}
    46  
    47  				if cfg.Experimental {
    48  					logger.Info("Experimental features enabled!")
    49  				} else {
    50  					logger.Info("Experimental features disabled")
    51  				}
    52  			}
    53  
    54  			return nil
    55  		}),
    56  	}
    57  
    58  	AddHelpFlag(cmd, "experimental")
    59  	return cmd
    60  }