-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.go
More file actions
69 lines (57 loc) · 1.61 KB
/
config.go
File metadata and controls
69 lines (57 loc) · 1.61 KB
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
package main
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
)
// Config represents the application configuration
type Config struct {
Port int `yaml:"port"`
CacheDir string `yaml:"cache_dir"`
MaxCacheAge time.Duration `yaml:"max_cache_age"`
Registries map[string]RegistryConfig `yaml:"registries"`
}
// RegistryConfig holds credentials for a specific registry
type RegistryConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// LoadConfig loads configuration from a YAML file
func LoadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
config.ApplyDefaults()
if err := config.Validate(); err != nil {
return nil, err
}
return &config, nil
}
// ApplyDefaults sets default values for unspecified configuration options
func (c *Config) ApplyDefaults() {
if c.Port == 0 {
c.Port = 8080
}
if c.MaxCacheAge == 0 {
c.MaxCacheAge = 48 * time.Hour
}
}
// Validate checks if the configuration is valid
func (c *Config) Validate() error {
if c.Port < 1 || c.Port > 65535 {
return fmt.Errorf("invalid port: %d (must be between 1 and 65535)", c.Port)
}
return nil
}
// ApplyCredentials registers all configured registry credentials
func (c *Config) ApplyCredentials() {
for registry, creds := range c.Registries {
SetCredentials(registry, creds.Username, creds.Password)
}
}