Load reads ~/.cloudcent/config.yaml. Returns nil, nil if missing. If the CLOUDCENT_API_KEY environment variable is set, it takes precedence over the config file (useful for CI environments).
()
| 40 | // If the CLOUDCENT_API_KEY environment variable is set, it takes precedence |
| 41 | // over the config file (useful for CI environments). |
| 42 | func Load() (*Config, error) { |
| 43 | // Environment variable takes precedence — no file needed in CI. |
| 44 | if apiKey := os.Getenv("CLOUDCENT_API_KEY"); apiKey != "" { |
| 45 | cliID := os.Getenv("CLOUDCENT_CLI_ID") // optional |
| 46 | return &Config{CliID: cliID, APIKey: &apiKey}, nil |
| 47 | } |
| 48 | |
| 49 | p, err := Path() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | data, err := os.ReadFile(p) |
| 54 | if os.IsNotExist(err) { |
| 55 | return nil, nil |
| 56 | } |
| 57 | if err != nil { |
| 58 | return nil, fmt.Errorf("failed to read config file: %w", err) |
| 59 | } |
| 60 | var cfg Config |
| 61 | if err := yaml.Unmarshal(data, &cfg); err != nil { |
| 62 | return nil, fmt.Errorf("failed to parse config YAML: %w", err) |
| 63 | } |
| 64 | return &cfg, nil |
| 65 | } |
| 66 | |
| 67 | // Save writes config to ~/.cloudcent/config.yaml with 0600 permissions. |
| 68 | func Save(cfg *Config) error { |
no test coverage detected