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
#![allow(unused_imports)]
#![doc(html_logo_url = "https://lambdastackio.github.io/static/images/lambdastack-200x200.png",
html_favicon_url = "https://lambdastackio.github.io/static/images/favicon.ico",
html_root_url = "https://lambdastackio.github.io/s3lsio/s3lsio/index.html")]
#[macro_use]
extern crate lsio;
extern crate aws_sdk_rust;
extern crate rustc_serialize;
extern crate term;
extern crate url;
extern crate uuid;
#[macro_use]
extern crate log;
extern crate env_logger;
#[macro_use]
extern crate clap;
extern crate pbr;
extern crate unicase;
use std::io::{self, Write};
use std::env;
use std::path::PathBuf;
use clap::{Shell};
use url::Url;
use aws_sdk_rust::aws::errors::s3::S3Error;
use aws_sdk_rust::aws::s3::endpoint::*;
use aws_sdk_rust::aws::s3::s3client::S3Client;
use aws_sdk_rust::aws::common::region::Region;
use aws_sdk_rust::aws::common::credentials::{AwsCredentialsProvider, DefaultCredentialsProvider};
use aws_sdk_rust::aws::common::request::DispatchSignedRequest;
use common::progress::ProgressBar;
#[derive(Debug, Clone, Copy)]
pub enum OutputFormat {
JSON,
PrettyJSON,
Plain,
Serialize,
None,
}
pub struct Error {
pub format: OutputFormat,
pub color: term::color::Color,
}
pub struct Output {
pub format: OutputFormat,
pub color: term::color::Color,
}
pub struct Client<'a, P: 'a, D: 'a>
where P: AwsCredentialsProvider,
D: DispatchSignedRequest,
{
pub s3client: &'a mut S3Client<P, D>,
pub error: Error,
pub output: Output,
pub is_quiet: bool,
pub is_default_config: bool,
}
mod bucket;
mod object;
mod util;
mod common;
mod cli;
fn main() {
let mut is_quiet: bool = false;
env_logger::init().unwrap();
let version = format!("v{}", crate_version!());
let mut home: PathBuf;
match env::home_dir() {
Some(path) => {
home = path;
home.push(".s3lsio/config");
},
None => home = PathBuf::new(),
}
let matches = cli::build_cli("s3lsio", home.to_str().unwrap_or(""), &version).get_matches();
if matches.is_present("generate-bash-completions") {
cli::build_cli("s3lsio", home.to_str().unwrap_or(""), &version)
.gen_completions_to("s3lsio", Shell::Bash, &mut io::stdout());
::std::process::exit(0);
}
if matches.is_present("quiet") {
is_quiet = true;
}
let config = matches.value_of("config").unwrap();
let region = match matches.value_of("region").unwrap().to_string().to_lowercase().as_ref() {
"useast1" => Region::UsEast1,
"uswest1" => Region::UsWest1,
"uswest2" => Region::UsWest2,
"cnnorth1" => Region::CnNorth1,
"eucentral1" => Region::EuCentral1,
"euwest1" => Region::EuWest1,
"saeast1" => Region::SaEast1,
"apnortheast1" => Region::ApNortheast1,
"apnortheast2" => Region::ApNortheast2,
"apsouth1" => Region::ApSouth1,
"apsoutheast1" => Region::ApSoutheast1,
"apsoutheast2" => Region::ApSoutheast2,
_ => Region::UsEast1,
};
let ep = matches.value_of("endpoint");
let proxy = matches.value_of("proxy");
let output = match matches.value_of("output").unwrap().to_string().to_lowercase().as_ref() {
"json" => OutputFormat::JSON,
"pretty-json" => OutputFormat::PrettyJSON,
"plain" => OutputFormat::Plain,
"serialize" => OutputFormat::Serialize,
_ => OutputFormat::PrettyJSON,
};
let output_color = match matches.value_of("output-color").unwrap().to_string().to_lowercase().as_ref() {
"green" => term::color::GREEN,
"red" => term::color::RED,
"blue" => term::color::BLUE,
"yellow" => term::color::YELLOW,
"white" => term::color::WHITE,
_ => term::color::GREEN,
};
let provider = DefaultCredentialsProvider::new(None).unwrap();
let endpoint = Endpoint::new(region,
if matches.value_of("signature").unwrap() == "V2" {
Signature::V2
} else {
Signature::V4
},
if ep.is_some() {
Some(Url::parse(ep.unwrap()).unwrap())
} else {
None
},
if proxy.is_some() {
Some(Url::parse(proxy.unwrap()).unwrap())
} else {
None
},
Some(format!("s3lsio - {}", version)));
let mut s3client = S3Client::new(provider, endpoint);
let mut client = Client {
s3client: &mut s3client,
error: Error {
format: OutputFormat::Serialize,
color: term::color::RED,
},
output: Output {
format: output,
color: output_color,
},
is_quiet: is_quiet,
is_default_config: config == home.to_str().unwrap(),
};
let res = match matches.subcommand() {
("bucket", Some(sub_matches)) => bucket::commands(sub_matches, &mut client),
("object", Some(sub_matches)) => object::commands(sub_matches, &mut client),
(e, _) => {
println_color_quiet!(client.is_quiet, term::color::RED, "{}", e);
Err(S3Error::new("A valid instruction is required"))
},
};
if let Err(e) = res {
println_color_quiet!(client.is_quiet, term::color::RED, "An error occured: {}", e);
println_color_quiet!(client.is_quiet, term::color::RED, "{}", matches.usage());
::std::process::exit(1);
}
}