agpm-cli 0.4.14

AGent Package Manager - A Git-based package manager for coding agents
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
//! Global configuration management for AGPM.
//!
//! This module handles the global user configuration file (`~/.agpm/config.toml`) which stores
//! user-wide settings including authentication tokens for private repositories. The global
//! configuration provides a secure way to manage credentials without exposing them in
//! version-controlled project files.
//!
//! # Security Model
//!
//! The global configuration is designed with security as a primary concern:
//!
//! - **Credential Isolation**: Authentication tokens are stored only in the global config
//! - **Never Committed**: Global config is never part of version control
//! - **User-Specific**: Each user maintains their own global configuration
//! - **Platform-Secure**: Uses platform-appropriate secure storage locations
//!
//! # Configuration File Location
//!
//! The global configuration file is stored in platform-specific locations:
//!
//! - **Unix/macOS**: `~/.agpm/config.toml`
//! - **Windows**: `%LOCALAPPDATA%\agpm\config.toml`
//!
//! The location can be overridden using the `AGPM_CONFIG_PATH` environment variable.
//!
//! # File Format
//!
//! The global configuration uses TOML format:
//!
//! ```toml
//! # Global sources with authentication (never commit this file!)
//! [sources]
//! # GitHub with personal access token
//! private = "https://oauth2:ghp_xxxxxxxxxxxx@github.com/company/private-agpm.git"
//!
//! # GitLab with deploy token
//! enterprise = "https://gitlab-ci-token:token123@gitlab.company.com/ai/resources.git"
//!
//! # SSH-based authentication
//! internal = "git@internal.company.com:team/agpm-resources.git"
//!
//! # Basic authentication (not recommended)
//! legacy = "https://username:password@old-server.com/repo.git"
//! ```
//!
//! # Authentication Methods
//!
//! Supported authentication methods for Git repositories:
//!
//! ## GitHub Personal Access Token (Recommended)
//! ```text
//! https://oauth2:ghp_xxxxxxxxxxxx@github.com/owner/repo.git
//! ```
//!
//! ## GitLab Deploy Token
//! ```text
//! https://gitlab-ci-token:token@gitlab.com/group/repo.git
//! ```
//!
//! ## SSH Keys
//! ```text
//! git@github.com:owner/repo.git
//! ```
//!
//! ## Basic Authentication (Not Recommended)
//! ```text
//! https://username:password@server.com/repo.git
//! ```
//!
//! # Source Resolution Priority
//!
//! When resolving sources, AGPM follows this priority order:
//!
//! 1. **Global sources** from `~/.agpm/config.toml` (loaded first)
//! 2. **Project sources** from `agpm.toml` (can override global sources)
//!
//! This allows teams to share public sources in `agpm.toml` while keeping
//! authentication tokens private in individual global configurations.
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```rust,no_run
//! use agpm_cli::config::GlobalConfig;
//!
//! # async fn example() -> anyhow::Result<()> {
//! // Load existing configuration or create default
//! let mut config = GlobalConfig::load().await?;
//!
//! // Add authenticated source
//! config.add_source(
//!     "private".to_string(),
//!     "https://oauth2:token@github.com/company/repo.git".to_string()
//! );
//!
//! // Save changes
//! config.save().await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Using Configuration Manager
//!
//! ```rust,no_run
//! use agpm_cli::config::GlobalConfigManager;
//!
//! # async fn example() -> anyhow::Result<()> {
//! let mut manager = GlobalConfigManager::new()?;
//!
//! // Get configuration with caching
//! let config = manager.get().await?;
//! println!("Found {} global sources", config.sources.len());
//!
//! // Modify configuration
//! let config = manager.get_mut().await?;
//! config.add_source("new".to_string(), "https://example.com/repo.git".to_string());
//!
//! // Save changes
//! manager.save().await?;
//! # Ok(())
//! # }
//! ```

use crate::core::file_error::LARGE_FILE_SIZE;
use crate::upgrade::config::UpgradeConfig;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::fs;

/// Default maximum file size for operations that read/embed files.
///
/// Default: 1 MB (1,048,576 bytes)
///
/// This limit prevents memory exhaustion when reading files. Currently used by:
/// - Template content filter for embedding project files
///
/// Future uses may include:
/// - General file reading operations
/// - Resource validation
/// - Content processing
const fn default_max_content_file_size() -> u64 {
    LARGE_FILE_SIZE as u64
}

/// Default token warning threshold.
///
/// Default: 100,000 tokens - Claude's context window is ~200k, so warning at 50% is sensible.
const fn default_token_warning_threshold() -> u64 {
    100_000
}

fn is_default_token_warning_threshold(value: &u64) -> bool {
    *value == default_token_warning_threshold()
}

/// Global configuration structure for AGPM.
///
/// This structure represents the global user configuration file stored at `~/.agpm/config.toml`.
/// It contains user-wide settings including authentication credentials for private Git repositories.
///
/// # Security Considerations
///
/// - **Never commit** this configuration to version control
/// - Store **only** in the user's home directory or secure location
/// - Contains **sensitive data** like authentication tokens
/// - Should have **restricted file permissions** (600 on Unix systems)
///
/// # Structure
///
/// Currently contains only source definitions, but designed to be extensible
/// for future configuration options like:
/// - Default author information
/// - Preferred Git configuration
/// - Cache settings
/// - Proxy configuration
///
/// # Examples
///
/// ```rust,no_run
/// use agpm_cli::config::GlobalConfig;
/// use std::collections::HashMap;
///
/// // Create new configuration
/// let mut config = GlobalConfig::default();
///
/// // Add authenticated source
/// config.add_source(
///     "company".to_string(),
///     "https://oauth2:token@github.com/company/agpm.git".to_string()
/// );
///
/// assert!(config.has_source("company"));
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GlobalConfig {
    /// Global Git repository sources with optional authentication.
    ///
    /// Maps source names to Git repository URLs. These URLs may contain authentication
    /// credentials and are kept separate from project manifests for security.
    ///
    /// # Authentication URL Formats
    ///
    /// - `https://oauth2:token@github.com/owner/repo.git` - GitHub personal access token
    /// - `https://gitlab-ci-token:token@gitlab.com/group/repo.git` - GitLab deploy token
    /// - `git@github.com:owner/repo.git` - SSH authentication
    /// - `https://user:pass@server.com/repo.git` - Basic auth (not recommended)
    ///
    /// # Security Notes
    ///
    /// - URLs with credentials are **never** logged in plain text
    /// - The `sources` field is skipped during serialization if empty
    /// - Authentication details should use tokens rather than passwords when possible
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub sources: HashMap<String, String>,

    /// Upgrade configuration settings.
    ///
    /// Controls the behavior of the self-upgrade functionality including
    /// update checks, backup preferences, and verification settings.
    #[serde(default, skip_serializing_if = "is_default_upgrade_config")]
    pub upgrade: UpgradeConfig,

    /// Maximum file size in bytes for file operations.
    ///
    /// Default: 1 MB (1,048,576 bytes)
    ///
    /// This limit prevents memory exhaustion when reading or embedding files.
    /// Currently used by template content filter, may be used by other operations in the future.
    ///
    /// # Configuration
    ///
    /// Set in `~/.agpm/config.toml`:
    /// ```toml
    /// max_content_file_size = 2097152  # 2 MB
    /// ```
    #[serde(
        default = "default_max_content_file_size",
        skip_serializing_if = "is_default_max_content_file_size"
    )]
    pub max_content_file_size: u64,

    /// Token count warning threshold.
    ///
    /// Default: 100,000 tokens (Claude's context window is ~200k)
    ///
    /// Resources exceeding this threshold will emit a warning during installation.
    /// This helps identify resources that may consume significant context.
    ///
    /// # Configuration
    ///
    /// Set in `~/.agpm/config.toml`:
    /// ```toml
    /// token_warning_threshold = 50000  # 50k tokens
    /// ```
    #[serde(
        default = "default_token_warning_threshold",
        skip_serializing_if = "is_default_token_warning_threshold"
    )]
    pub token_warning_threshold: u64,
}

fn is_default_max_content_file_size(size: &u64) -> bool {
    *size == default_max_content_file_size()
}

const fn is_default_upgrade_config(config: &UpgradeConfig) -> bool {
    // Skip serializing if it's the default config
    !config.check_on_startup
        && config.check_interval == 86400
        && config.auto_backup
        && config.verify_checksum
}

impl GlobalConfig {
    /// Load global configuration from the default platform-specific location.
    ///
    /// Attempts to load the configuration file from the default path. If the file
    /// doesn't exist, returns a default (empty) configuration instead of an error.
    ///
    /// # Default Locations
    ///
    /// - **Unix/macOS**: `~/.agpm/config.toml`
    /// - **Windows**: `%LOCALAPPDATA%\agpm\config.toml`
    /// - **Override**: Set `AGPM_CONFIG_PATH` environment variable
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let config = GlobalConfig::load().await?;
    /// println!("Loaded {} global sources", config.sources.len());
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The default path cannot be determined
    /// - The file exists but cannot be read
    /// - The file contains invalid TOML syntax
    pub async fn load() -> Result<Self> {
        let path = Self::default_path()?;
        if path.exists() {
            Self::load_from(&path).await
        } else {
            Ok(Self::default())
        }
    }

    /// Load global configuration from an optional path.
    ///
    /// If a path is provided, loads from that path. Otherwise, loads from the
    /// default location (`~/.agpm/config.toml` or platform equivalent).
    ///
    /// # Parameters
    ///
    /// - `path`: Optional path to the configuration file
    ///
    /// # Returns
    ///
    /// Returns the loaded configuration or a default configuration if the file
    /// doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file exists but cannot be read
    /// - The file contains invalid TOML syntax
    pub async fn load_with_optional(path: Option<PathBuf>) -> Result<Self> {
        let path = path.unwrap_or_else(|| {
            Self::default_path().unwrap_or_else(|_| PathBuf::from("~/.agpm/config.toml"))
        });
        if path.exists() {
            Self::load_from(&path).await
        } else {
            Ok(Self::default())
        }
    }

    /// Load global configuration from a specific file path.
    ///
    /// This method is primarily used for testing or when a custom configuration
    /// location is needed.
    ///
    /// # Parameters
    ///
    /// - `path`: Path to the configuration file to load
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    /// use std::path::Path;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let config = GlobalConfig::load_from(Path::new("/custom/config.toml")).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file cannot be read (permissions, not found, etc.)
    /// - The file contains invalid TOML syntax
    /// - The TOML structure doesn't match the expected schema
    pub async fn load_from(path: &Path) -> Result<Self> {
        let content = fs::read_to_string(path)
            .await
            .with_context(|| format!("Failed to read global config from {}", path.display()))?;

        toml::from_str(&content)
            .with_context(|| format!("Failed to parse global config from {}", path.display()))
    }

    /// Save global configuration to the default platform-specific location.
    ///
    /// Saves the current configuration to the default path, creating parent
    /// directories as needed. The file is written atomically to prevent
    /// corruption during the write process.
    ///
    /// # File Permissions
    ///
    /// The configuration file should be created with restricted permissions
    /// since it may contain sensitive authentication tokens.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let mut config = GlobalConfig::load().await?;
    /// config.add_source(
    ///     "new".to_string(),
    ///     "https://github.com/owner/repo.git".to_string()
    /// );
    /// config.save().await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The default path cannot be determined
    /// - Parent directories cannot be created
    /// - The file cannot be written (permissions, disk space, etc.)
    /// - Serialization to TOML fails
    pub async fn save(&self) -> Result<()> {
        let path = Self::default_path()?;
        self.save_to(&path).await
    }

    /// Save global configuration to a specific file path.
    ///
    /// Creates parent directories as needed and writes the configuration
    /// as pretty-formatted TOML.
    ///
    /// # Parameters
    ///
    /// - `path`: Path where the configuration file should be saved
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    /// use std::path::Path;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let config = GlobalConfig::default();
    /// config.save_to(Path::new("/tmp/test-config.toml")).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Parent directories cannot be created
    /// - The file cannot be written
    /// - Serialization to TOML fails
    pub async fn save_to(&self, path: &Path) -> Result<()> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).await.with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }

        let content = toml::to_string_pretty(self).context("Failed to serialize global config")?;

        fs::write(path, content)
            .await
            .with_context(|| format!("Failed to write global config to {}", path.display()))?;

        // Set restrictive permissions on Unix systems to protect credentials
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            use tokio::fs as async_fs;

            let mut perms = async_fs::metadata(path)
                .await
                .with_context(|| format!("Failed to read permissions for {}", path.display()))?
                .permissions();
            perms.set_mode(0o600); // Owner read/write only, no group/other access
            async_fs::set_permissions(path, perms).await.with_context(|| {
                format!("Failed to set secure permissions on {}", path.display())
            })?;
        }

        Ok(())
    }

    /// Get the default file path for global configuration.
    ///
    /// Returns the platform-appropriate path for storing global configuration.
    /// This location is chosen to be secure and follow platform conventions.
    ///
    /// # Path Resolution
    ///
    /// - **Windows**: `%LOCALAPPDATA%\agpm\config.toml`
    /// - **Unix/macOS**: `~/.agpm/config.toml`
    ///
    /// Note: Environment variable overrides are deprecated. Use the `load_with_optional()`
    /// method with an explicit path instead for better thread safety.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// # fn example() -> anyhow::Result<()> {
    /// let path = GlobalConfig::default_path()?;
    /// println!("Global config path: {}", path.display());
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The home directory cannot be determined
    /// - The local data directory cannot be determined (Windows)
    pub fn default_path() -> Result<PathBuf> {
        let config_dir = if cfg!(target_os = "windows") {
            dirs::data_local_dir()
                .ok_or_else(|| anyhow::anyhow!("Unable to determine local data directory"))?
                .join("agpm")
        } else {
            dirs::home_dir()
                .ok_or_else(|| anyhow::anyhow!("Unable to determine home directory"))?
                .join(".agpm")
        };

        Ok(config_dir.join("config.toml"))
    }

    /// Merge global sources with project manifest sources.
    ///
    /// Combines the global configuration sources with sources from a project manifest,
    /// with project sources taking precedence over global sources. This allows users
    /// to maintain private authentication in global config while projects can override
    /// with public sources.
    ///
    /// # Merge Strategy
    ///
    /// 1. Start with all global sources (may include authentication)
    /// 2. Add/override with local sources from project manifest
    /// 3. Local sources win in case of name conflicts
    ///
    /// # Parameters
    ///
    /// - `local_sources`: Sources from project manifest (`agpm.toml`)
    ///
    /// # Returns
    ///
    /// Combined source map with local sources taking precedence.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    /// use std::collections::HashMap;
    ///
    /// let mut global = GlobalConfig::default();
    /// global.add_source(
    ///     "private".to_string(),
    ///     "https://token@private.com/repo.git".to_string()
    /// );
    ///
    /// let mut local = HashMap::new();
    /// local.insert(
    ///     "public".to_string(),
    ///     "https://github.com/public/repo.git".to_string()
    /// );
    ///
    /// let merged = global.merge_sources(&local);
    /// assert_eq!(merged.len(), 2);
    /// assert!(merged.contains_key("private"));
    /// assert!(merged.contains_key("public"));
    /// ```
    ///
    /// # Security Note
    ///
    /// The merged result may contain authentication credentials from global sources.
    /// Handle with care and never log or expose in version control.
    #[must_use]
    pub fn merge_sources(
        &self,
        local_sources: &HashMap<String, String>,
    ) -> HashMap<String, String> {
        let mut merged = self.sources.clone();

        // Local sources override global ones
        for (name, url) in local_sources {
            merged.insert(name.clone(), url.clone());
        }

        merged
    }

    /// Add or update a source in the global configuration.
    ///
    /// Adds a new source or updates an existing one with the given name and URL.
    /// The URL may contain authentication credentials.
    ///
    /// # Parameters
    ///
    /// - `name`: Unique name for the source (used in manifests)
    /// - `url`: Git repository URL, optionally with authentication
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// let mut config = GlobalConfig::default();
    ///
    /// // Add source with authentication
    /// config.add_source(
    ///     "private".to_string(),
    ///     "https://oauth2:token@github.com/company/repo.git".to_string()
    /// );
    ///
    /// // Update existing source
    /// config.add_source(
    ///     "private".to_string(),
    ///     "git@github.com:company/repo.git".to_string()
    /// );
    ///
    /// assert!(config.has_source("private"));
    /// ```
    ///
    /// # Security Note
    ///
    /// URLs containing credentials should use tokens rather than passwords when possible.
    pub fn add_source(&mut self, name: String, url: String) {
        self.sources.insert(name, url);
    }

    /// Remove a source from the global configuration.
    ///
    /// Removes the source with the given name if it exists.
    ///
    /// # Parameters
    ///
    /// - `name`: Name of the source to remove
    ///
    /// # Returns
    ///
    /// - `true` if the source was found and removed
    /// - `false` if the source didn't exist
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// let mut config = GlobalConfig::default();
    /// config.add_source("test".to_string(), "https://example.com/repo.git".to_string());
    ///
    /// assert!(config.remove_source("test"));
    /// assert!(!config.remove_source("test")); // Already removed
    /// ```
    pub fn remove_source(&mut self, name: &str) -> bool {
        self.sources.remove(name).is_some()
    }

    /// Check if a source exists in the global configuration.
    ///
    /// # Parameters
    ///
    /// - `name`: Name of the source to check
    ///
    /// # Returns
    ///
    /// - `true` if the source exists
    /// - `false` if the source doesn't exist
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// let mut config = GlobalConfig::default();
    /// assert!(!config.has_source("test"));
    ///
    /// config.add_source("test".to_string(), "https://example.com/repo.git".to_string());
    /// assert!(config.has_source("test"));
    /// ```
    #[must_use]
    pub fn has_source(&self, name: &str) -> bool {
        self.sources.contains_key(name)
    }

    /// Get a source URL by name.
    ///
    /// Returns a reference to the URL for the specified source name.
    ///
    /// # Parameters
    ///
    /// - `name`: Name of the source to retrieve
    ///
    /// # Returns
    ///
    /// - `Some(&String)` with the URL if the source exists
    /// - `None` if the source doesn't exist
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// let mut config = GlobalConfig::default();
    /// config.add_source(
    ///     "test".to_string(),
    ///     "https://example.com/repo.git".to_string()
    /// );
    ///
    /// assert_eq!(
    ///     config.get_source("test"),
    ///     Some(&"https://example.com/repo.git".to_string())
    /// );
    /// assert_eq!(config.get_source("missing"), None);
    /// ```
    ///
    /// # Security Note
    ///
    /// The returned URL may contain authentication credentials. Handle with care.
    #[must_use]
    pub fn get_source(&self, name: &str) -> Option<&String> {
        self.sources.get(name)
    }

    /// Create a global configuration with example content.
    ///
    /// Creates a new configuration populated with example sources to demonstrate
    /// the expected format. Useful for initial setup or documentation.
    ///
    /// # Returns
    ///
    /// A new [`GlobalConfig`] with example private source configuration.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfig;
    ///
    /// let config = GlobalConfig::init_example();
    /// assert!(config.has_source("private"));
    ///
    /// // The example uses a placeholder token
    /// let url = config.get_source("private").unwrap();
    /// assert!(url.contains("YOUR_TOKEN"));
    /// ```
    ///
    /// # Note
    ///
    /// The example configuration contains placeholder values that must be
    /// replaced with actual authentication credentials before use.
    #[must_use]
    pub fn init_example() -> Self {
        let mut sources = HashMap::new();
        sources.insert(
            "private".to_string(),
            "https://oauth2:YOUR_TOKEN@github.com/yourcompany/private-agpm.git".to_string(),
        );

        Self {
            sources,
            upgrade: UpgradeConfig::default(),
            max_content_file_size: default_max_content_file_size(),
            token_warning_threshold: default_token_warning_threshold(),
        }
    }
}

/// Configuration manager with caching for global configuration.
///
/// Provides a higher-level interface for working with global configuration
/// that includes caching to avoid repeated file I/O operations. This is
/// particularly useful in command-line applications that may access
/// configuration multiple times.
///
/// # Features
///
/// - **Lazy Loading**: Configuration is loaded only when first accessed
/// - **Caching**: Subsequent accesses use the cached configuration
/// - **Reload Support**: Can reload from disk when needed
/// - **Custom Paths**: Supports custom configuration file paths for testing
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust,no_run
/// use agpm_cli::config::GlobalConfigManager;
///
/// # async fn example() -> anyhow::Result<()> {
/// let mut manager = GlobalConfigManager::new()?;
///
/// // First access loads from disk
/// let config = manager.get().await?;
/// println!("Found {} sources", config.sources.len());
///
/// // Subsequent accesses use cache
/// let config2 = manager.get().await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Modifying Configuration
///
/// ```rust,no_run
/// use agpm_cli::config::GlobalConfigManager;
///
/// # async fn example() -> anyhow::Result<()> {
/// let mut manager = GlobalConfigManager::new()?;
///
/// // Get mutable reference
/// let config = manager.get_mut().await?;
/// config.add_source(
///     "new".to_string(),
///     "https://github.com/owner/repo.git".to_string()
/// );
///
/// // Save changes to disk
/// manager.save().await?;
/// # Ok(())
/// # }
/// ```
pub struct GlobalConfigManager {
    config: Option<GlobalConfig>,
    path: PathBuf,
}

impl GlobalConfigManager {
    /// Create a new configuration manager using the default global config path.
    ///
    /// The manager will use the platform-appropriate default location for
    /// the global configuration file.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfigManager;
    ///
    /// # fn example() -> anyhow::Result<()> {
    /// let manager = GlobalConfigManager::new()?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the default configuration path cannot be determined.
    pub fn new() -> Result<Self> {
        Ok(Self {
            config: None,
            path: GlobalConfig::default_path()?,
        })
    }

    /// Create a configuration manager with a custom file path.
    ///
    /// This method is primarily useful for testing or when you need to
    /// use a non-standard location for the configuration file.
    ///
    /// # Parameters
    ///
    /// - `path`: Custom path for the configuration file
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfigManager;
    /// use std::path::PathBuf;
    ///
    /// let manager = GlobalConfigManager::with_path(PathBuf::from("/tmp/test.toml"));
    /// ```
    #[must_use]
    pub const fn with_path(path: PathBuf) -> Self {
        Self {
            config: None,
            path,
        }
    }

    /// Get a reference to the global configuration, loading it if necessary.
    ///
    /// If the configuration hasn't been loaded yet, this method will load it
    /// from disk. Subsequent calls will return the cached configuration.
    ///
    /// # Returns
    ///
    /// A reference to the cached [`GlobalConfig`].
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfigManager;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let mut manager = GlobalConfigManager::new()?;
    /// let config = manager.get().await?;
    /// println!("Global config has {} sources", config.sources.len());
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The configuration file exists but cannot be read
    /// - The configuration file contains invalid TOML syntax
    pub async fn get(&mut self) -> Result<&GlobalConfig> {
        if self.config.is_none() {
            self.config = Some(if self.path.exists() {
                GlobalConfig::load_from(&self.path).await?
            } else {
                GlobalConfig::default()
            });
        }

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

    /// Get a mutable reference to the global configuration, loading it if necessary.
    ///
    /// Similar to [`get`](Self::get), but returns a mutable reference allowing
    /// modification of the configuration.
    ///
    /// # Returns
    ///
    /// A mutable reference to the cached [`GlobalConfig`].
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfigManager;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let mut manager = GlobalConfigManager::new()?;
    /// let config = manager.get_mut().await?;
    ///
    /// config.add_source(
    ///     "new".to_string(),
    ///     "https://github.com/owner/repo.git".to_string()
    /// );
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The configuration file exists but cannot be read
    /// - The configuration file contains invalid TOML syntax
    pub async fn get_mut(&mut self) -> Result<&mut GlobalConfig> {
        if self.config.is_none() {
            self.config = Some(if self.path.exists() {
                GlobalConfig::load_from(&self.path).await?
            } else {
                GlobalConfig::default()
            });
        }

        Ok(self.config.as_mut().unwrap())
    }

    /// Save the current cached configuration to disk.
    ///
    /// Writes the current configuration state to the file system. If no
    /// configuration has been loaded, this method does nothing.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfigManager;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let mut manager = GlobalConfigManager::new()?;
    ///
    /// // Modify configuration
    /// let config = manager.get_mut().await?;
    /// config.add_source("test".to_string(), "https://test.com/repo.git".to_string());
    ///
    /// // Save changes
    /// manager.save().await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file cannot be written (permissions, disk space, etc.)
    /// - Parent directories cannot be created
    pub async fn save(&self) -> Result<()> {
        if let Some(config) = &self.config {
            config.save_to(&self.path).await?;
        }
        Ok(())
    }

    /// Reload the configuration from disk, discarding cached data.
    ///
    /// Forces a reload of the configuration from the file system, discarding
    /// any cached data. Useful when the configuration file may have been
    /// modified externally.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::config::GlobalConfigManager;
    ///
    /// # async fn example() -> anyhow::Result<()> {
    /// let mut manager = GlobalConfigManager::new()?;
    ///
    /// // Load initial configuration
    /// let config1 = manager.get().await?;
    /// let count1 = config1.sources.len();
    ///
    /// // Configuration file modified externally...
    ///
    /// // Reload to pick up external changes
    /// manager.reload().await?;
    /// let config2 = manager.get().await?;
    /// let count2 = config2.sources.len();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The configuration file exists but cannot be read
    /// - The configuration file contains invalid TOML syntax
    pub async fn reload(&mut self) -> Result<()> {
        self.config = Some(if self.path.exists() {
            GlobalConfig::load_from(&self.path).await?
        } else {
            GlobalConfig::default()
        });
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_global_config_default() {
        let config = GlobalConfig::default();
        assert!(config.sources.is_empty());
    }

    #[tokio::test]
    async fn test_global_config_save_load() {
        let temp = TempDir::new().unwrap();
        let config_path = temp.path().join("config.toml");

        let mut config = GlobalConfig::default();
        config.add_source("test".to_string(), "https://example.com/repo.git".to_string());

        config.save_to(&config_path).await.unwrap();

        let loaded = GlobalConfig::load_from(&config_path).await.unwrap();
        assert_eq!(loaded.sources.len(), 1);
        assert_eq!(loaded.get_source("test"), Some(&"https://example.com/repo.git".to_string()));
    }

    #[tokio::test]
    async fn test_merge_sources() {
        let mut global = GlobalConfig::default();
        global.add_source("private".to_string(), "https://token@private.com/repo.git".to_string());
        global.add_source("shared".to_string(), "https://shared.com/repo.git".to_string());

        let mut local = HashMap::new();
        local.insert("shared".to_string(), "https://override.com/repo.git".to_string());
        local.insert("public".to_string(), "https://public.com/repo.git".to_string());

        let merged = global.merge_sources(&local);

        // Global source preserved
        assert_eq!(merged.get("private"), Some(&"https://token@private.com/repo.git".to_string()));

        // Local override wins
        assert_eq!(merged.get("shared"), Some(&"https://override.com/repo.git".to_string()));

        // Local-only source included
        assert_eq!(merged.get("public"), Some(&"https://public.com/repo.git".to_string()));
    }

    #[tokio::test]
    async fn test_source_operations() {
        let mut config = GlobalConfig::default();

        // Add source
        config.add_source("test".to_string(), "https://test.com/repo.git".to_string());
        assert!(config.has_source("test"));
        assert_eq!(config.get_source("test"), Some(&"https://test.com/repo.git".to_string()));

        // Update source
        config.add_source("test".to_string(), "https://updated.com/repo.git".to_string());
        assert_eq!(config.get_source("test"), Some(&"https://updated.com/repo.git".to_string()));

        // Remove source
        assert!(config.remove_source("test"));
        assert!(!config.has_source("test"));
        assert!(!config.remove_source("test")); // Already removed
    }

    #[tokio::test]
    async fn test_init_example() {
        let config = GlobalConfig::init_example();

        assert!(config.has_source("private"));
        assert_eq!(
            config.get_source("private"),
            Some(&"https://oauth2:YOUR_TOKEN@github.com/yourcompany/private-agpm.git".to_string())
        );
    }

    #[tokio::test]
    #[cfg(unix)]
    async fn test_config_file_permissions() {
        use std::os::unix::fs::PermissionsExt;
        let temp_dir = tempfile::tempdir().unwrap();
        let config_path = temp_dir.path().join("test-config.toml");

        // Create and save config
        let config = GlobalConfig::default();
        config.save_to(&config_path).await.unwrap();

        // Check permissions
        let metadata = tokio::fs::metadata(&config_path).await.unwrap();
        let permissions = metadata.permissions();
        let mode = permissions.mode() & 0o777; // Get only permission bits

        assert_eq!(mode, 0o600, "Config file should have 600 permissions");
    }

    #[tokio::test]
    async fn test_config_manager() {
        let temp = TempDir::new().unwrap();
        let config_path = temp.path().join("config.toml");

        let mut manager = GlobalConfigManager::with_path(config_path.clone());

        // Get config (should create default)
        let config = manager.get_mut().await.unwrap();
        config.add_source("test".to_string(), "https://test.com/repo.git".to_string());

        // Save
        manager.save().await.unwrap();

        // Create new manager and verify it loads
        let mut manager2 = GlobalConfigManager::with_path(config_path);
        let config2 = manager2.get().await.unwrap();
        assert!(config2.has_source("test"));
    }
}