claude-code-toolkit 0.3.1

Claude Code management tool for credential sync, session monitoring, and GitHub integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Configuration manager following Repository Pattern and Dependency Injection

use crate::error::{ ClaudeCodeError, Result };
use crate::traits::{ ConfigManager as ConfigManagerTrait, ConfigProvider };
use crate::types::Config;
use async_trait::async_trait;
use dirs::home_dir;
use std::path::{ Path, PathBuf };
use tokio::fs;
use tracing::{ debug, info, warn };

/// YAML configuration provider implementation
pub struct YamlConfigProvider {
  config_path: PathBuf,
  config_dir: PathBuf,
}

impl YamlConfigProvider {
  pub fn new() -> Result<Self> {
    let config_dir = home_dir()
      .ok_or("Could not determine home directory")?
      .join(".goodiebag")
      .join("claude-code");

    let config_path = config_dir.join("config.yml");

    Ok(Self {
      config_path,
      config_dir,
    })
  }

  pub fn with_path(config_path: PathBuf) -> Self {
    let config_dir = config_path
      .parent()
      .map(|p| p.to_path_buf())
      .unwrap_or_else(|| PathBuf::from("."));

    Self {
      config_path,
      config_dir,
    }
  }

  pub async fn ensure_config_dir(&self) -> Result<()> {
    fs::create_dir_all(&self.config_dir).await.map_err(ClaudeCodeError::Io)?;
    Ok(())
  }
}

#[async_trait]
impl ConfigProvider for YamlConfigProvider {
  async fn load_config(&self) -> Result<Config> {
    if !self.config_path.exists() {
      debug!("Config file not found, returning default config");
      return Ok(Config::default());
    }

    let content = fs::read_to_string(&self.config_path).await.map_err(ClaudeCodeError::Io)?;

    let config: Config = serde_yaml
      ::from_str(&content)
      .map_err(|e| ClaudeCodeError::InvalidConfig(e.to_string()))?;

    debug!("Loaded configuration from {:?}", self.config_path);
    Ok(config)
  }

  async fn save_config(&self, config: &Config) -> Result<()> {
    self.ensure_config_dir().await?;

    let content = serde_yaml
      ::to_string(config)
      .map_err(|e| ClaudeCodeError::InvalidConfig(e.to_string()))?;

    fs::write(&self.config_path, content).await.map_err(ClaudeCodeError::Io)?;

    info!("Saved configuration to {:?}", self.config_path);
    Ok(())
  }

  async fn validate_config(&self, config: &Config) -> Result<()> {
    // Basic validation - could be extended with validation rules
    if config.daemon.log_level.is_empty() {
      return Err(ClaudeCodeError::InvalidConfig("log_level cannot be empty".to_string()));
    }

    if config.daemon.sync_delay_after_expiry == 0 {
      warn!("sync_delay_after_expiry is 0, which may cause rapid sync attempts");
    }

    for org in &config.github.organizations {
      if org.name.is_empty() {
        return Err(ClaudeCodeError::InvalidConfig("Organization name cannot be empty".to_string()));
      }
    }

    for repo in &config.github.repositories {
      if !repo.repo.contains('/') {
        return Err(
          ClaudeCodeError::InvalidConfig(format!("Invalid repository format: {}", repo.repo))
        );
      }
    }

    Ok(())
  }

  async fn config_exists(&self) -> Result<bool> {
    Ok(self.config_path.exists())
  }

  fn config_path(&self) -> Option<&Path> {
    Some(&self.config_path)
  }

  fn as_any(&self) -> &dyn std::any::Any {
    self
  }
}

/// High-level configuration manager
pub struct ConfigurationManager {
  provider: Box<dyn ConfigProvider>,
  cache: Option<Config>,
}

impl ConfigurationManager {
  pub fn new() -> Result<Self> {
    let provider = Box::new(YamlConfigProvider::new()?);
    Ok(Self {
      provider,
      cache: None,
    })
  }

  pub fn with_provider(provider: Box<dyn ConfigProvider>) -> Self {
    Self {
      provider,
      cache: None,
    }
  }

  pub fn with_yaml_provider() -> Result<Self> {
    Self::new()
  }

  /// Invalidate cache
  pub fn invalidate_cache(&mut self) {
    self.cache = None;
  }

  /// Get cached config or load from provider
  #[allow(dead_code)]
  async fn get_cached_config(&mut self) -> Result<&Config> {
    if self.cache.is_none() {
      let config = self.provider.load_config().await?;
      self.provider.validate_config(&config).await?;
      self.cache = Some(config);
    }

    Ok(self.cache.as_ref().unwrap())
  }
}

#[async_trait]
impl ConfigManagerTrait for ConfigurationManager {
  async fn initialize(&self) -> Result<Config> {
    let config = Config::default();
    self.provider.save_config(&config).await?;
    info!("Initialized configuration with defaults");
    Ok(config)
  }

  async fn load(&self) -> Result<Config> {
    if !self.provider.config_exists().await? {
      debug!("Config does not exist, initializing with defaults");
      return self.initialize().await;
    }

    self.provider.load_config().await
  }

  async fn save(&self, config: &Config) -> Result<()> {
    self.provider.validate_config(config).await?;
    self.provider.save_config(config).await
  }

  async fn update_section<T>(&self, _section: &str, _data: T) -> Result<()> where T: Send + Sync {
    // TODO: Implement section-specific updates
    // This would involve deserializing specific sections and merging
    todo!("Section updates not yet implemented")
  }

  async fn backup(&self) -> Result<String> {
    let _config = self.provider.load_config().await?;
    let timestamp = chrono::Utc::now().timestamp();
    let backup_id = format!("backup_{}", timestamp);

    // In a real implementation, this would save to a backup location
    // For now, just return the backup ID
    info!("Created config backup: {}", backup_id);
    Ok(backup_id)
  }

  async fn restore(&self, backup_id: &str) -> Result<()> {
    // TODO: Implement backup restoration
    info!("Restoring config from backup: {}", backup_id);
    // For now, just log the restoration attempt
    Ok(())
  }
}

// Convenience methods that match the expected API
impl ConfigurationManager {
  /// Convenience method that matches the expected API
  pub async fn load_config(&self) -> Result<Config> {
    self.load().await
  }

  /// Convenience method that matches the expected API
  pub async fn save_config(&self, config: &Config) -> Result<()> {
    self.save(config).await
  }

  /// Add an organization to the configuration
  pub async fn add_organization(&self, name: String) -> Result<()> {
    let mut config = self.load_config().await?;

    // Check if organization already exists
    if config.github.organizations.iter().any(|org| org.name == name) {
      return Err(ClaudeCodeError::Generic(format!("Organization '{}' already exists", name)));
    }

    config.github.organizations.push(crate::types::GitHubOrganization { name });

    self.save_config(&config).await
  }

  /// Remove an organization from the configuration
  pub async fn remove_organization(&self, name: &str) -> Result<()> {
    let mut config = self.load_config().await?;

    let original_len = config.github.organizations.len();
    config.github.organizations.retain(|org| org.name != name);

    if config.github.organizations.len() == original_len {
      return Err(ClaudeCodeError::Generic(format!("Organization '{}' not found", name)));
    }

    self.save_config(&config).await
  }

  /// Add a repository to the configuration
  pub async fn add_repository(&self, repo: String) -> Result<()> {
    let mut config = self.load_config().await?;

    // Check if repository already exists
    if config.github.repositories.iter().any(|r| r.repo == repo) {
      return Err(ClaudeCodeError::Generic(format!("Repository '{}' already exists", repo)));
    }

    config.github.repositories.push(crate::types::GitHubRepository { repo });

    self.save_config(&config).await
  }

  /// Remove a repository from the configuration
  pub async fn remove_repository(&self, repo: &str) -> Result<()> {
    let mut config = self.load_config().await?;

    let original_len = config.github.repositories.len();
    config.github.repositories.retain(|r| r.repo != repo);

    if config.github.repositories.len() == original_len {
      return Err(ClaudeCodeError::Generic(format!("Repository '{}' not found", repo)));
    }

    self.save_config(&config).await
  }

  /// Load state information (placeholder for now)
  pub async fn load_state(&self) -> Result<crate::types::SyncState> {
    // For now, return a default state
    // TODO: Implement proper state management
    Ok(crate::types::SyncState {
      last_sync: 0,
      last_token: String::new(),
      targets: vec![],
    })
  }

  /// Ensure configuration directory exists
  pub async fn ensure_config_dir(&self) -> Result<()> {
    if let Some(yaml_provider) = self.provider.as_any().downcast_ref::<YamlConfigProvider>() {
      yaml_provider.ensure_config_dir().await
    } else {
      Ok(())
    }
  }

  /// Get configuration file path
  pub fn config_path(&self) -> &Path {
    self.provider.config_path().unwrap_or_else(|| Path::new(""))
  }

  /// Get configuration directory path
  pub fn config_dir(&self) -> &Path {
    self
      .config_path()
      .parent()
      .unwrap_or_else(|| Path::new(""))
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::types::*;
  use tempfile::TempDir;

  fn create_test_config() -> Config {
    Config {
      daemon: DaemonConfig {
        log_level: "info".to_string(),
        sync_delay_after_expiry: 60,
      },
      github: GitHubConfig {
        organizations: vec![GitHubOrganization {
          name: "test-org".to_string(),
        }],
        repositories: vec![GitHubRepository {
          repo: "owner/repo".to_string(),
        }],
      },
      notifications: NotificationConfig {
        session_warnings: vec![30, 15, 5],
        sync_failures: true,
      },
      credentials: CredentialsConfig {
        file_path: "~/.config/claude/test_credentials.json".to_string(),
        json_path: "claudeAiOauth".to_string(),
        field_mappings: {
          let mut mappings = std::collections::HashMap::new();
          mappings.insert("accessToken".to_string(), "CLAUDE_ACCESS_TOKEN".to_string());
          mappings.insert("refreshToken".to_string(), "CLAUDE_REFRESH_TOKEN".to_string());
          mappings.insert("expiresAt".to_string(), "CLAUDE_EXPIRES_AT".to_string());
          mappings
        },
      },
    }
  }

  #[tokio::test]
  async fn test_yaml_provider_save_and_load() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("test_config.yml");
    let provider = YamlConfigProvider::with_path(config_path);

    let test_config = create_test_config();

    // Save config
    provider.save_config(&test_config).await.unwrap();

    // Load config
    let loaded_config = provider.load_config().await.unwrap();

    assert_eq!(loaded_config.daemon.log_level, test_config.daemon.log_level);
    assert_eq!(loaded_config.github.organizations.len(), 1);
    assert_eq!(loaded_config.github.organizations[0].name, "test-org");
  }

  #[tokio::test]
  async fn test_yaml_provider_load_nonexistent() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("nonexistent.yml");
    let provider = YamlConfigProvider::with_path(config_path);

    let config = provider.load_config().await.unwrap();

    // Should return default config
    assert_eq!(config.daemon.log_level, "info");
    assert!(config.github.organizations.is_empty());
  }

  #[tokio::test]
  async fn test_config_validation() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("test_config.yml");
    let provider = YamlConfigProvider::with_path(config_path);

    // Test valid config
    let valid_config = create_test_config();
    assert!(provider.validate_config(&valid_config).await.is_ok());

    // Test invalid config - empty log level
    let mut invalid_config = create_test_config();
    invalid_config.daemon.log_level = "".to_string();
    assert!(provider.validate_config(&invalid_config).await.is_err());

    // Test invalid config - bad repo format
    let mut invalid_config = create_test_config();
    invalid_config.github.repositories[0].repo = "invalid-repo".to_string();
    assert!(provider.validate_config(&invalid_config).await.is_err());
  }

  #[tokio::test]
  async fn test_configuration_manager() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("test_config.yml");
    let provider = Box::new(YamlConfigProvider::with_path(config_path));
    let manager = ConfigurationManager::with_provider(provider);

    // Test initialize
    let config = manager.initialize().await.unwrap();
    assert_eq!(config.daemon.log_level, "info");

    // Test load
    let loaded_config = manager.load().await.unwrap();
    assert_eq!(loaded_config.daemon.log_level, "info");

    // Test save
    let mut test_config = create_test_config();
    test_config.daemon.log_level = "debug".to_string();
    manager.save(&test_config).await.unwrap();

    let updated_config = manager.load().await.unwrap();
    assert_eq!(updated_config.daemon.log_level, "debug");
  }

  #[tokio::test]
  async fn test_backup_restore() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("test_config.yml");
    let provider = Box::new(YamlConfigProvider::with_path(config_path));
    let manager = ConfigurationManager::with_provider(provider);

    let config = create_test_config();
    manager.save(&config).await.unwrap();

    // Test backup
    let backup_id = manager.backup().await.unwrap();
    assert!(backup_id.starts_with("backup_"));

    // Test restore (currently just logs, doesn't fail)
    let result = manager.restore(&backup_id).await;
    // Should succeed for now (returns Ok(()))
    assert!(result.is_ok());
  }
}