style: 格式化代码并优化导入顺序

This commit is contained in:
2026-05-27 15:15:15 +08:00
parent b8182e7538
commit 90074e6e32
34 changed files with 2931 additions and 1648 deletions

View File

@@ -1,6 +1,8 @@
use super::{AppConfig, GitProfile, TokenConfig};
use crate::utils::keyring::{KeyringManager, get_default_base_url, get_default_model, provider_needs_api_key};
use anyhow::{bail, Context, Result};
use crate::utils::keyring::{
KeyringManager, get_default_base_url, get_default_model, provider_needs_api_key,
};
use anyhow::{Context, Result, bail};
// use std::collections::HashMap;
use std::path::{Path, PathBuf};
@@ -91,13 +93,13 @@ impl ConfigManager {
if !self.config.profiles.contains_key(name) {
bail!("Profile '{}' does not exist", name);
}
if self.config.default_profile.as_ref() == Some(&name.to_string()) {
self.config.default_profile = None;
}
self.config.repo_profiles.retain(|_, v| v != name);
self.config.profiles.remove(name);
self.modified = true;
Ok(())
@@ -137,9 +139,10 @@ impl ConfigManager {
/// Set default profile
pub fn set_default_profile(&mut self, name: Option<String>) -> Result<()> {
if let Some(ref n) = name
&& !self.config.profiles.contains_key(n) {
bail!("Profile '{}' does not exist", n);
}
&& !self.config.profiles.contains_key(n)
{
bail!("Profile '{}' does not exist", n);
}
self.config.default_profile = name;
self.modified = true;
Ok(())
@@ -177,36 +180,49 @@ impl ConfigManager {
// Token management
/// Add a token to a profile (stores token in keyring)
pub fn add_token_to_profile(&mut self, profile_name: &str, service: String, token: TokenConfig) -> Result<()> {
pub fn add_token_to_profile(
&mut self,
profile_name: &str,
service: String,
token: TokenConfig,
) -> Result<()> {
if !self.config.profiles.contains_key(profile_name) {
bail!("Profile '{}' does not exist", profile_name);
}
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
profile.add_token(service, token);
self.modified = true;
}
Ok(())
}
/// Store a PAT token in keyring for a profile
pub fn store_pat_for_profile(&self, profile_name: &str, service: &str, token_value: &str) -> Result<()> {
let profile = self.get_profile(profile_name)
pub fn store_pat_for_profile(
&self,
profile_name: &str,
service: &str,
token_value: &str,
) -> Result<()> {
let profile = self
.get_profile(profile_name)
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
let user_email = &profile.user_email;
self.keyring.store_pat(profile_name, user_email, service, token_value)
self.keyring
.store_pat(profile_name, user_email, service, token_value)
}
/// Get a PAT token from keyring for a profile
pub fn get_pat_for_profile(&self, profile_name: &str, service: &str) -> Result<Option<String>> {
let profile = self.get_profile(profile_name)
let profile = self
.get_profile(profile_name)
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
let user_email = &profile.user_email;
self.keyring.get_pat(profile_name, user_email, service)
}
@@ -225,21 +241,40 @@ impl ConfigManager {
if !self.config.profiles.contains_key(profile_name) {
bail!("Profile '{}' does not exist", profile_name);
}
let user_email = self.config.profiles.get(profile_name).unwrap().user_email.clone();
let services: Vec<String> = self.config.profiles.get(profile_name).unwrap().tokens.keys().cloned().collect();
let user_email = self
.config
.profiles
.get(profile_name)
.unwrap()
.user_email
.clone();
let services: Vec<String> = self
.config
.profiles
.get(profile_name)
.unwrap()
.tokens
.keys()
.cloned()
.collect();
if !services.contains(&service.to_string()) {
bail!("Token for service '{}' not found in profile '{}'", service, profile_name);
bail!(
"Token for service '{}' not found in profile '{}'",
service,
profile_name
);
}
self.keyring.delete_pat(profile_name, &user_email, service)?;
self.keyring
.delete_pat(profile_name, &user_email, service)?;
if let Some(profile) = self.config.profiles.get_mut(profile_name) {
profile.remove_token(service);
self.modified = true;
}
Ok(())
}
@@ -248,8 +283,9 @@ impl ConfigManager {
if let Some(profile) = self.get_profile(profile_name) {
let user_email = &profile.user_email;
let services: Vec<String> = profile.tokens.keys().cloned().collect();
self.keyring.delete_all_pats_for_profile(profile_name, user_email, &services)?;
self.keyring
.delete_all_pats_for_profile(profile_name, user_email, &services)?;
}
Ok(())
}
@@ -301,14 +337,24 @@ impl ConfigManager {
// }
/// Check and compare profile with git configuration
pub fn check_profile_config(&self, profile_name: &str, repo: &git2::Repository) -> Result<super::ProfileComparison> {
let profile = self.get_profile(profile_name)
pub fn check_profile_config(
&self,
profile_name: &str,
repo: &git2::Repository,
) -> Result<super::ProfileComparison> {
let profile = self
.get_profile(profile_name)
.ok_or_else(|| anyhow::anyhow!("Profile '{}' not found", profile_name))?;
profile.compare_with_git_config(repo)
}
/// Find a profile that matches the given user config (name, email, signing_key)
pub fn find_matching_profile(&self, user_name: &str, user_email: &str, signing_key: Option<&str>) -> Option<&GitProfile> {
pub fn find_matching_profile(
&self,
user_name: &str,
user_email: &str,
signing_key: Option<&str>,
) -> Option<&GitProfile> {
for profile in self.config.profiles.values() {
let name_match = profile.user_name == user_name;
let email_match = profile.user_email == user_email;
@@ -318,7 +364,7 @@ impl ConfigManager {
(Some(_), None) => false,
(None, Some(_)) => false,
};
if name_match && email_match && key_match {
return Some(profile);
}
@@ -328,7 +374,9 @@ impl ConfigManager {
/// Find profiles that partially match (same name or same email)
pub fn find_partial_matches(&self, user_name: &str, user_email: &str) -> Vec<&GitProfile> {
self.config.profiles.values()
self.config
.profiles
.values()
.filter(|p| p.user_name == user_name || p.user_email == user_email)
.collect()
}
@@ -383,7 +431,11 @@ impl ConfigManager {
/// Get API key from configured storage method
pub fn get_api_key(&self) -> Option<String> {
// First try environment variables (always checked)
if let Some(key) = self.keyring.get_api_key(&self.config.llm.provider).unwrap_or(None) {
if let Some(key) = self
.keyring
.get_api_key(&self.config.llm.provider)
.unwrap_or(None)
{
return Some(key);
}
@@ -400,20 +452,29 @@ impl ConfigManager {
match self.config.llm.api_key_storage.as_str() {
"keyring" => {
if !self.keyring.is_available() {
bail!("Keyring is not available. Set QUICOMMIT_API_KEY environment variable instead or change api_key_storage to 'config'.");
bail!(
"Keyring is not available. Set QUICOMMIT_API_KEY environment variable instead or change api_key_storage to 'config'."
);
}
self.keyring.store_api_key(&self.config.llm.provider, api_key)
},
self.keyring
.store_api_key(&self.config.llm.provider, api_key)
}
"config" => {
// We can't modify self.config here since self is immutable
// This will be handled by the caller updating the config
Ok(())
},
}
"environment" => {
bail!("API key storage set to 'environment'. Please set QUICOMMIT_{}_API_KEY environment variable.", self.config.llm.provider.to_uppercase());
},
bail!(
"API key storage set to 'environment'. Please set QUICOMMIT_{}_API_KEY environment variable.",
self.config.llm.provider.to_uppercase()
);
}
_ => {
bail!("Invalid API key storage method: {}", self.config.llm.api_key_storage);
bail!(
"Invalid API key storage method: {}",
self.config.llm.api_key_storage
);
}
}
}
@@ -425,16 +486,19 @@ impl ConfigManager {
if self.keyring.is_available() {
self.keyring.delete_api_key(&self.config.llm.provider)?;
}
},
}
"config" => {
// We can't modify self.config here since self is immutable
// This will be handled by the caller updating the config
},
}
"environment" => {
// Environment variables are not managed by the app
},
}
_ => {
bail!("Invalid API key storage method: {}", self.config.llm.api_key_storage);
bail!(
"Invalid API key storage method: {}",
self.config.llm.api_key_storage
);
}
}
Ok(())
@@ -447,7 +511,12 @@ impl ConfigManager {
}
// Check environment variables
if self.keyring.get_api_key(&self.config.llm.provider).unwrap_or(None).is_some() {
if self
.keyring
.get_api_key(&self.config.llm.provider)
.unwrap_or(None)
.is_some()
{
return true;
}
@@ -467,19 +536,19 @@ impl ConfigManager {
// /// Configure LLM provider with all settings
// pub fn configure_llm(&mut self, provider: String, model: Option<String>, base_url: Option<String>, api_key: Option<&str>) -> Result<()> {
// self.set_llm_provider(provider.clone());
// if let Some(m) = model {
// self.set_llm_model(m);
// }
// self.set_llm_base_url(base_url);
// if let Some(key) = api_key {
// if provider_needs_api_key(&provider) {
// self.set_api_key(key)?;
// }
// }
// Ok(())
// }
@@ -575,14 +644,12 @@ impl ConfigManager {
/// Export configuration to TOML string
pub fn export(&self) -> Result<String> {
toml::to_string_pretty(&self.config)
.context("Failed to serialize config")
toml::to_string_pretty(&self.config).context("Failed to serialize config")
}
/// Import configuration from TOML string
pub fn import(&mut self, toml_str: &str) -> Result<()> {
self.config = toml::from_str(toml_str)
.context("Failed to parse config")?;
self.config = toml::from_str(toml_str).context("Failed to parse config")?;
self.modified = true;
Ok(())
}