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
|
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// 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
//
use std::path::PathBuf;
use std::io::stdin;
use std::fs::OpenOptions;
use std::io::Read;
use std::ops::DerefMut;
use clap::ArgMatches;
use toml::Value;
use failure::Fallible as Result;
use failure::err_msg;
use libimagrt::runtime::Runtime;
use libimagstore::store::Entry;
use libimagstore::storeid::StoreId;
use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap;
use libimagutil::debug_result::*;
use util::build_toml_header;
pub fn create(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("create").unwrap();
debug!("Found 'create' subcommand...");
// 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());
let path = StoreId::new(store, path).map_err_trace_exit_unwrap();
debug!("path = {:?}", path);
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())
}
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(&path).unwrap_or_exit();
}
fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {
let content = matches.subcommand_matches("entry")
.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| {
debug!("Found entry subcommand, parsing content");
entry_subcommand
.value_of("content")
.map_or_else(|| {
entry_subcommand.value_of("content-from")
.map_or_else(String::new, string_from_raw_src)
}, String::from)
});
debug!("Got content with len = {}", content.len());
let header = matches.subcommand_matches("entry")
.map_or_else(|| Entry::default_header(),
|entry_matches| build_toml_header(entry_matches, Entry::default_header()));
create_with_content_and_header(rt, path, content, header)
}
fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> {
let content = matches
.value_of("from-raw")
.ok_or_else(|| err_msg("No Commandline call"))
.map(string_from_raw_src)?;
debug!("Content with len = {}", content.len());
Entry::from_str(path.clone(), &content[..])
.map_dbg_err(|e| format!("Error building entry: {:?}", e))
.and_then(|new_e| {
let r = rt.store()
.create(path.clone())
.map_dbg_err(|e| format!("Error in Store::create(): {:?}", e))
.map(|mut old_e| {
*old_e.deref_mut() = new_e;
});
debug!("Entry build");
r
})
.map_dbg_err(|e| format!("Error storing entry: {:?}", e))
}
fn create_with_content_and_header(rt: &Runtime,
path: &StoreId,
content: String,
header: Value) -> Result<()>
{
debug!("Creating entry with content at {:?}", path);
rt.store()
.create(path.clone())
.map_dbg_err(|e| format!("Error in Store::create(): {:?}", e))
.map(|mut element| {
{
let e_content = element.get_content_mut();
*e_content = content;
debug!("New content set");
}
{
let e_header = element.get_header_mut();
*e_header = header;
debug!("New header set");
}
})
}
fn string_from_raw_src(raw_src: &str) -> String {
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);
let _ = OpenOptions::new()
.read(true)
.write(false)
.create(false)
.open(raw_src)
.and_then(|mut f| f.read_to_string(&mut content));
}
content
}
#[cfg(test)]
mod tests {
use super::create;
use std::path::PathBuf;
use toml_query::read::TomlValueReadExt;
use toml::Value;
make_mock_app! {
app "imag-store";
modulename mock;
version env!("CARGO_PKG_VERSION");
with help "imag-store mocking app";
}
use self::mock::generate_test_runtime;
#[test]
fn test_create_simple() {
let test_name = "test_create_simple";
let rt = generate_test_runtime(vec!["create", "test_create_simple"]).unwrap();
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();
assert_eq!(Value::String(String::from(env!("CARGO_PKG_VERSION"))), *version);
}
}
|