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