Skip to content

Commit 40830ab

Browse files
rpendletonclaude
andcommitted
perf(config): add get_secret for non-cloning single-key lookup
Avoid cloning the entire secrets map in `set` just to read the existing secret's provider. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 98f1519 commit 40830ab

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/commands/set.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ impl SetCommand {
9090
let provider_name_to_use = if let Some(ref provider_name) = self.provider {
9191
Some(provider_name.clone())
9292
} else if let Some(existing) = config
93-
.get_secrets(&profile)?
94-
.get(&self.key)
93+
.get_secret(&profile, &self.key)
9594
.and_then(|s| s.provider().map(str::to_string))
9695
{
9796
Some(existing)

src/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,26 @@ impl Config {
10521052
Ok(secrets)
10531053
}
10541054

1055+
/// Look up a single secret by key without cloning the secrets map.
1056+
///
1057+
/// Mirrors the precedence used by [`Self::get_secrets`]: profile-specific
1058+
/// secrets take precedence, falling back to top-level secrets unless
1059+
/// `no_defaults` is set.
1060+
pub fn get_secret(&self, profile: &str, key: &str) -> Option<&SecretConfig> {
1061+
if profile != "default"
1062+
&& let Some(profile_config) = self.profiles.get(profile)
1063+
&& let Some(secret) = profile_config.secrets.get(key)
1064+
{
1065+
return Some(secret);
1066+
}
1067+
1068+
if profile != "default" && Settings::get().no_defaults {
1069+
return None;
1070+
}
1071+
1072+
self.secrets.get(key)
1073+
}
1074+
10551075
/// Get effective secrets (default or profile, mutable)
10561076
pub fn get_secrets_mut(&mut self, profile: &str) -> &mut IndexMap<String, SecretConfig> {
10571077
if profile == "default" {

0 commit comments

Comments
 (0)