rustfs-cli 0.1.22

A Rust S3 CLI client for S3-compatible object storage
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! sql command — S3 Select queries on objects.

use clap::{Args, ValueEnum};
use rc_core::{
    AliasManager, ObjectStore, SelectCompression, SelectCsvFileHeaderInfo, SelectCsvInputOptions,
    SelectCsvOutputOptions, SelectInputFormat, SelectJsonInputOptions, SelectJsonInputType,
    SelectJsonOutputOptions, SelectOptions, SelectOutputFormat, SelectQuoteFields,
    SelectScanRangeOptions, SelectSseCustomerOptions, parse_object_path,
};
use rc_s3::S3Client;

use crate::exit_code::ExitCode;
use crate::output::{Formatter, OutputConfig};

/// Run an S3 Select SQL query on an object and stream results to stdout.
#[derive(Args, Debug)]
pub struct SqlArgs {
    /// Object path (alias/bucket/key)
    pub path: String,

    /// SQL expression (S3 Select)
    #[arg(long)]
    pub query: String,

    /// Input object format
    #[arg(long, value_enum, default_value_t = InputFormatArg::Csv)]
    pub input_format: InputFormatArg,

    /// Select result format
    #[arg(long, value_enum, default_value_t = OutputFormatArg::Csv)]
    pub output_format: OutputFormatArg,

    /// Compression of the stored object (input decompression)
    #[arg(long, value_enum, default_value_t = CompressionArg::None)]
    pub compression: CompressionArg,

    /// CSV input header handling
    #[arg(long, value_enum, default_value_t = CsvFileHeaderInfoArg::None)]
    pub csv_file_header_info: CsvFileHeaderInfoArg,

    /// CSV input field delimiter
    #[arg(long)]
    pub csv_input_field_delimiter: Option<String>,

    /// CSV input quote character
    #[arg(long)]
    pub csv_input_quote: Option<String>,

    /// CSV input quote escape character
    #[arg(long)]
    pub csv_input_quote_escape: Option<String>,

    /// CSV input comment character
    #[arg(long)]
    pub csv_input_comment: Option<String>,

    /// CSV output field delimiter
    #[arg(long)]
    pub csv_output_field_delimiter: Option<String>,

    /// CSV output record delimiter
    #[arg(long)]
    pub csv_output_record_delimiter: Option<String>,

    /// CSV output quote character
    #[arg(long)]
    pub csv_output_quote: Option<String>,

    /// CSV output quote escape character
    #[arg(long)]
    pub csv_output_quote_escape: Option<String>,

    /// CSV output quote behavior
    #[arg(long, value_enum, default_value_t = QuoteFieldsArg::AsNeeded)]
    pub csv_output_quote_fields: QuoteFieldsArg,

    /// JSON input shape
    #[arg(long, value_enum, default_value_t = JsonTypeArg::Lines)]
    pub json_type: JsonTypeArg,

    /// JSON output record delimiter
    #[arg(long)]
    pub json_output_record_delimiter: Option<String>,

    /// Select ScanRange start byte
    #[arg(long)]
    pub scan_start: Option<i64>,

    /// Select ScanRange end byte
    #[arg(long)]
    pub scan_end: Option<i64>,

    /// SSE-C customer algorithm, usually AES256
    #[arg(long)]
    pub sse_customer_algorithm: Option<String>,

    /// SSE-C customer key
    #[arg(long)]
    pub sse_customer_key: Option<String>,

    /// SSE-C customer key MD5
    #[arg(long)]
    pub sse_customer_key_md5: Option<String>,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum InputFormatArg {
    Csv,
    /// Newline-delimited JSON (S3 Select `Type=LINES`).
    Json,
    Parquet,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum OutputFormatArg {
    Csv,
    Json,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum CompressionArg {
    None,
    Gzip,
    Bzip2,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum CsvFileHeaderInfoArg {
    None,
    Ignore,
    Use,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum JsonTypeArg {
    Lines,
    Document,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum QuoteFieldsArg {
    Always,
    AsNeeded,
}

impl From<InputFormatArg> for SelectInputFormat {
    fn from(value: InputFormatArg) -> Self {
        match value {
            InputFormatArg::Csv => SelectInputFormat::Csv,
            InputFormatArg::Json => SelectInputFormat::Json,
            InputFormatArg::Parquet => SelectInputFormat::Parquet,
        }
    }
}

impl From<OutputFormatArg> for SelectOutputFormat {
    fn from(value: OutputFormatArg) -> Self {
        match value {
            OutputFormatArg::Csv => SelectOutputFormat::Csv,
            OutputFormatArg::Json => SelectOutputFormat::Json,
        }
    }
}

impl From<CompressionArg> for SelectCompression {
    fn from(value: CompressionArg) -> Self {
        match value {
            CompressionArg::None => SelectCompression::None,
            CompressionArg::Gzip => SelectCompression::Gzip,
            CompressionArg::Bzip2 => SelectCompression::Bzip2,
        }
    }
}

impl From<CsvFileHeaderInfoArg> for SelectCsvFileHeaderInfo {
    fn from(value: CsvFileHeaderInfoArg) -> Self {
        match value {
            CsvFileHeaderInfoArg::None => SelectCsvFileHeaderInfo::None,
            CsvFileHeaderInfoArg::Ignore => SelectCsvFileHeaderInfo::Ignore,
            CsvFileHeaderInfoArg::Use => SelectCsvFileHeaderInfo::Use,
        }
    }
}

impl From<JsonTypeArg> for SelectJsonInputType {
    fn from(value: JsonTypeArg) -> Self {
        match value {
            JsonTypeArg::Lines => SelectJsonInputType::Lines,
            JsonTypeArg::Document => SelectJsonInputType::Document,
        }
    }
}

impl From<QuoteFieldsArg> for SelectQuoteFields {
    fn from(value: QuoteFieldsArg) -> Self {
        match value {
            QuoteFieldsArg::Always => SelectQuoteFields::Always,
            QuoteFieldsArg::AsNeeded => SelectQuoteFields::AsNeeded,
        }
    }
}

/// Execute the `sql` command.
pub async fn execute(args: SqlArgs, output_config: OutputConfig) -> ExitCode {
    let formatter = Formatter::new(output_config);

    if args.query.trim().is_empty() {
        formatter.error("Query must not be empty (--query)");
        return ExitCode::UsageError;
    }

    let remote = match parse_object_path(&args.path) {
        Ok(p) => p,
        Err(e) => {
            formatter.error(&e.to_string());
            return ExitCode::UsageError;
        }
    };

    if let Err(message) = validate_select_args(&args) {
        formatter.error(&message);
        return ExitCode::UsageError;
    }

    let alias_manager = match AliasManager::new() {
        Ok(am) => am,
        Err(e) => {
            formatter.error(&format!("Failed to load aliases: {e}"));
            return ExitCode::GeneralError;
        }
    };

    let alias = match alias_manager.get(&remote.alias) {
        Ok(a) => a,
        Err(_) => {
            formatter.error(&format!("Alias '{}' not found", remote.alias));
            return ExitCode::NotFound;
        }
    };

    let client = match S3Client::new(alias).await {
        Ok(c) => c,
        Err(e) => {
            formatter.error(&format!("Failed to create S3 client: {e}"));
            return ExitCode::NetworkError;
        }
    };

    let options = SelectOptions {
        expression: args.query,
        input_format: args.input_format.into(),
        output_format: args.output_format.into(),
        compression: args.compression.into(),
        csv_input: SelectCsvInputOptions {
            file_header_info: args.csv_file_header_info.into(),
            field_delimiter: args.csv_input_field_delimiter,
            quote_character: args.csv_input_quote,
            quote_escape_character: args.csv_input_quote_escape,
            comments: args.csv_input_comment,
        },
        csv_output: SelectCsvOutputOptions {
            field_delimiter: args.csv_output_field_delimiter,
            record_delimiter: args.csv_output_record_delimiter,
            quote_character: args.csv_output_quote,
            quote_escape_character: args.csv_output_quote_escape,
            quote_fields: args.csv_output_quote_fields.into(),
        },
        json_input: SelectJsonInputOptions {
            input_type: args.json_type.into(),
        },
        json_output: SelectJsonOutputOptions {
            record_delimiter: args.json_output_record_delimiter,
        },
        scan_range: SelectScanRangeOptions {
            start: args.scan_start,
            end: args.scan_end,
        },
        sse_customer: SelectSseCustomerOptions {
            algorithm: args.sse_customer_algorithm,
            key: args.sse_customer_key,
            key_md5: args.sse_customer_key_md5,
        },
    };

    let mut stdout = tokio::io::stdout();

    match client
        .select_object_content(&remote, &options, &mut stdout)
        .await
    {
        Ok(()) => ExitCode::Success,
        Err(e) => {
            formatter.error(&e.to_string());
            exit_code_from_error(&e)
        }
    }
}

fn validate_select_args(args: &SqlArgs) -> std::result::Result<(), String> {
    validate_single_byte(
        "--csv-input-field-delimiter",
        args.csv_input_field_delimiter.as_deref(),
    )?;
    validate_single_byte("--csv-input-quote", args.csv_input_quote.as_deref())?;
    validate_single_byte(
        "--csv-input-quote-escape",
        args.csv_input_quote_escape.as_deref(),
    )?;
    validate_single_byte("--csv-input-comment", args.csv_input_comment.as_deref())?;
    validate_single_byte(
        "--csv-output-field-delimiter",
        args.csv_output_field_delimiter.as_deref(),
    )?;
    validate_record_delimiter(
        "--csv-output-record-delimiter",
        args.csv_output_record_delimiter.as_deref(),
    )?;
    validate_single_byte("--csv-output-quote", args.csv_output_quote.as_deref())?;
    validate_single_byte(
        "--csv-output-quote-escape",
        args.csv_output_quote_escape.as_deref(),
    )?;
    validate_scan_range_args(args)
}

fn validate_single_byte(name: &str, value: Option<&str>) -> std::result::Result<(), String> {
    if let Some(value) = value
        && value.len() != 1
    {
        return Err(format!("{name} must be exactly one byte"));
    }
    Ok(())
}

fn validate_record_delimiter(name: &str, value: Option<&str>) -> std::result::Result<(), String> {
    if let Some(value) = value
        && value.len() != 1
        && value != "\r\n"
    {
        return Err(format!("{name} must be exactly one byte or CRLF"));
    }
    Ok(())
}

fn validate_scan_range_args(args: &SqlArgs) -> std::result::Result<(), String> {
    if args.scan_start.is_none() && args.scan_end.is_none() {
        return Ok(());
    }
    if matches!(args.input_format, InputFormatArg::Parquet) {
        return Err("ScanRange is not supported for Parquet input".to_string());
    }
    if matches!(args.input_format, InputFormatArg::Json)
        && matches!(args.json_type, JsonTypeArg::Document)
    {
        return Err("ScanRange is not supported for JSON document input".to_string());
    }
    if args.scan_start.is_some_and(|start| start < 0) || args.scan_end.is_some_and(|end| end < 0) {
        return Err("ScanRange start and end must be non-negative".to_string());
    }
    if let (Some(start), Some(end)) = (args.scan_start, args.scan_end)
        && start > end
    {
        return Err("ScanRange start must not be greater than end".to_string());
    }
    Ok(())
}

fn exit_code_from_error(error: &rc_core::Error) -> ExitCode {
    ExitCode::from_i32(error.exit_code()).unwrap_or(ExitCode::GeneralError)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::output::OutputConfig;
    use rc_core::Error;

    fn base_args(path: &str, query: &str) -> SqlArgs {
        SqlArgs {
            path: path.to_string(),
            query: query.to_string(),
            input_format: InputFormatArg::Csv,
            output_format: OutputFormatArg::Csv,
            compression: CompressionArg::None,
            csv_file_header_info: CsvFileHeaderInfoArg::None,
            csv_input_field_delimiter: None,
            csv_input_quote: None,
            csv_input_quote_escape: None,
            csv_input_comment: None,
            csv_output_field_delimiter: None,
            csv_output_record_delimiter: None,
            csv_output_quote: None,
            csv_output_quote_escape: None,
            csv_output_quote_fields: QuoteFieldsArg::AsNeeded,
            json_type: JsonTypeArg::Lines,
            json_output_record_delimiter: None,
            scan_start: None,
            scan_end: None,
            sse_customer_algorithm: None,
            sse_customer_key: None,
            sse_customer_key_md5: None,
        }
    }

    #[tokio::test]
    async fn sql_empty_query_is_usage_error() {
        let args = base_args("a/b/c", "  ");
        let code = execute(args, OutputConfig::default()).await;
        assert_eq!(code, ExitCode::UsageError);
    }

    #[tokio::test]
    async fn sql_invalid_object_path_is_usage_error() {
        let args = base_args("a/b", "SELECT 1");
        let code = execute(args, OutputConfig::default()).await;
        assert_eq!(code, ExitCode::UsageError);
    }

    #[tokio::test]
    async fn sql_rejects_multi_byte_csv_delimiter() {
        let mut args = base_args("a/b/c", "SELECT * FROM S3Object");
        args.csv_input_field_delimiter = Some("||".to_string());
        let code = execute(args, OutputConfig::default()).await;
        assert_eq!(code, ExitCode::UsageError);
    }

    #[tokio::test]
    async fn sql_rejects_scan_range_for_json_document() {
        let mut args = base_args("a/b/c", "SELECT * FROM S3Object");
        args.input_format = InputFormatArg::Json;
        args.json_type = JsonTypeArg::Document;
        args.scan_start = Some(0);
        let code = execute(args, OutputConfig::default()).await;
        assert_eq!(code, ExitCode::UsageError);
    }

    #[tokio::test]
    async fn sql_rejects_scan_start_after_end() {
        let mut args = base_args("a/b/c", "SELECT * FROM S3Object");
        args.scan_start = Some(20);
        args.scan_end = Some(10);
        let code = execute(args, OutputConfig::default()).await;
        assert_eq!(code, ExitCode::UsageError);
    }

    #[test]
    fn sql_exit_code_from_backend_errors() {
        let cases = [
            (
                Error::UnsupportedFeature("S3 Select is not supported".to_string()),
                ExitCode::UnsupportedFeature,
            ),
            (
                Error::NotFound("Object not found".to_string()),
                ExitCode::NotFound,
            ),
            (
                Error::Auth("Access denied".to_string()),
                ExitCode::AuthError,
            ),
            (
                Error::Network("Request timeout".to_string()),
                ExitCode::NetworkError,
            ),
            (
                Error::General("Query failed".to_string()),
                ExitCode::GeneralError,
            ),
        ];

        for (error, expected) in cases {
            assert_eq!(exit_code_from_error(&error), expected);
        }
    }
}