s3rs 0.2.7

A s3 cli client with multi configs with diffent provider
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
extern crate dirs;
extern crate toml;
#[macro_use]
extern crate serde_derive;
extern crate interactor;
extern crate reqwest;

extern crate base64;
extern crate chrono;
extern crate crypto;
extern crate hmac;
extern crate hyper;
extern crate rustc_serialize;
extern crate sha2;
extern crate url;
#[macro_use]
extern crate log;
extern crate colored;
extern crate hmacsha1;
extern crate md5;
extern crate quick_xml;
extern crate regex;
extern crate s3handler;
extern crate serde_json;

use colored::*;
use dirs::home_dir;
use log::{Level, LevelFilter, Metadata, Record};
use regex::Regex;
use std::fs::{create_dir, read_dir, File, OpenOptions};
use std::io;
use std::io::stdout;
use std::io::{BufRead, BufReader, Read, Write};
use std::str;
use std::str::FromStr;

static MY_LOGGER: MyLogger = MyLogger;
static S3_FORMAT: &'static str =
    r#"[sS]3://(?P<bucket>[A-Za-z0-9\-\._]+)(?P<object>[A-Za-z0-9\-\._/]*)"#;

struct MyLogger;

impl log::Log for MyLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= Level::Trace
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            match record.level() {
                log::Level::Error => println!("{} - {}", "ERROR".red().bold(), record.args()),
                log::Level::Warn => println!("{} - {}", "WARN".red(), record.args()),
                log::Level::Info => println!("{} - {}", "INFO".cyan(), record.args()),
                log::Level::Debug => println!("{} - {}", "DEBUG".blue().bold(), record.args()),
                log::Level::Trace => println!("{} - {}", "TRACE".blue(), record.args()),
            }
        }
    }
    fn flush(&self) {}
}

#[derive(Debug, Deserialize)]
struct Config {
    credential: Option<Vec<s3handler::CredentialConfig>>,
}
impl<'a> Config {
    fn gen_selecitons(&'a self) -> Vec<String> {
        let mut display_list = Vec::new();
        let credential = &self.credential.clone().unwrap();
        for cre in credential.into_iter() {
            let c = cre.clone();
            let option = String::from(format!(
                "[{}] {} ({}) {} ({})",
                c.s3_type.unwrap_or(String::from("aws")),
                c.host,
                c.region.unwrap_or(String::from("us-east-1")),
                c.user.unwrap_or(String::from("user")),
                c.access_key
            ));
            display_list.push(option);
        }
        display_list
    }
}

fn read_parse<T>(tty: &mut File, prompt: &str, min: T, max: T) -> io::Result<T>
where
    T: FromStr + Ord,
{
    let _ = tty.write_all(prompt.as_bytes());
    let mut reader = io::BufReader::new(tty);
    let mut result = String::new();
    let _ = reader.read_line(&mut result);
    match result.replace("\n", "").parse::<T>() {
        Ok(x) => {
            if x >= min && x <= max {
                Ok(x)
            } else {
                read_parse(reader.into_inner(), prompt, min, max)
            }
        }
        _ => read_parse(reader.into_inner(), prompt, min, max),
    }
}

fn my_pick_from_list_internal<T: AsRef<str>>(items: &[T], prompt: &str) -> io::Result<usize> {
    let mut tty = OpenOptions::new().read(true).write(true).open("/dev/tty")?;
    let pad_len = ((items.len() as f32).log10().floor() + 1.0) as usize;
    for (i, item) in items.iter().enumerate() {
        tty.write_all(
            format!(
                "{1:0$}. {2}\n",
                pad_len,
                i + 1,
                item.as_ref().replace("\n", "")
            )
            .as_bytes(),
        )?
    }
    let idx = read_parse::<usize>(&mut tty, prompt, 1, items.len())? - 1;
    Ok(idx)
}

fn main() {
    log::set_logger(&MY_LOGGER).unwrap();
    log::set_max_level(LevelFilter::Error);

    let s3rs_config_foler = home_dir().unwrap().join(".config/s3rs");
    let legacy_s3rs_config = home_dir().unwrap().join(".s3rs.toml");
    let mut config_contents = String::new();
    if s3rs_config_foler.exists() {
        for entry in read_dir(s3rs_config_foler).unwrap() {
            let path = entry.unwrap().path();
            if !path.is_dir() {
                let mut f = File::open(path).expect("cannot open file");
                f.read_to_string(&mut config_contents)
                    .expect("cannot read file");
            }
        }
    } else if legacy_s3rs_config.exists() {
        println!("{}", "legacy s3rs config file detected, you may split it into different config files, and put them under ~/.config/s3rs".bold());
        let mut f = File::open(legacy_s3rs_config).expect("Can not open legacy 3rs config file");
        f.read_to_string(&mut config_contents)
            .expect("legacy s3rs config is not readable");
    } else {
        create_dir(s3rs_config_foler.clone()).expect("create config folder fail");
        let mut f = File::create(s3rs_config_foler.join("aws-example.toml"))
            .expect("Can not write s3rs config example file");
        let _ = f.write_all(include_str!("../config_examples/aws-example.toml").as_bytes());
        let mut f = File::create(s3rs_config_foler.join("ceph-example.toml"))
            .expect("Can not write s3rs config example file");
        let _ = f.write_all(include_str!("../config_examples/ceph-example.toml").as_bytes());
        println!(
            "Example files is created in {}, multiple toml files can put under this folder",
            "~/.config/s3rs".bold()
        );
        return;
    }

    if config_contents == "" {
        println!(
            "{}",
            "Lack of config files please put in ~/.config/s3rs".bold()
        );
        return;
    }

    let config: Config = toml::from_str(config_contents.as_str()).unwrap();
    let config_option: Vec<String> = config.gen_selecitons();

    let mut chosen_int = my_pick_from_list_internal(&config_option, "Selection: ").unwrap();
    let config_list = config.credential.unwrap();
    let mut handler = s3handler::Handler::init_from_config(&config_list[chosen_int]);
    let mut login_user = config_list[chosen_int]
        .user
        .clone()
        .unwrap_or("unknown".to_string());
    let mut s3_type = config_list[chosen_int]
        .s3_type
        .clone()
        .unwrap_or("aws".to_string());

    println!(
        "enter command, type {} for usage or type {} for quit",
        "help".bold(),
        "exit".bold()
    );

    // let mut raw_input;
    let mut command = String::new();

    fn change_log_type(command: &str) {
        if command.ends_with("trace") {
            log::set_max_level(LevelFilter::Trace);
            println!("set up log level trace");
        } else if command.ends_with("debug") {
            log::set_max_level(LevelFilter::Debug);
            println!("set up log level debug");
        } else if command.ends_with("info") {
            log::set_max_level(LevelFilter::Info);
            println!("set up log level info");
        } else if command.ends_with("error") {
            log::set_max_level(LevelFilter::Error);
            println!("set up log level error");
        } else {
            println!("usage: log [trace/debug/info/error]");
        }
    }

    fn print_if_error(result: Result<(), &str>) {
        match result {
            Err(e) => println!("{}", e),
            Ok(_) => {}
        };
    }

    while command != "exit" && command != "quit" {
        command = match OpenOptions::new().read(true).write(true).open("/dev/tty") {
            Ok(mut tty) => {
                tty.flush().expect("Could not open tty");
                let _ = tty.write_all(
                    format!("{} {} {} ", "s3rs".green(), login_user.cyan(), ">".green()).as_bytes(),
                );
                let reader = BufReader::new(&tty);
                let mut command_iter = reader.lines().map(|l| l.unwrap());
                command_iter.next().unwrap_or("logout".to_string())
            }
            Err(e) => {
                println!("{:?}", e);
                "quit".to_string()
            }
        };

        debug!("===== do command: {} =====", command);
        if command.starts_with("la") {
            match handler.la() {
                Err(e) => println!("{}", e),
                Ok(v) => {
                    for o in v {
                        debug!("{:?}", o);
                        println!("{}", String::from(o));
                    }
                }
            };
        } else if command.starts_with("ls") {
            match handler.ls(command.split_whitespace().nth(1)) {
                Err(e) => println!("{}", e),
                Ok(v) => {
                    for o in v {
                        debug!("{:?}", o);
                        println!("{}", String::from(o));
                    }
                }
            };
        } else if command.starts_with("ll") {
            let r = match command.split_whitespace().nth(1) {
                Some(b) => handler.ls(Some(b)),
                None => handler.la(),
            };
            match r {
                Err(e) => println!("{}", e),
                Ok(v) => {
                    println!("STORAGE CLASS\tMODIFIED TIME\t\t\tETAG\t\t\t\t\tKEY",);
                    for o in v {
                        debug!("{:?}", o);
                        println!(
                            "{}\t{}\t{}\t{}",
                            o.storage_class.clone().unwrap_or("        ".to_string()),
                            o.mtime
                                .clone()
                                .unwrap_or("                        ".to_string()),
                            o.etag
                                .clone()
                                .unwrap_or("                                 ".to_string()),
                            String::from(o)
                        );
                    }
                }
            };
        } else if command.starts_with("put") {
            match handler.put(
                command.split_whitespace().nth(1).unwrap_or(""),
                command.split_whitespace().nth(2).unwrap_or(""),
            ) {
                Err(e) => println!("{}", e),
                Ok(_) => println!("upload completed"),
            };
        } else if command.starts_with("get") {
            match handler.get(
                command.split_whitespace().nth(1).unwrap_or(""),
                command.split_whitespace().nth(2),
            ) {
                Err(e) => println!("{}", e),
                Ok(_) => println!("download completed"),
            };
        } else if command.starts_with("cat") {
            print_if_error(handler.cat(command.split_whitespace().nth(1).unwrap_or("")));
        } else if command.starts_with("del") {
            let mut iter = command.split_whitespace();
            let target = iter.nth(1).unwrap_or("");
            let mut headers = Vec::new();
            loop {
                match iter.next() {
                    Some(header_pair) => match header_pair.find(':') {
                        Some(_) => headers.push((
                            header_pair.split(':').nth(0).unwrap(),
                            header_pair.split(':').nth(1).unwrap(),
                        )),
                        None => headers.push((&header_pair, "")),
                    },
                    None => {
                        break;
                    }
                };
            }
            match handler.del_with_flag(target, &headers) {
                Err(e) => println!("{}", e),
                Ok(_) => println!("deletion completed"),
            }
        } else if command.starts_with("tag") {
            let mut iter = command.split_whitespace();
            let action = iter.nth(1).unwrap_or("");
            let target = iter.nth(0).unwrap_or("");
            let mut tags = Vec::new();
            loop {
                match iter.next() {
                    Some(kv_pair) => match kv_pair.find('=') {
                        Some(_) => tags.push((
                            kv_pair.split('=').nth(0).unwrap(),
                            kv_pair.split('=').nth(1).unwrap(),
                        )),
                        None => tags.push((&kv_pair, "")),
                    },
                    None => {
                        break;
                    }
                };
            }
            match action {
                "add" | "put" => match handler.add_tag(target, &tags) {
                    Err(e) => println!("{}", e),
                    Ok(_) => println!("tag completed"),
                },
                "del" | "rm" => match handler.del_tag(target) {
                    Err(e) => println!("{}", e),
                    Ok(_) => println!("tag removed"),
                },
                "ls" | "list" => match handler.list_tag(target) {
                    Err(e) => println!("{}", e),
                    Ok(_) => {}
                },
                _ => println!("only support these tag actions: ls, add, put, del, rm"),
            }
        } else if command.starts_with("usage") {
            let mut iter = command.split_whitespace();
            let target = iter.nth(1).unwrap_or("");
            let mut options = Vec::new();
            loop {
                match iter.next() {
                    Some(kv_pair) => match kv_pair.find('=') {
                        Some(_) => options.push((
                            kv_pair.split('=').nth(0).unwrap(),
                            kv_pair.split('=').nth(1).unwrap(),
                        )),
                        None => options.push((&kv_pair, "")),
                    },
                    None => {
                        break;
                    }
                };
            }
            match handler.usage(target, &options) {
                Err(e) => println!("{}", e),
                Ok(_) => {}
            }
        } else if command.starts_with("mb") {
            print_if_error(handler.mb(command.split_whitespace().nth(1).unwrap_or("")));
        } else if command.starts_with("rb") {
            print_if_error(handler.rb(command.split_whitespace().nth(1).unwrap_or("")));
        } else if command.starts_with("/") {
            match handler.url_command(&command) {
                Err(e) => println!("{}", e),
                Ok(_) => {}
            };
        } else if command.starts_with("info") {
            let target = command.split_whitespace().nth(1).unwrap_or("");
            let caps;
            let bucket = if target.starts_with("s3://") || target.starts_with("S3://") {
                let re = Regex::new(S3_FORMAT).unwrap();
                caps = re
                    .captures(command.split_whitespace().nth(1).unwrap_or(""))
                    .expect("S3 object format error.");
                &caps["bucket"]
            } else {
                target
            };
            println!("{}", "location:".yellow().bold());
            let _ = handler.url_command(format!("/{}?location", bucket).as_str());
            println!("\n{}", "acl:".yellow().bold());
            let _ = handler.url_command(format!("/{}?acl", bucket).as_str());
            println!("\n{}", "versioning:".yellow().bold());
            let _ = handler.url_command(format!("/{}?versioning", bucket).as_str());
            match s3_type.as_str() {
                "ceph" => {
                    println!("\n{}", "version:".yellow().bold());
                    let _ = handler.url_command(format!("/{}?version", bucket).as_str());
                    println!("\n{}", "uploads:".yellow().bold());
                    let _ = handler.url_command(format!("/{}?uploads", bucket).as_str());
                }
                "aws" | _ => {}
            }
        } else if command.starts_with("s3_type") {
            handler.change_s3_type(&command);
        } else if command.starts_with("auth_type") {
            handler.change_auth_type(&command);
        } else if command.starts_with("format") {
            handler.change_format_type(&command);
        } else if command.starts_with("url_style") {
            handler.change_url_style(&command);
        } else if command.starts_with("logout") {
            println!("");
            chosen_int = my_pick_from_list_internal(&config_option, "Selection: ").unwrap();
            handler = s3handler::Handler::init_from_config(&config_list[chosen_int]);
            login_user = config_list[chosen_int]
                .user
                .clone()
                .unwrap_or(" ".to_string());
            s3_type = config_list[chosen_int]
                .s3_type
                .clone()
                .unwrap_or("aws".to_string());
        } else if command.starts_with("log") {
            change_log_type(&command);
        } else if command.starts_with("exit") || command.starts_with("quit") {
            println!("Thanks for using, cya~");
        } else if command.starts_with("help") {
            println!(
                r#"
USAGE:

    {0}
        list all objects
    
    {1}
        list all buckets

    {1} {2}
        list all objects of the bucket

    {39}
        list all object detail

    {39} {2}
        list all objects detail of the bucket

    {3} {2}
        create bucket

    {4} {2}
        delete bucket

    {5} {6} s3://{2}/{7}
        upload the file with specify object name

    {5} {6} s3://{2}
        upload the file as the same file name
        
    {5} test s3://{2}/{7}
        upload a small test text file with specify object name

    {8} s3://{2}/{7} {6}
        download the object

    {8} s3://{2}/{7} 
        download the object to current folder

    {9} s3://{2}/{7} 
        display the object content

    {10} s3://{2}/{7} [delete-marker:true] [secure-delete:true]
        delete the object

    {29} {1}/{36} s3://{2}/{7}
        list tags of the object

    {29} {33}/{5} s3://{2}/{7}  {30}={31} ...
        add tags to the object

    {29} {10}/{4} s3://{2}/{7}
        remove tags from the object

    /{11}?{12}
        get uri command

    {13}
        show this usage

    {14} {32}/{15}/{16}/{17}/{18}
        change the log level
        {32} for every thing
        {15} for request auth detail
        {16} for request header, status code, raw body
        {17} for request http response
        {18} is default

    {19} {20}/{21}
        change the auth type and format for different S3 service

    {22} {23}/{24}
        change the auth type 

    {25} {26}/{27}
        change the request format

    {28}
        quit the programe

    {34} / {35}
        logout and reselect account

    {37} s3://{2} 
        show the usage of the bucket (ceph admin only)

    {38} s3://{2} / {38} {2}
        show the bucket information
        acl(ceph, aws), location(ceph, aws), versioning(ceph, aws), uploads(ceph), version(ceph)

    If you have any issue, please submit to here https://github.com/yanganto/s3rs/issues 
        "#,
                "la".bold(),
                "ls".bold(),
                "<bucket>".cyan(),
                "mb".bold(),
                "rm".bold(),
                "put".bold(),
                "<file>".cyan(),
                "<object>".cyan(),
                "get".bold(),
                "cat".bold(),
                "del".bold(),
                "<uri>".cyan(),
                "<query string>".cyan(),
                "help".bold(),
                "log".bold(),
                "trace".blue(),
                "debug".blue(),
                "info".blue(),
                "error".blue(),
                "s3_type".bold(),
                "aws".blue(),
                "ceph".blue(),
                "auth_type".bold(),
                "aws2".blue(),
                "aws4".blue(),
                "format".bold(),
                "xml".blue(),
                "json".blue(),
                "exit".bold(),
                "tag".bold(),
                "<key>".cyan(),
                "<value>".cyan(),
                "trace".blue(),
                "add".bold(),
                "logout".bold(),
                "Ctrl + d".bold(),
                "list".bold(),
                "usage".bold(),
                "info".bold(),
                "ll".bold() //39
            );
        } else {
            println!(
                "command {} not found, help for usage or exit for quit",
                command
            );
        }
        println!("");
        stdout().flush().expect("Could not flush stdout");
    }
}