-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathalias.rs
More file actions
331 lines (290 loc) · 10.2 KB
/
Copy pathalias.rs
File metadata and controls
331 lines (290 loc) · 10.2 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Alias management commands
//!
//! Aliases are named references to S3-compatible storage endpoints,
//! including connection details and credentials.
use clap::Subcommand;
use serde::Serialize;
use crate::exit_code::ExitCode;
use crate::output::{Formatter, OutputConfig};
use rc_core::{Alias, AliasManager, validate_alias_endpoint};
/// Alias subcommands for managing storage service connections
#[derive(Subcommand, Debug)]
pub enum AliasCommands {
/// Add or update an alias
Set(SetArgs),
/// List all configured aliases
List(ListArgs),
/// Remove an alias
Remove(RemoveArgs),
}
/// Arguments for the `alias set` command
#[derive(clap::Args, Debug)]
pub struct SetArgs {
/// Alias name (e.g., "local", "s3", "rustfs")
pub name: String,
/// S3 endpoint URL (e.g., `http://localhost:9000`, `https://s3.amazonaws.com`)
pub endpoint: String,
/// Access key ID
pub access_key: String,
/// Secret access key
pub secret_key: String,
/// AWS region (default: us-east-1)
#[arg(long, default_value = "us-east-1")]
pub region: String,
/// Signature version: v4 or v2 (default: v4)
#[arg(long, default_value = "v4")]
pub signature: String,
/// Bucket lookup style: auto, path, or dns (default: auto)
#[arg(long, default_value = "auto")]
pub bucket_lookup: String,
/// Allow insecure TLS connections
#[arg(long, default_value = "false")]
pub insecure: bool,
}
/// Arguments for the `alias list` command
#[derive(clap::Args, Debug)]
pub struct ListArgs {
/// Show full details including endpoints
#[arg(short, long)]
pub long: bool,
}
/// Arguments for the `alias remove` command
#[derive(clap::Args, Debug)]
pub struct RemoveArgs {
/// Name of the alias to remove
pub name: String,
}
/// JSON output for alias list
#[derive(Serialize)]
struct AliasListOutput {
aliases: Vec<AliasInfo>,
}
/// Alias information for JSON output (without sensitive data)
#[derive(Serialize)]
struct AliasInfo {
name: String,
endpoint: String,
region: String,
bucket_lookup: String,
}
impl From<&Alias> for AliasInfo {
fn from(alias: &Alias) -> Self {
Self {
name: alias.name.clone(),
endpoint: alias.endpoint.clone(),
region: alias.region.clone(),
bucket_lookup: alias.bucket_lookup.clone(),
}
}
}
/// JSON output for alias set/remove operations
#[derive(Serialize)]
struct AliasOperationOutput {
success: bool,
alias: String,
message: String,
}
/// Execute an alias subcommand
pub async fn execute(cmd: AliasCommands, output_config: OutputConfig) -> ExitCode {
let formatter = Formatter::new(output_config);
let alias_manager = match AliasManager::new() {
Ok(am) => am,
Err(e) => {
formatter.error(&format!("Failed to load aliases: {e}"));
return ExitCode::GeneralError;
}
};
match cmd {
AliasCommands::Set(args) => execute_set(args, &alias_manager, &formatter).await,
AliasCommands::List(args) => execute_list(args, &alias_manager, &formatter).await,
AliasCommands::Remove(args) => execute_remove(args, &alias_manager, &formatter).await,
}
}
async fn execute_set(args: SetArgs, manager: &AliasManager, formatter: &Formatter) -> ExitCode {
// Validate inputs
if args.name.is_empty() {
formatter.error("Alias name cannot be empty");
return ExitCode::UsageError;
}
if args.endpoint.is_empty() {
formatter.error("Endpoint URL cannot be empty");
return ExitCode::UsageError;
}
if let Err(e) = validate_alias_endpoint(&args.endpoint) {
formatter.error_with_code(ExitCode::UsageError, &alias_endpoint_error_message(e));
return ExitCode::UsageError;
}
// Validate signature version
if args.signature != "v4" && args.signature != "v2" {
formatter.error("Signature must be 'v4' or 'v2'");
return ExitCode::UsageError;
}
// Validate bucket lookup
if args.bucket_lookup != "auto" && args.bucket_lookup != "path" && args.bucket_lookup != "dns" {
formatter.error("Bucket lookup must be 'auto', 'path', or 'dns'");
return ExitCode::UsageError;
}
// Create alias
let mut alias = Alias::new(
&args.name,
&args.endpoint,
&args.access_key,
&args.secret_key,
);
alias.region = args.region;
alias.signature = args.signature;
alias.bucket_lookup = args.bucket_lookup;
alias.insecure = args.insecure;
// Save alias
match manager.set(alias) {
Ok(()) => {
if formatter.is_json() {
let output = AliasOperationOutput {
success: true,
alias: args.name.clone(),
message: format!("Alias '{}' configured successfully", args.name),
};
formatter.json(&output);
} else {
let styled_name = formatter.style_name(&args.name);
formatter.success(&format!("Alias '{styled_name}' configured successfully."));
}
ExitCode::Success
}
Err(e) => {
formatter.error(&e.to_string());
ExitCode::GeneralError
}
}
}
async fn execute_list(args: ListArgs, manager: &AliasManager, formatter: &Formatter) -> ExitCode {
match manager.list() {
Ok(aliases) => {
if formatter.is_json() {
let output = AliasListOutput {
aliases: aliases.iter().map(AliasInfo::from).collect(),
};
formatter.json(&output);
} else if aliases.is_empty() {
formatter.println("No aliases configured.");
} else if args.long {
// Long format with details
for alias in &aliases {
let styled_name = formatter.style_name(&format!("{:<12}", alias.name));
let styled_url = formatter.style_url(&alias.endpoint);
let styled_region = formatter.style_date(&alias.region);
let styled_lookup = formatter.style_date(&alias.bucket_lookup);
formatter.println(&format!(
"{styled_name} {styled_url} (region: {styled_region}, lookup: {styled_lookup})"
));
}
} else {
// Short format
for alias in &aliases {
let styled_name = formatter.style_name(&format!("{:<12}", alias.name));
let styled_url = formatter.style_url(&alias.endpoint);
formatter.println(&format!("{styled_name} {styled_url}"));
}
}
ExitCode::Success
}
Err(e) => {
formatter.error(&e.to_string());
ExitCode::GeneralError
}
}
}
async fn execute_remove(
args: RemoveArgs,
manager: &AliasManager,
formatter: &Formatter,
) -> ExitCode {
match manager.remove(&args.name) {
Ok(()) => {
if formatter.is_json() {
let output = AliasOperationOutput {
success: true,
alias: args.name.clone(),
message: format!("Alias '{}' removed successfully", args.name),
};
formatter.json(&output);
} else {
let styled_name = formatter.style_name(&args.name);
formatter.success(&format!("Alias '{styled_name}' removed successfully."));
}
ExitCode::Success
}
Err(rc_core::Error::AliasNotFound(_)) => {
formatter.error(&format!("Alias '{}' not found", args.name));
ExitCode::NotFound
}
Err(e) => {
formatter.error(&e.to_string());
ExitCode::GeneralError
}
}
}
fn alias_endpoint_error_message(error: rc_core::Error) -> String {
match error {
rc_core::Error::Config(message) => message,
other => format!("Invalid endpoint: {other}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_set_args_defaults() {
// Verify default values are applied correctly
let args = SetArgs {
name: "test".to_string(),
endpoint: "http://localhost:9000".to_string(),
access_key: "accesskey".to_string(),
secret_key: "secretkey".to_string(),
region: "us-east-1".to_string(),
signature: "v4".to_string(),
bucket_lookup: "auto".to_string(),
insecure: false,
};
assert_eq!(args.region, "us-east-1");
assert_eq!(args.signature, "v4");
assert_eq!(args.bucket_lookup, "auto");
assert!(!args.insecure);
}
#[test]
fn test_alias_info_from_alias() {
let alias = Alias::new("test", "http://localhost:9000", "key", "secret");
let info = AliasInfo::from(&alias);
assert_eq!(info.name, "test");
assert_eq!(info.endpoint, "http://localhost:9000");
assert_eq!(info.region, "us-east-1");
}
#[tokio::test]
async fn test_execute_set_rejects_invalid_endpoint_url() {
let temp_dir = tempfile::TempDir::new().unwrap();
let manager = AliasManager::with_config_manager(rc_core::ConfigManager::with_path(
temp_dir.path().join("config.toml"),
));
let formatter = Formatter::new(OutputConfig::default());
let args = SetArgs {
name: "rustfs".to_string(),
endpoint: "http://rustfs-node{1...32}:9000".to_string(),
access_key: "accesskey".to_string(),
secret_key: "secretkey".to_string(),
region: "us-east-1".to_string(),
signature: "v4".to_string(),
bucket_lookup: "auto".to_string(),
insecure: false,
};
let exit_code = execute_set(args, &manager, &formatter).await;
assert_eq!(exit_code, ExitCode::UsageError);
assert!(manager.get("rustfs").is_err());
}
#[test]
fn test_alias_endpoint_error_message_omits_config_prefix() {
let message = alias_endpoint_error_message(rc_core::Error::Config(
"Endpoint must include a host".to_string(),
));
assert_eq!(message, "Endpoint must include a host");
}
}