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
//! `router providers` — manage stored OpenAI-compatible providers.
//!
//! Split from `main.rs` to keep that file within the repository's 1000-line
//! limit.
use std::process::ExitCode;
use crate::cli::ProviderOp;
use crate::config::Config;
use crate::providers::{ProviderStore, ProviderUpsert};
#[must_use]
pub fn run(config: &Config, op: &ProviderOp) -> ExitCode {
let store = match ProviderStore::open(&config.data_dir, &config.token_secret) {
Ok(store) => store,
Err(e) => {
eprintln!("error: {e}");
return ExitCode::from(1);
}
};
run_with(&store, op)
}
/// The same command against an already-open store.
///
/// Split from [`run`] so the operations can be exercised without constructing
/// a whole configuration around them.
#[must_use]
pub fn run_with(store: &ProviderStore, op: &ProviderOp) -> ExitCode {
match op {
ProviderOp::List => match store.list_redacted() {
Ok(records) => {
println!(
"{:<20} {:<18} {:<32} {:<10} default_model",
"name", "kind", "base_url", "enabled"
);
for record in records {
println!(
"{:<20} {:<18} {:<32} {:<10} {}",
record.name,
record.kind.as_str(),
record.base_url,
record.enabled,
record.default_model.unwrap_or_default()
);
}
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("error: {e}");
ExitCode::from(1)
}
},
ProviderOp::Add {
name,
kind,
base_url,
model,
models,
api_key,
api_key_env,
enabled,
} => {
let input = ProviderUpsert {
name: name.clone(),
kind: Some(kind.clone()),
base_url: base_url.clone(),
default_model: model.clone(),
models: Some(models.clone()),
api_key: api_key.clone(),
api_key_env: api_key_env.clone(),
encrypted_api_key: None,
enabled: Some(*enabled),
};
match store.upsert(input) {
Ok(record) => {
println!(
"{}",
serde_json::to_string_pretty(&record.redacted()).unwrap_or_default()
);
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("error: {e}");
ExitCode::from(1)
}
}
}
ProviderOp::Show { name } => match store.get(name) {
Ok(Some(record)) => {
println!(
"{}",
serde_json::to_string_pretty(&record.redacted()).unwrap_or_default()
);
ExitCode::SUCCESS
}
Ok(None) => {
eprintln!("not found: {name}");
ExitCode::from(2)
}
Err(e) => {
eprintln!("error: {e}");
ExitCode::from(1)
}
},
ProviderOp::Remove { name } => match store.delete(name) {
Ok(true) => {
println!("removed {name}");
ExitCode::SUCCESS
}
Ok(false) => {
eprintln!("not found: {name}");
ExitCode::from(2)
}
Err(e) => {
eprintln!("error: {e}");
ExitCode::from(1)
}
},
ProviderOp::Import { path } => match store.import_file(path) {
Ok(count) => {
println!("imported {count} provider(s)");
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("error: {e}");
ExitCode::from(1)
}
},
}
}
#[cfg(test)]
#[path = "providers_cli_tests.rs"]
mod tests;