|
| 1 | +// Package config loads CLI configuration from file and environment. |
| 2 | +package config |
| 3 | + |
| 4 | +import ( |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/github/gh-actions-pin/internal/tag" |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +// Config holds process-wide settings loaded from the config file and |
| 14 | +// environment variables. Create one via LoadConfig at startup and pass |
| 15 | +// it through the pipeline. |
| 16 | +type Config struct { |
| 17 | + // Path is the resolved config file path (empty if none found). |
| 18 | + Path string |
| 19 | + // Cooldown controls how recently a tag must have been released to |
| 20 | + // be excluded from upgrade suggestions. |
| 21 | + Cooldown tag.CooldownConfig |
| 22 | + // Workers is the concurrency limit for pool-parallelized phases. |
| 23 | + // Defaults to 8; overridden by GH_ACTIONS_PIN_WORKERS. |
| 24 | + Workers int |
| 25 | + // StallHintMS is the stall-detection threshold in milliseconds. |
| 26 | + // 0 disables the watcher. Overridden by GH_ACTIONS_PIN_STALL_HINT_MS. |
| 27 | + StallHintMS int |
| 28 | + // DebugProgress enables per-phase progress tracing. |
| 29 | + DebugProgress bool |
| 30 | +} |
| 31 | + |
| 32 | +// Load reads the config file and environment, returning a Config |
| 33 | +// with sensible defaults for any unset values. |
| 34 | +func Load() Config { |
| 35 | + p := configPath() |
| 36 | + c := Config{ |
| 37 | + Path: p, |
| 38 | + Workers: 8, |
| 39 | + StallHintMS: -1, // sentinel: use pinpool default |
| 40 | + DebugProgress: envBool("GH_ACTIONS_PIN_DEBUG_PROGRESS"), |
| 41 | + } |
| 42 | + |
| 43 | + if v, err := strconv.Atoi(os.Getenv("GH_ACTIONS_PIN_WORKERS")); err == nil && v > 0 { |
| 44 | + c.Workers = v |
| 45 | + } |
| 46 | + if v := os.Getenv("GH_ACTIONS_PIN_STALL_HINT_MS"); v != "" { |
| 47 | + if ms, err := strconv.Atoi(v); err == nil { |
| 48 | + c.StallHintMS = ms |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + c.Cooldown = loadCooldownFromFile(p) |
| 53 | + return c |
| 54 | +} |
| 55 | + |
| 56 | +func envBool(key string) bool { |
| 57 | + v := os.Getenv(key) |
| 58 | + return v == "1" || v == "true" || v == "yes" |
| 59 | +} |
| 60 | + |
| 61 | +// loadCooldownFromFile reads cooldown settings from the config file. |
| 62 | +func loadCooldownFromFile(path string) tag.CooldownConfig { |
| 63 | + cfg := tag.CooldownConfig{ |
| 64 | + DefaultDays: 3, |
| 65 | + RepoOverrides: make(map[string]int), |
| 66 | + } |
| 67 | + if path == "" { |
| 68 | + return cfg |
| 69 | + } |
| 70 | + data, err := os.ReadFile(path) |
| 71 | + if err != nil { |
| 72 | + return cfg |
| 73 | + } |
| 74 | + var file struct { |
| 75 | + CooldownDays int `yaml:"cooldown_days"` |
| 76 | + Repos map[string]struct { |
| 77 | + CooldownDays int `yaml:"cooldown_days"` |
| 78 | + } `yaml:"repos"` |
| 79 | + } |
| 80 | + if err := yaml.Unmarshal(data, &file); err != nil { |
| 81 | + return cfg |
| 82 | + } |
| 83 | + if file.CooldownDays > 0 { |
| 84 | + cfg.DefaultDays = file.CooldownDays |
| 85 | + } |
| 86 | + for nwo, repoCfg := range file.Repos { |
| 87 | + if repoCfg.CooldownDays >= 0 { |
| 88 | + cfg.RepoOverrides[nwo] = repoCfg.CooldownDays |
| 89 | + } |
| 90 | + } |
| 91 | + return cfg |
| 92 | +} |
| 93 | + |
| 94 | +// configPath returns the path to the config file, respecting |
| 95 | +// GH_ACTIONS_PIN_CONFIG for testing/demos. |
| 96 | +func configPath() string { |
| 97 | + if p := os.Getenv("GH_ACTIONS_PIN_CONFIG"); p != "" { |
| 98 | + return p |
| 99 | + } |
| 100 | + home, err := os.UserHomeDir() |
| 101 | + if err != nil { |
| 102 | + return "" |
| 103 | + } |
| 104 | + return filepath.Join(home, ".config", "gh-actions-pin", "config.yml") |
| 105 | +} |
0 commit comments