aenvs/internal/config/global.go

90 lines
2.0 KiB
Go

package config
import (
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// GlobalConfig represents ~/.config/aevs/config.yaml
type GlobalConfig struct {
Storage StorageConfig `yaml:"storage"`
}
// StorageConfig holds S3-compatible storage credentials
type StorageConfig struct {
Type string `yaml:"type"` // storage type: "s3"
Endpoint string `yaml:"endpoint"` // S3 endpoint URL
Region string `yaml:"region"` // AWS region (optional, default: us-east-1)
Bucket string `yaml:"bucket"` // S3 bucket name
AccessKey string `yaml:"access_key"` // AWS Access Key ID
SecretKey string `yaml:"secret_key"` // AWS Secret Access Key
}
// GlobalConfigPath returns the full path to the global config file
func GlobalConfigPath() string {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(home, DefaultConfigDir, DefaultGlobalConfigFile)
}
// GlobalConfigExists checks if the global config file exists
func GlobalConfigExists() bool {
path := GlobalConfigPath()
if path == "" {
return false
}
_, err := os.Stat(path)
return err == nil
}
// LoadGlobalConfig loads the global config from ~/.config/aevs/config.yaml
func LoadGlobalConfig() (*GlobalConfig, error) {
path := GlobalConfigPath()
if path == "" {
return nil, os.ErrNotExist
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg GlobalConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
// SaveGlobalConfig saves the global config to ~/.config/aevs/config.yaml with 0600 permissions
func SaveGlobalConfig(cfg *GlobalConfig) error {
path := GlobalConfigPath()
if path == "" {
return os.ErrNotExist
}
// Ensure directory exists
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
// Marshal to YAML
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
// Write with secure permissions (0600)
if err := os.WriteFile(path, data, 0600); err != nil {
return err
}
return nil
}