cli/pkg/devops/config.go
Snider c615408700
Centralized Configuration Service Implementation (#319)
* feat: implement centralized configuration service using viper

This commit introduces a centralized configuration service in `pkg/config`
to reduce code duplication and provide a unified way to manage configuration
across the project.

Key changes:
- Refactored `pkg/config` to use `github.com/spf13/viper` as the backend.
- Implemented `core.Config` interface with support for layered resolution
  (defaults, files, environment variables).
- Added `LoadFile` to support merging multiple configuration files, with
  automatic type detection for YAML and .env files.
- Migrated `pkg/agentic`, `pkg/devops`, `pkg/build`, and `pkg/release`
  to use the new centralized service.
- Added `mapstructure` tags to configuration structs to support viper unmarshaling.
- Added comprehensive tests for the new configuration service features.

This addresses the recommendations from the Architecture & Design Pattern Audit.

* feat: implement centralized configuration service and address security alerts

- Introduced centralized configuration service in `pkg/config` using `viper`.
- Migrated major packages (`agentic`, `devops`, `build`, `release`) to the new service.
- Resolved merge conflicts with `dev` branch.
- Addressed CodeQL security alert by making UniFi TLS verification configurable.
- Fixed `go.mod` to ensure it is tidy and consistent with direct dependencies.
- Updated UniFi CLI to support TLS verification configuration.
2026-02-05 10:26:44 +00:00

90 lines
2.3 KiB
Go

package devops
import (
"os"
"path/filepath"
"github.com/host-uk/core/pkg/config"
"github.com/host-uk/core/pkg/io"
)
// Config holds global devops configuration from ~/.core/config.yaml.
type Config struct {
Version int `yaml:"version" mapstructure:"version"`
Images ImagesConfig `yaml:"images" mapstructure:"images"`
}
// ImagesConfig holds image source configuration.
type ImagesConfig struct {
Source string `yaml:"source" mapstructure:"source"` // auto, github, registry, cdn
GitHub GitHubConfig `yaml:"github,omitempty" mapstructure:"github,omitempty"`
Registry RegistryConfig `yaml:"registry,omitempty" mapstructure:"registry,omitempty"`
CDN CDNConfig `yaml:"cdn,omitempty" mapstructure:"cdn,omitempty"`
}
// GitHubConfig holds GitHub Releases configuration.
type GitHubConfig struct {
Repo string `yaml:"repo" mapstructure:"repo"` // owner/repo format
}
// RegistryConfig holds container registry configuration.
type RegistryConfig struct {
Image string `yaml:"image" mapstructure:"image"` // e.g., ghcr.io/host-uk/core-devops
}
// CDNConfig holds CDN/S3 configuration.
type CDNConfig struct {
URL string `yaml:"url" mapstructure:"url"` // base URL for downloads
}
// DefaultConfig returns sensible defaults.
func DefaultConfig() *Config {
return &Config{
Version: 1,
Images: ImagesConfig{
Source: "auto",
GitHub: GitHubConfig{
Repo: "host-uk/core-images",
},
Registry: RegistryConfig{
Image: "ghcr.io/host-uk/core-devops",
},
},
}
}
// ConfigPath returns the path to the config file.
func ConfigPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".core", "config.yaml"), nil
}
// LoadConfig loads configuration from ~/.core/config.yaml using the provided medium.
// Returns default config if file doesn't exist.
func LoadConfig(m io.Medium) (*Config, error) {
configPath, err := ConfigPath()
if err != nil {
return DefaultConfig(), nil
}
cfg := DefaultConfig()
if !m.IsFile(configPath) {
return cfg, nil
}
// Use centralized config service
c, err := config.New(config.WithMedium(m), config.WithPath(configPath))
if err != nil {
return nil, err
}
if err := c.Get("", cfg); err != nil {
return nil, err
}
return cfg, nil
}