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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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
//
#![deny(
non_camel_case_types,
non_snake_case,
path_statements,
trivial_numeric_casts,
unstable_features,
unused_allocation,
unused_import_braces,
unused_imports,
unused_must_use,
unused_mut,
unused_qualifications,
while_true,
)]
extern crate clap;
#[macro_use] extern crate log;
extern crate handlebars;
extern crate tempfile;
extern crate toml;
extern crate toml_query;
extern crate libimagentryview;
extern crate libimagerror;
#[macro_use] extern crate libimagrt;
extern crate libimagstore;
use std::collections::BTreeMap;
use std::io::Write;
use std::io::Read;
use std::path::PathBuf;
use std::process::Command;
use std::process::exit;
use handlebars::Handlebars;
use toml_query::read::TomlValueReadTypeExt;
use libimagrt::setup::generate_runtime_setup;
use libimagrt::runtime::Runtime;
use libimagerror::str::ErrFromStr;
use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator;
use libimagentryview::builtin::stdout::StdoutViewer;
use libimagentryview::viewer::Viewer;
use libimagentryview::error::ViewError as VE;
use libimagstore::storeid::IntoStoreId;
use libimagstore::error::StoreError;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
mod ui;
use ui::build_ui;
fn main() {
let version = make_imag_version!();
let rt = generate_runtime_setup( "imag-view",
&version,
"View entries (readonly)",
build_ui);
let entry_ids = entry_ids(&rt);
let view_header = rt.cli().is_present("view-header");
let hide_content = rt.cli().is_present("not-view-content");
if rt.cli().is_present("in") {
let files = entry_ids
.into_iter()
.into_get_iter(rt.store())
.map(|e| {
e.map_err_trace_exit_unwrap(1)
.ok_or_else(|| String::from("Entry not found"))
.map_err(StoreError::from)
.map_err_trace_exit_unwrap(1)
})
.map(|entry| create_tempfile_for(&entry, view_header, hide_content))
.collect::<Vec<_>>();
let mut command = {
let viewer = rt
.cli()
.value_of("in")
.ok_or_else::<VE, _>(|| "No viewer given".to_owned().into())
.map_err_trace_exit_unwrap(1);
let config = rt
.config()
.ok_or_else::<VE, _>(|| "No configuration, cannot continue".to_owned().into())
.map_err_trace_exit_unwrap(1);
let query = format!("view.viewers.{}", viewer);
let viewer_template = config
.read_string(&query)
.map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| {
error!("Cannot find '{}' in config", query);
exit(1)
});
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(::handlebars::no_escape);
let _ = handlebars
.register_template_string("template", viewer_template)
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1);
let mut data = BTreeMap::new();
let file_paths = files
.iter()
.map(|&(_, ref path)| path.clone())
.collect::<Vec<String>>()
.join(" ");
data.insert("entries", file_paths);
let call = handlebars
.render("template", &data)
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1);
let mut elems = call.split_whitespace();
let command_string = elems
.next()
.ok_or::<VE>("No command".to_owned().into())
.map_err_trace_exit_unwrap(1);
let mut cmd = Command::new(command_string);
for arg in elems {
cmd.arg(arg);
}
cmd
};
debug!("Calling: {:?}", command);
if !command
.status()
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1)
.success()
{
exit(1)
}
drop(files);
} else {
let viewer = StdoutViewer::new(view_header, !hide_content);
entry_ids
.into_iter()
.into_get_iter(rt.store())
.map(|e| {
e.map_err_trace_exit_unwrap(1)
.ok_or_else(|| String::from("Entry not found"))
.map_err(StoreError::from)
.map_err_trace_exit_unwrap(1)
})
.for_each(|e| {
viewer.view_entry(&e).map_err_trace_exit_unwrap(1);
});
}
}
fn entry_ids(rt: &Runtime) -> Vec<StoreId> {
match rt.cli().values_of("id") {
Some(pathes) => pathes
.map(PathBuf::from)
.map(PathBuf::into_storeid)
.trace_unwrap_exit(1)
.collect(),
None => if rt.cli().is_present("entries-from-stdin") {
let stdin = rt.stdin().unwrap_or_else(|| {
error!("Cannot get handle to stdin");
::std::process::exit(1)
});
let mut buf = String::new();
let _ = stdin.lock().read_to_string(&mut buf).unwrap_or_else(|_| {
error!("Failed to read from stdin");
::std::process::exit(1)
});
buf.lines()
.map(PathBuf::from)
.map(PathBuf::into_storeid)
.trace_unwrap_exit(1)
.collect()
} else {
error!("Something weird happened. I was not able to find the path of the entries to edit");
::std::process::exit(1)
}
}
}
fn create_tempfile_for<'a>(entry: &FileLockEntry<'a>, view_header: bool, hide_content: bool)
-> (tempfile::NamedTempFile, String)
{
let mut tmpfile = tempfile::NamedTempFile::new()
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1);
if view_header {
let hdr = toml::ser::to_string_pretty(entry.get_header())
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1);
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1);
}
if !hide_content {
let _ = tmpfile.write(entry.get_content().as_bytes())
.err_from_str()
.map_err(VE::from)
.map_err_trace_exit_unwrap(1);
}
let file_path = tmpfile
.path()
.to_str()
.map(String::from)
.ok_or::<VE>("Cannot build path".to_owned().into())
.map_err_trace_exit_unwrap(1);
(tmpfile, file_path)
}
|