LoadCredentials loads API credentials from file
()
| 256 | |
| 257 | // LoadCredentials loads API credentials from file |
| 258 | func (m *Manager) LoadCredentials() error { |
| 259 | if _, err := os.Stat(m.config.CredentialsFile); errors.Is(err, fs.ErrNotExist) { |
| 260 | return fmt.Errorf("credentials file not found at %s", m.config.CredentialsFile) |
| 261 | } |
| 262 | |
| 263 | viper.New() |
| 264 | credViper := viper.New() |
| 265 | credViper.SetConfigFile(m.config.CredentialsFile) |
| 266 | credViper.SetConfigType("yaml") |
| 267 | |
| 268 | if err := credViper.ReadInConfig(); err != nil { |
| 269 | return fmt.Errorf("error reading credentials file: %w", err) |
| 270 | } |
| 271 | |
| 272 | m.credentials = &models.Credentials{} |
| 273 | if err := credViper.Unmarshal(m.credentials); err != nil { |
| 274 | return fmt.Errorf("error unmarshaling credentials: %w", err) |
| 275 | } |
| 276 | |
| 277 | if m.credentials.APIID == "" || m.credentials.APIKey == "" { |
| 278 | return fmt.Errorf("api_id and api_key must be configured in %s", m.config.CredentialsFile) |
| 279 | } |
| 280 | |
| 281 | return nil |
| 282 | } |
| 283 | |
| 284 | // SaveCredentials saves API credentials to file using atomic write to prevent TOCTOU race |
| 285 | func (m *Manager) SaveCredentials(apiID, apiKey string) error { |
no test coverage detected