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

     1  package config
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/BurntSushi/toml"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/buildpacks/pack/internal/style"
    11  )
    12  
    13  type Config struct {
    14  	// Deprecated: Use DefaultRegistryName instead. See https://github.com/buildpacks/pack/issues/747.
    15  	DefaultRegistry     string            `toml:"default-registry-url,omitempty"`
    16  	DefaultRegistryName string            `toml:"default-registry,omitempty"`
    17  	DefaultBuilder      string            `toml:"default-builder-image,omitempty"`
    18  	PullPolicy          string            `toml:"pull-policy,omitempty"`
    19  	Experimental        bool              `toml:"experimental,omitempty"`
    20  	RunImages           []RunImage        `toml:"run-images"`
    21  	TrustedBuilders     []TrustedBuilder  `toml:"trusted-builders,omitempty"`
    22  	Registries          []Registry        `toml:"registries,omitempty"`
    23  	LifecycleImage      string            `toml:"lifecycle-image,omitempty"`
    24  	RegistryMirrors     map[string]string `toml:"registry-mirrors,omitempty"`
    25  	LayoutRepositoryDir string            `toml:"layout-repo-dir,omitempty"`
    26  }
    27  
    28  type Registry struct {
    29  	Name string `toml:"name"`
    30  	Type string `toml:"type"`
    31  	URL  string `toml:"url"`
    32  }
    33  
    34  type RunImage struct {
    35  	Image   string   `toml:"image"`
    36  	Mirrors []string `toml:"mirrors"`
    37  }
    38  
    39  type TrustedBuilder struct {
    40  	Name string `toml:"name"`
    41  }
    42  
    43  const OfficialRegistryName = "official"
    44  
    45  func DefaultRegistry() Registry {
    46  	return Registry{
    47  		OfficialRegistryName,
    48  		"github",
    49  		"https://github.com/buildpacks/registry-index",
    50  	}
    51  }
    52  
    53  func DefaultConfigPath() (string, error) {
    54  	home, err := PackHome()
    55  	if err != nil {
    56  		return "", errors.Wrap(err, "getting pack home")
    57  	}
    58  	return filepath.Join(home, "config.toml"), nil
    59  }
    60  
    61  func PackHome() (string, error) {
    62  	packHome := os.Getenv("PACK_HOME")
    63  	if packHome == "" {
    64  		home, err := os.UserHomeDir()
    65  		if err != nil {
    66  			return "", errors.Wrap(err, "getting user home")
    67  		}
    68  		packHome = filepath.Join(home, ".pack")
    69  	}
    70  	return packHome, nil
    71  }
    72  
    73  func Read(path string) (Config, error) {
    74  	cfg := Config{}
    75  	_, err := toml.DecodeFile(path, &cfg)
    76  	if err != nil && !os.IsNotExist(err) {
    77  		return Config{}, errors.Wrapf(err, "failed to read config file at path %s", path)
    78  	}
    79  	return cfg, nil
    80  }
    81  
    82  func Write(cfg Config, path string) error {
    83  	if err := MkdirAll(filepath.Dir(path)); err != nil {
    84  		return err
    85  	}
    86  	w, err := os.Create(path)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	defer w.Close()
    91  
    92  	return toml.NewEncoder(w).Encode(cfg)
    93  }
    94  
    95  func MkdirAll(path string) error {
    96  	return os.MkdirAll(path, 0750)
    97  }
    98  
    99  func SetRunImageMirrors(cfg Config, image string, mirrors []string) Config {
   100  	for i := range cfg.RunImages {
   101  		if cfg.RunImages[i].Image == image {
   102  			cfg.RunImages[i].Mirrors = mirrors
   103  			return cfg
   104  		}
   105  	}
   106  	cfg.RunImages = append(cfg.RunImages, RunImage{Image: image, Mirrors: mirrors})
   107  	return cfg
   108  }
   109  
   110  func GetRegistries(cfg Config) []Registry {
   111  	return append(cfg.Registries, DefaultRegistry())
   112  }
   113  
   114  func GetRegistry(cfg Config, registryName string) (Registry, error) {
   115  	if registryName == "" && cfg.DefaultRegistryName != "" {
   116  		registryName = cfg.DefaultRegistryName
   117  	}
   118  	if registryName == "" && cfg.DefaultRegistryName == "" {
   119  		registryName = OfficialRegistryName
   120  	}
   121  	if registryName != "" {
   122  		for _, registry := range GetRegistries(cfg) {
   123  			if registry.Name == registryName {
   124  				return registry, nil
   125  			}
   126  		}
   127  	}
   128  	return Registry{}, errors.Errorf("registry %s is not defined in your config file", style.Symbol(registryName))
   129  }
   130  
   131  const DefaultLifecycleImageRepo = "buildpacksio/lifecycle"