2016-01-24 19:20:04 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::io::stdin;
|
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::result::Result as RResult;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::ops::DerefMut;
|
2016-02-06 10:54:36 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::io::stderr;
|
|
|
|
use std::process::exit;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
|
2016-01-23 15:29:04 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-01-24 19:20:04 +00:00
|
|
|
use libimagstore::store::Entry;
|
|
|
|
use libimagstore::store::EntryHeader;
|
2016-08-28 14:22:43 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
|
|
|
use libimagerror::trace::trace_error_exit;
|
|
|
|
use libimagutil::debug_result::*;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
|
|
|
use error::StoreError;
|
|
|
|
use error::StoreErrorKind;
|
2016-01-28 18:40:58 +00:00
|
|
|
use util::build_toml_header;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
|
|
|
type Result<T> = RResult<T, StoreError>;
|
2016-01-23 15:29:04 +00:00
|
|
|
|
|
|
|
pub fn create(rt: &Runtime) {
|
2016-01-24 19:20:04 +00:00
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("create")
|
|
|
|
.map(|scmd| {
|
|
|
|
debug!("Found 'create' subcommand...");
|
|
|
|
|
|
|
|
// unwrap is safe as value is required
|
2016-02-06 10:54:36 +00:00
|
|
|
let path = scmd.value_of("path").or_else(|| scmd.value_of("id"));
|
|
|
|
if path.is_none() {
|
|
|
|
warn!("No ID / Path provided. Exiting now");
|
2016-04-21 11:31:29 +00:00
|
|
|
write!(stderr(), "No ID / Path provided. Exiting now").ok();
|
2016-02-06 10:54:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-08-28 14:22:43 +00:00
|
|
|
let store_path = rt.store().path().clone();
|
|
|
|
let path = match StoreId::new(Some(store_path), PathBuf::from(path.unwrap())) {
|
|
|
|
Err(e) => trace_error_exit(&e, 1),
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2016-01-24 19:20:04 +00:00
|
|
|
debug!("path = {:?}", path);
|
|
|
|
|
2016-01-31 18:01:40 +00:00
|
|
|
if scmd.subcommand_matches("entry").is_some() {
|
2016-08-28 14:22:43 +00:00
|
|
|
debug!("Creating entry from CLI specification");
|
2016-01-31 18:01:40 +00:00
|
|
|
create_from_cli_spec(rt, scmd, &path)
|
|
|
|
.or_else(|_| create_from_source(rt, scmd, &path))
|
|
|
|
.or_else(|_| create_with_content_and_header(rt,
|
|
|
|
&path,
|
|
|
|
String::new(),
|
|
|
|
EntryHeader::new()))
|
|
|
|
} else {
|
2016-08-28 14:22:43 +00:00
|
|
|
debug!("Creating entry");
|
2016-01-31 18:01:40 +00:00
|
|
|
create_with_content_and_header(rt, &path, String::new(), EntryHeader::new())
|
|
|
|
}
|
2016-08-28 14:22:43 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!("Error building Entry");
|
|
|
|
trace_error_exit(&e, 1);
|
|
|
|
})
|
2016-01-24 19:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-28 14:22:43 +00:00
|
|
|
fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {
|
2016-01-24 19:20:04 +00:00
|
|
|
let content = matches.subcommand_matches("entry")
|
2016-05-02 22:57:41 +00:00
|
|
|
.map_or_else(|| {
|
|
|
|
debug!("Didn't find entry subcommand, getting raw content");
|
|
|
|
matches.value_of("from-raw")
|
|
|
|
.map_or_else(String::new, string_from_raw_src)
|
|
|
|
}, |entry_subcommand| {
|
2016-01-24 19:20:04 +00:00
|
|
|
debug!("Found entry subcommand, parsing content");
|
|
|
|
entry_subcommand
|
|
|
|
.value_of("content")
|
2016-05-02 22:57:41 +00:00
|
|
|
.map_or_else(|| {
|
|
|
|
entry_subcommand.value_of("content-from")
|
|
|
|
.map_or_else(String::new, string_from_raw_src)
|
|
|
|
}, String::from)
|
2016-01-24 19:20:04 +00:00
|
|
|
});
|
|
|
|
debug!("Got content with len = {}", content.len());
|
|
|
|
|
2016-01-31 18:01:40 +00:00
|
|
|
let header = matches.subcommand_matches("entry")
|
2016-05-02 22:57:41 +00:00
|
|
|
.map_or_else(EntryHeader::new,
|
|
|
|
|entry_matches| build_toml_header(entry_matches, EntryHeader::new()));
|
2016-01-31 18:01:40 +00:00
|
|
|
|
|
|
|
create_with_content_and_header(rt, path, content, header)
|
2016-01-24 19:20:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 14:22:43 +00:00
|
|
|
fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {
|
2016-01-24 19:20:04 +00:00
|
|
|
let content = matches
|
|
|
|
.value_of("from-raw")
|
2016-05-14 18:12:19 +00:00
|
|
|
.ok_or(StoreError::new(StoreErrorKind::NoCommandlineCall, None))
|
|
|
|
.map(string_from_raw_src);
|
2016-01-24 19:20:04 +00:00
|
|
|
|
|
|
|
if content.is_err() {
|
|
|
|
return content.map(|_| ());
|
|
|
|
}
|
|
|
|
let content = content.unwrap();
|
|
|
|
debug!("Content with len = {}", content.len());
|
|
|
|
|
|
|
|
Entry::from_str(path.clone(), &content[..])
|
2016-08-28 14:22:43 +00:00
|
|
|
.map_dbg_err(|e| format!("Error building entry: {:?}", e))
|
2016-04-21 11:31:29 +00:00
|
|
|
.and_then(|new_e| {
|
|
|
|
let r = rt.store()
|
2016-01-24 19:20:04 +00:00
|
|
|
.create(path.clone())
|
2016-08-28 14:22:43 +00:00
|
|
|
.map_dbg_err(|e| format!("Error in Store::create(): {:?}", e))
|
2016-01-24 19:20:04 +00:00
|
|
|
.map(|mut old_e| {
|
|
|
|
*old_e.deref_mut() = new_e;
|
|
|
|
});
|
|
|
|
|
|
|
|
debug!("Entry build");
|
2016-04-21 11:31:29 +00:00
|
|
|
r
|
2016-01-24 19:20:04 +00:00
|
|
|
})
|
2016-08-28 14:22:43 +00:00
|
|
|
.map_dbg_err(|e| format!("Error storing entry: {:?}", e))
|
2016-01-24 19:20:04 +00:00
|
|
|
.map_err(|serr| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(serr))))
|
|
|
|
}
|
|
|
|
|
2016-01-31 18:01:40 +00:00
|
|
|
fn create_with_content_and_header(rt: &Runtime,
|
2016-08-28 14:22:43 +00:00
|
|
|
path: &StoreId,
|
2016-01-31 18:01:40 +00:00
|
|
|
content: String,
|
|
|
|
header: EntryHeader) -> Result<()>
|
|
|
|
{
|
2016-08-26 10:52:57 +00:00
|
|
|
debug!("Creating entry with content at {:?}", path);
|
2016-01-31 18:01:40 +00:00
|
|
|
rt.store()
|
2016-08-28 14:22:43 +00:00
|
|
|
.create(path.clone())
|
|
|
|
.map_dbg_err(|e| format!("Error in Store::create(): {:?}", e))
|
2016-01-31 18:01:40 +00:00
|
|
|
.map(|mut element| {
|
|
|
|
{
|
|
|
|
let mut e_content = element.get_content_mut();
|
|
|
|
*e_content = content;
|
|
|
|
debug!("New content set");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut e_header = element.get_header_mut();
|
|
|
|
*e_header = header;
|
|
|
|
debug!("New header set");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(|e| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(e))))
|
|
|
|
}
|
|
|
|
|
2016-01-31 11:39:47 +00:00
|
|
|
fn string_from_raw_src(raw_src: &str) -> String {
|
2016-01-24 19:20:04 +00:00
|
|
|
let mut content = String::new();
|
|
|
|
if raw_src == "-" {
|
|
|
|
debug!("Reading entry from stdin");
|
|
|
|
let res = stdin().read_to_string(&mut content);
|
|
|
|
debug!("Read {:?} bytes", res);
|
|
|
|
} else {
|
|
|
|
debug!("Reading entry from file at {:?}", raw_src);
|
2016-04-21 11:31:29 +00:00
|
|
|
let _ = OpenOptions::new()
|
2016-01-24 19:20:04 +00:00
|
|
|
.read(true)
|
|
|
|
.write(false)
|
|
|
|
.create(false)
|
|
|
|
.open(raw_src)
|
|
|
|
.and_then(|mut f| f.read_to_string(&mut content));
|
|
|
|
}
|
|
|
|
content
|
|
|
|
}
|