Skip to content

Commit 8e1dabe

Browse files
committed
新增 cct list/ls 命令显示用户配置列表
- 使用 tabled 库以表格形式展示配置信息 - 支持 "list" 和别名 "ls" 两种调用方式 - 显示别名、提供商和 API 密钥三列信息
1 parent 0ceecdd commit 8e1dabe

4 files changed

Lines changed: 53 additions & 5 deletions

File tree

src/commands/config.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::models::{AppConfig, ConfigInstance};
44
use crate::provider_store::ProviderStore;
55
use anyhow::{Result, anyhow};
66
use console::style;
7+
use tabled::{Table, Tabled};
78

89
pub struct AddCommand {
910
pub alias: String,
@@ -49,3 +50,38 @@ impl Command for AddCommand {
4950
Ok(())
5051
}
5152
}
53+
54+
#[derive(Tabled)]
55+
struct ConfigRow {
56+
#[tabled(rename = "别名")]
57+
alias: String,
58+
#[tabled(rename = "提供商")]
59+
provider: String,
60+
#[tabled(rename = "API密钥")]
61+
api_key: String,
62+
}
63+
64+
pub struct ListCommand;
65+
66+
impl Command for ListCommand {
67+
fn execute(self, config: &mut AppConfig) -> Result<()> {
68+
if config.configs.is_empty() {
69+
println!("{}", style("暂无配置").dim());
70+
return Ok(());
71+
}
72+
73+
let mut rows = Vec::new();
74+
for (alias, config_instance) in &config.configs {
75+
rows.push(ConfigRow {
76+
alias: alias.clone(),
77+
provider: config_instance.provider.clone(),
78+
api_key: config_instance.api_key.clone(),
79+
});
80+
}
81+
82+
let table = Table::new(rows);
83+
println!("{}", table);
84+
85+
Ok(())
86+
}
87+
}

src/commands/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ pub mod config;
22
pub mod provider;
33
pub mod switch;
44

5+
pub use config::{AddCommand, ListCommand};
6+
pub use provider::ProviderCommand;
7+
pub use switch::UseCommand;
8+
59
use crate::models::AppConfig;
610
use anyhow::Result;
711

src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod utils;
77

88
use anyhow::Result;
99
use clap::{Parser, Subcommand};
10-
use commands::{Command, config::AddCommand, provider::ProviderCommand, switch::UseCommand};
10+
use commands::{Command, AddCommand, ListCommand, ProviderCommand, UseCommand};
1111
use config_manager::ConfigManager;
1212

1313
#[derive(Parser)]
@@ -39,6 +39,10 @@ enum Commands {
3939
api_key: String,
4040
},
4141

42+
/// List all configurations
43+
#[command(alias = "ls")]
44+
List,
45+
4246
/// Use a configuration
4347
Use {
4448
/// Configuration alias (optional, will show interactive selection if not provided)
@@ -94,6 +98,10 @@ fn main() -> Result<()> {
9498
};
9599
cmd.execute(&mut config)?;
96100
}
101+
Commands::List => {
102+
let cmd = ListCommand;
103+
cmd.execute(&mut config)?;
104+
}
97105
Commands::Use { alias } => {
98106
let cmd = UseCommand { alias };
99107
cmd.execute(&mut config)?;

tests/integration_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::Command;
44
#[test]
55
fn test_provider_list_runs() {
66
let output = Command::new("cargo")
7-
.args(&["run", "--", "provider", "list"])
7+
.args(["run", "--", "provider", "list"])
88
.output()
99
.expect("Failed to execute command");
1010

@@ -19,7 +19,7 @@ fn test_provider_list_runs() {
1919
#[test]
2020
fn test_version() {
2121
let output = Command::new("cargo")
22-
.args(&["run", "--", "--version"])
22+
.args(["run", "--", "--version"])
2323
.output()
2424
.expect("Failed to execute command");
2525

@@ -31,7 +31,7 @@ fn test_version() {
3131
#[test]
3232
fn test_help() {
3333
let output = Command::new("cargo")
34-
.args(&["run", "--", "--help"])
34+
.args(["run", "--", "--help"])
3535
.output()
3636
.expect("Failed to execute command");
3737

@@ -43,7 +43,7 @@ fn test_help() {
4343
#[test]
4444
fn test_provider_ls() {
4545
let output = Command::new("cargo")
46-
.args(&["run", "--", "provider", "ls"])
46+
.args(["run", "--", "provider", "ls"])
4747
.output()
4848
.expect("Failed to execute command");
4949

0 commit comments

Comments
 (0)