2017-10-28 09:55:54 +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
|
2017-10-28 09:55:54 +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
|
|
|
|
//
|
|
|
|
|
2018-09-27 06:13:58 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2017-10-28 09:55:54 +00:00
|
|
|
extern crate clap;
|
2018-04-14 14:01:40 +00:00
|
|
|
extern crate regex;
|
|
|
|
extern crate filters;
|
2017-10-28 09:55:54 +00:00
|
|
|
#[macro_use] extern crate log;
|
|
|
|
|
|
|
|
#[macro_use] extern crate libimagrt;
|
|
|
|
extern crate libimagerror;
|
|
|
|
extern crate libimagstore;
|
|
|
|
extern crate libimagwiki;
|
2018-04-11 08:43:29 +00:00
|
|
|
extern crate libimagentryedit;
|
2018-04-14 13:49:14 +00:00
|
|
|
extern crate libimagentrylink;
|
2018-04-15 08:31:22 +00:00
|
|
|
extern crate libimagutil;
|
2017-10-28 09:55:54 +00:00
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2018-05-01 15:42:47 +00:00
|
|
|
use libimagerror::iter::TraceIterator;
|
2018-04-18 12:30:53 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2017-10-28 09:55:54 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
|
|
|
use libimagerror::io::ToExitCode;
|
|
|
|
use libimagwiki::store::WikiStore;
|
2018-04-11 08:43:29 +00:00
|
|
|
use libimagentryedit::edit::{Edit, EditHeader};
|
2017-10-28 09:55:54 +00:00
|
|
|
|
|
|
|
mod ui;
|
|
|
|
use ui::build_ui;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let version = make_imag_version!();
|
|
|
|
let rt = generate_runtime_setup("imag-wiki",
|
|
|
|
&version,
|
2018-10-30 17:40:53 +00:00
|
|
|
"Personal wiki",
|
2017-10-28 09:55:54 +00:00
|
|
|
build_ui);
|
|
|
|
|
|
|
|
let wiki_name = rt.cli().value_of("wikiname").unwrap_or("default");
|
|
|
|
|
|
|
|
match rt.cli().subcommand_name() {
|
2018-04-14 18:23:58 +00:00
|
|
|
Some("ids") => ids(&rt, wiki_name),
|
|
|
|
Some("idof") => idof(&rt, wiki_name),
|
|
|
|
Some("create") => create(&rt, wiki_name),
|
2018-04-18 12:30:53 +00:00
|
|
|
Some("create-wiki") => create_wiki(&rt),
|
2018-04-14 18:39:56 +00:00
|
|
|
Some("show") => show(&rt, wiki_name),
|
2018-04-14 18:23:58 +00:00
|
|
|
Some("delete") => delete(&rt, wiki_name),
|
|
|
|
Some(other) => {
|
2017-10-28 09:55:54 +00:00
|
|
|
debug!("Unknown command");
|
|
|
|
let _ = rt.handle_unknown_subcommand("imag-wiki", other, rt.cli())
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.code()
|
|
|
|
.map(std::process::exit);
|
|
|
|
}
|
|
|
|
None => warn!("No command"),
|
|
|
|
} // end match scmd
|
|
|
|
} // end main
|
|
|
|
|
|
|
|
fn ids(rt: &Runtime, wiki_name: &str) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("ids").unwrap(); // safed by clap
|
|
|
|
let prefix = if scmd.is_present("ids-full") {
|
|
|
|
format!("{}/", rt.store().path().display())
|
|
|
|
} else {
|
|
|
|
String::from("")
|
|
|
|
};
|
|
|
|
|
|
|
|
let out = rt.stdout();
|
|
|
|
let mut outlock = out.lock();
|
|
|
|
|
|
|
|
rt.store()
|
|
|
|
.get_wiki(wiki_name)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("No wiki '{}' found", wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
|
|
|
.all_ids()
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
2018-05-01 15:42:47 +00:00
|
|
|
.trace_unwrap_exit(1)
|
2017-10-28 09:55:54 +00:00
|
|
|
.for_each(|id| {
|
|
|
|
let _ = writeln!(outlock, "{}{}", prefix, id)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn idof(rt: &Runtime, wiki_name: &str) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("idof").unwrap(); // safed by clap
|
|
|
|
|
|
|
|
let entryname = scmd
|
|
|
|
.value_of("idof-name")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap(); // safed by clap
|
|
|
|
|
|
|
|
let out = rt.stdout();
|
|
|
|
let mut lock = out.lock();
|
|
|
|
|
|
|
|
let _ = rt.store()
|
|
|
|
.get_wiki(wiki_name)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("No wiki '{}' found", wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
|
|
|
.get_entry(&entryname)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.map(|entry| {
|
|
|
|
let id = entry.get_location().clone();
|
|
|
|
let prefix = if scmd.is_present("idof-full") {
|
|
|
|
format!("{}/", rt.store().path().display())
|
|
|
|
} else {
|
|
|
|
String::from("")
|
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(lock, "{}{}", prefix, id).to_exit_code().unwrap_or_exit()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("Entry '{}' in wiki '{}' not found!", entryname, wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(rt: &Runtime, wiki_name: &str) {
|
2018-04-16 08:48:45 +00:00
|
|
|
use libimagwiki::entry::WikiEntry;
|
|
|
|
use libimagutil::warn_result::WarnResult;
|
|
|
|
|
2018-04-14 18:23:58 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("create").unwrap(); // safed by clap
|
|
|
|
let name = String::from(scmd.value_of("create-name").unwrap()); // safe by clap
|
|
|
|
|
|
|
|
let wiki = rt
|
2018-04-11 08:43:29 +00:00
|
|
|
.store()
|
2018-04-14 18:23:58 +00:00
|
|
|
.get_wiki(&wiki_name)
|
2018-04-11 08:43:29 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
2018-04-14 18:23:58 +00:00
|
|
|
error!("No wiki '{}' found", wiki_name);
|
|
|
|
::std::process::exit(1)
|
2018-04-11 08:43:29 +00:00
|
|
|
});
|
|
|
|
|
2018-04-16 08:48:45 +00:00
|
|
|
let mut entry = wiki.create_entry(name).map_err_trace_exit_unwrap(1);
|
2018-04-14 18:23:58 +00:00
|
|
|
|
2018-04-16 08:48:45 +00:00
|
|
|
if !scmd.is_present("create-noedit") {
|
|
|
|
if scmd.is_present("create-editheader") {
|
2018-04-14 18:23:58 +00:00
|
|
|
let _ = entry.edit_header_and_content(rt).map_err_trace_exit_unwrap(1);
|
2018-04-11 08:43:29 +00:00
|
|
|
} else {
|
2018-04-14 18:23:58 +00:00
|
|
|
let _ = entry.edit_content(rt).map_err_trace_exit_unwrap(1);
|
2018-04-11 08:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-15 08:31:22 +00:00
|
|
|
let _ = entry.autolink(rt.store())
|
|
|
|
.map_warn_err_str("Linking has failed. Trying to safe the entry now. Please investigate by hand if this succeeds.")
|
|
|
|
.map_err(|e| {
|
|
|
|
let _ = rt.store().update(&mut entry).map_err_trace_exit_unwrap(1);
|
|
|
|
e
|
|
|
|
})
|
|
|
|
.map_warn_err_str("Safed entry")
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
|
2018-10-17 12:45:58 +00:00
|
|
|
let id = entry.get_location();
|
|
|
|
|
2018-04-16 08:48:45 +00:00
|
|
|
if scmd.is_present("create-printid") {
|
2018-04-11 08:43:29 +00:00
|
|
|
let out = rt.stdout();
|
|
|
|
let mut lock = out.lock();
|
|
|
|
|
|
|
|
writeln!(lock, "{}", id).to_exit_code().unwrap_or_exit()
|
2018-04-14 18:39:56 +00:00
|
|
|
}
|
2018-10-17 12:45:58 +00:00
|
|
|
|
|
|
|
let _ = rt
|
|
|
|
.report_touched(&id)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-04-14 18:39:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 12:30:53 +00:00
|
|
|
fn create_wiki(rt: &Runtime) {
|
2018-10-17 12:45:58 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("create-wiki").unwrap(); // safed by clap
|
|
|
|
let wiki_name = String::from(scmd.value_of("create-wiki-name").unwrap()); // safe by clap
|
|
|
|
let (_, index) = rt.store().create_wiki(&wiki_name).map_err_trace_exit_unwrap(1);
|
|
|
|
|
|
|
|
let _ = rt
|
|
|
|
.report_touched(index.get_location())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-04-16 08:48:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 18:39:56 +00:00
|
|
|
fn show(rt: &Runtime, wiki_name: &str) {
|
2018-04-15 08:12:17 +00:00
|
|
|
use filters::filter::Filter;
|
|
|
|
|
2018-04-14 18:39:56 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("show").unwrap(); // safed by clap
|
2018-04-15 08:12:17 +00:00
|
|
|
|
|
|
|
struct NameFilter(Option<Vec<String>>);
|
|
|
|
impl Filter<String> for NameFilter {
|
|
|
|
fn filter(&self, e: &String) -> bool {
|
|
|
|
match self.0 {
|
|
|
|
Some(ref v) => v.contains(e),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let namefilter = NameFilter(scmd
|
|
|
|
.values_of("show-name")
|
|
|
|
.map(|v| v.map(String::from).collect::<Vec<String>>()));
|
|
|
|
|
2018-04-14 18:39:56 +00:00
|
|
|
let names = scmd
|
|
|
|
.values_of("show-name")
|
|
|
|
.unwrap() // safe by clap
|
|
|
|
.map(String::from)
|
2018-04-15 08:12:17 +00:00
|
|
|
.filter(|e| namefilter.filter(e))
|
2018-04-14 18:39:56 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let wiki = rt
|
|
|
|
.store()
|
|
|
|
.get_wiki(&wiki_name)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("No wiki '{}' found", wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
let out = rt.stdout();
|
|
|
|
let mut outlock = out.lock();
|
|
|
|
|
|
|
|
for name in names {
|
|
|
|
let entry = wiki
|
|
|
|
.get_entry(&name)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("No wiki entry '{}' found in wiki '{}'", name, wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
writeln!(outlock, "{}", entry.get_location())
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
|
|
|
|
writeln!(outlock, "{}", entry.get_content())
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2018-10-17 12:45:58 +00:00
|
|
|
|
|
|
|
let _ = rt
|
|
|
|
.report_touched(entry.get_location())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-04-11 08:43:29 +00:00
|
|
|
}
|
2017-10-28 09:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn delete(rt: &Runtime, wiki_name: &str) {
|
2018-04-14 13:49:14 +00:00
|
|
|
use libimagentrylink::internal::InternalLinker;
|
|
|
|
|
|
|
|
let scmd = rt.cli().subcommand_matches("delete").unwrap(); // safed by clap
|
|
|
|
let name = String::from(scmd.value_of("delete-name").unwrap()); // safe by clap
|
|
|
|
let unlink = !scmd.is_present("delete-no-remove-linkings");
|
|
|
|
|
|
|
|
let wiki = rt
|
|
|
|
.store()
|
|
|
|
.get_wiki(&wiki_name)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("No wiki '{}' found", wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
if unlink {
|
|
|
|
wiki.get_entry(&name)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("No wiki entry '{}' in '{}' found", name, wiki_name);
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
|
|
|
.unlink(rt.store())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = wiki
|
|
|
|
.delete_entry(&name)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-28 09:55:54 +00:00
|
|
|
}
|
|
|
|
|