2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-01-24 19:20:04 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::io::stdin;
|
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
|
|
use clap::ArgMatches;
|
2016-11-17 11:41:37 +00:00
|
|
|
use toml::Value;
|
2018-10-30 17:40:52 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::err_msg;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
2016-01-23 15:29:04 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-01-24 19:20:04 +00:00
|
|
|
use libimagstore::store::Entry;
|
2016-08-28 14:22:43 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2018-02-11 18:37:52 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2016-08-28 14:22:43 +00:00
|
|
|
use libimagutil::debug_result::*;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
2016-01-28 18:40:58 +00:00
|
|
|
use util::build_toml_header;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
2016-01-23 15:29:04 +00:00
|
|
|
pub fn create(rt: &Runtime) {
|
2017-09-04 14:33:54 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("create").unwrap();
|
|
|
|
debug!("Found 'create' subcommand...");
|
2016-01-24 19:20:04 +00:00
|
|
|
|
2017-09-04 14:33:54 +00:00
|
|
|
// unwrap is safe as value is required
|
|
|
|
let path = scmd.value_of("path").unwrap();
|
|
|
|
let path = PathBuf::from(path);
|
|
|
|
let store = Some(rt.store().path().clone());
|
2018-02-11 18:37:52 +00:00
|
|
|
let path = StoreId::new(store, path).map_err_trace_exit_unwrap(1);
|
2016-02-06 10:54:36 +00:00
|
|
|
|
2017-09-04 14:33:54 +00:00
|
|
|
debug!("path = {:?}", path);
|
2016-01-24 19:20:04 +00:00
|
|
|
|
2017-09-04 14:33:54 +00:00
|
|
|
if scmd.subcommand_matches("entry").is_some() {
|
|
|
|
debug!("Creating entry from CLI specification");
|
|
|
|
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(),
|
|
|
|
Entry::default_header()))
|
|
|
|
} else {
|
|
|
|
debug!("Creating entry");
|
|
|
|
create_with_content_and_header(rt, &path, String::new(),
|
|
|
|
Entry::default_header())
|
|
|
|
}
|
2018-02-11 18:37:52 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-11-02 10:08:22 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(&path).unwrap_or_exit();
|
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-11-17 11:41:37 +00:00
|
|
|
.map_or_else(|| Entry::default_header(),
|
|
|
|
|entry_matches| build_toml_header(entry_matches, Entry::default_header()));
|
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")
|
2018-10-30 17:40:52 +00:00
|
|
|
.ok_or_else(|| err_msg("No Commandline call"))
|
|
|
|
.map(string_from_raw_src)?;
|
2016-01-24 19:20:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
2016-11-17 11:41:37 +00:00
|
|
|
header: Value) -> Result<()>
|
2016-01-31 18:01:40 +00:00
|
|
|
{
|
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| {
|
|
|
|
{
|
2017-08-29 15:22:58 +00:00
|
|
|
let e_content = element.get_content_mut();
|
2016-01-31 18:01:40 +00:00
|
|
|
*e_content = content;
|
|
|
|
debug!("New content set");
|
|
|
|
}
|
|
|
|
{
|
2017-08-29 15:22:58 +00:00
|
|
|
let e_header = element.get_header_mut();
|
2016-01-31 18:01:40 +00:00
|
|
|
*e_header = header;
|
|
|
|
debug!("New header set");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2017-09-03 20:00:34 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::create;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
make_mock_app! {
|
2017-09-05 15:38:02 +00:00
|
|
|
app "imag-store";
|
2017-09-03 20:00:34 +00:00
|
|
|
modulename mock;
|
2018-03-03 12:51:55 +00:00
|
|
|
version env!("CARGO_PKG_VERSION");
|
2017-09-05 15:38:02 +00:00
|
|
|
with help "imag-store mocking app";
|
2017-09-03 20:00:34 +00:00
|
|
|
}
|
|
|
|
use self::mock::generate_test_runtime;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_simple() {
|
|
|
|
let test_name = "test_create_simple";
|
2017-09-05 15:38:02 +00:00
|
|
|
let rt = generate_test_runtime(vec!["create", "test_create_simple"]).unwrap();
|
2017-09-03 20:00:34 +00:00
|
|
|
|
|
|
|
create(&rt);
|
|
|
|
|
|
|
|
let e = rt.store().get(PathBuf::from(test_name));
|
|
|
|
assert!(e.is_ok());
|
|
|
|
let e = e.unwrap();
|
|
|
|
assert!(e.is_some());
|
|
|
|
let e = e.unwrap();
|
|
|
|
|
|
|
|
let version = e.get_header().read("imag.version").map(Option::unwrap).unwrap();
|
2018-03-03 12:51:55 +00:00
|
|
|
assert_eq!(Value::String(String::from(env!("CARGO_PKG_VERSION"))), *version);
|
2017-09-03 20:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|