refactor: main.rs resolve_oauth_client uses new dispatcher
This commit is contained in:
+5
-16
@@ -600,25 +600,12 @@ pub fn get_oauth_provider_for_client(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_provider_type(client_name: &str, clients: &[ClientConfig]) -> Option<&'static str> {
|
|
||||||
for client_config in clients {
|
|
||||||
let (config_name, provider_type, auth) = client_config_info(client_config);
|
|
||||||
if config_name == client_name {
|
|
||||||
if auth == Some("oauth") && get_oauth_provider(provider_type).is_some() {
|
|
||||||
return Some(provider_type);
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn list_oauth_capable_clients(clients: &[ClientConfig]) -> Vec<String> {
|
pub fn list_oauth_capable_clients(clients: &[ClientConfig]) -> Vec<String> {
|
||||||
clients
|
clients
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|client_config| {
|
.filter_map(|client_config| {
|
||||||
let (name, provider_type, auth) = client_config_info(client_config);
|
let (name, _, auth) = client_config_info(client_config);
|
||||||
if auth == Some("oauth") && get_oauth_provider(provider_type).is_some() {
|
if auth == Some("oauth") {
|
||||||
Some(name.to_string())
|
Some(name.to_string())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -627,7 +614,9 @@ pub fn list_oauth_capable_clients(clients: &[ClientConfig]) -> Vec<String> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn client_config_info(client_config: &ClientConfig) -> (&str, &'static str, Option<&str>) {
|
pub(crate) fn client_config_info(
|
||||||
|
client_config: &ClientConfig,
|
||||||
|
) -> (&str, &'static str, Option<&str>) {
|
||||||
match client_config {
|
match client_config {
|
||||||
ClientConfig::ClaudeConfig(c) => (
|
ClientConfig::ClaudeConfig(c) => (
|
||||||
c.name.as_deref().unwrap_or("claude"),
|
c.name.as_deref().unwrap_or("claude"),
|
||||||
|
|||||||
+30
-22
@@ -769,28 +769,36 @@ fn resolve_oauth_client(
|
|||||||
explicit: Option<&str>,
|
explicit: Option<&str>,
|
||||||
clients: &[ClientConfig],
|
clients: &[ClientConfig],
|
||||||
) -> Result<(String, Box<dyn OAuthProvider>)> {
|
) -> Result<(String, Box<dyn OAuthProvider>)> {
|
||||||
if let Some(name) = explicit {
|
let find_by_name = |name: &str| -> Option<&ClientConfig> {
|
||||||
let provider_type = oauth::resolve_provider_type(name, clients)
|
clients.iter().find(|cc| {
|
||||||
.ok_or_else(|| anyhow!("Client '{name}' not found or doesn't support OAuth"))?;
|
let (n, _, auth) = oauth::client_config_info(cc);
|
||||||
let provider = oauth::get_oauth_provider(provider_type).unwrap();
|
n == name && auth == Some("oauth")
|
||||||
return Ok((name.to_string(), provider));
|
})
|
||||||
}
|
};
|
||||||
|
|
||||||
let candidates = oauth::list_oauth_capable_clients(clients);
|
let target = if let Some(name) = explicit {
|
||||||
match candidates.len() {
|
find_by_name(name)
|
||||||
0 => bail!("No OAuth-capable clients configured."),
|
.ok_or_else(|| anyhow!("Client '{name}' not found or doesn't support OAuth"))?
|
||||||
1 => {
|
} else {
|
||||||
let name = &candidates[0];
|
let candidates = oauth::list_oauth_capable_clients(clients);
|
||||||
let provider_type = oauth::resolve_provider_type(name, clients).unwrap();
|
match candidates.len() {
|
||||||
let provider = oauth::get_oauth_provider(provider_type).unwrap();
|
0 => bail!("No OAuth-capable clients configured."),
|
||||||
Ok((name.clone(), provider))
|
1 => find_by_name(&candidates[0]).unwrap(),
|
||||||
|
_ => {
|
||||||
|
let choice = Select::new("Select a client to authenticate:", candidates.clone())
|
||||||
|
.prompt()?;
|
||||||
|
find_by_name(&choice)
|
||||||
|
.ok_or_else(|| anyhow!("Selected client '{choice}' not found"))?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
};
|
||||||
let choice =
|
|
||||||
Select::new("Select a client to authenticate:", candidates.clone()).prompt()?;
|
let name = oauth::client_config_info(target).0.to_string();
|
||||||
let provider_type = oauth::resolve_provider_type(&choice, clients).unwrap();
|
let provider = oauth::get_oauth_provider_for_client(target, &client::ALL_PROVIDER_MODELS)
|
||||||
let provider = oauth::get_oauth_provider(provider_type).unwrap();
|
.ok_or_else(|| {
|
||||||
Ok((choice, provider))
|
anyhow!(
|
||||||
}
|
"Could not build OAuth provider for '{name}' (no oauth config in models.yaml or user config)"
|
||||||
}
|
)
|
||||||
|
})?;
|
||||||
|
Ok((name, provider))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user