]>
Commit | Line | Data |
---|---|---|
ff5d3707 | 1 | extern crate apitest; |
2 | ||
3 | use failure::*; | |
4 | ||
43eeef28 | 5 | use apitest::tools; |
ff5d3707 | 6 | use apitest::cli::command::*; |
7 | use apitest::api::schema::*; | |
8 | use apitest::api::router::*; | |
9 | use apitest::backup::chunk_store::*; | |
43eeef28 | 10 | use serde_json::{Value}; |
ff5d3707 | 11 | |
12 | use apitest::config::datastore; | |
13 | ||
a914a774 | 14 | fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str { |
15 | param[name].as_str().expect(&format!("missing parameter '{}'", name)) | |
16 | } | |
17 | ||
18 | ||
ff5d3707 | 19 | fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> { |
20 | ||
a914a774 | 21 | let filename = required_string_param(¶m, "filename"); |
22 | let store = required_string_param(¶m, "store"); | |
23 | ||
24 | let config = datastore::config()?; | |
25 | let (_, store_config) = config.sections.get(store) | |
26 | .ok_or(format_err!("no such datastore '{}'", store))?; | |
27 | ||
28 | let path = store_config["path"].as_str().unwrap(); | |
29 | ||
30 | let _store = ChunkStore::open(path)?; | |
31 | ||
32 | println!("Backup file '{}' to '{}'", filename, store); | |
33 | ||
34 | let file = std::fs::File::open(filename)?; | |
35 | ||
43eeef28 | 36 | tools::file_chunker(file, 64*1024, |pos, chunk| { |
a914a774 | 37 | println!("CHUNK {} {}", pos, chunk.len()); |
38 | Ok(true) | |
39 | })?; | |
40 | ||
ff5d3707 | 41 | Ok(Value::Null) |
42 | } | |
43 | ||
44 | ||
45 | fn main() { | |
46 | ||
47 | let cmd_def = CliCommand::new( | |
48 | ApiMethod::new( | |
49 | backup_file, | |
50 | ObjectSchema::new("Create backup from file.") | |
51 | .required("filename", StringSchema::new("Source file name.")) | |
52 | .required("store", StringSchema::new("Datastore name.")) | |
53 | )) | |
54 | .arg_param(vec!["filename"]); | |
a914a774 | 55 | |
ff5d3707 | 56 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
57 | eprintln!("Error: {}", err); | |
58 | print_cli_usage(); | |
59 | std::process::exit(-1); | |
60 | } | |
61 | ||
62 | } |