Rename "create" command to "create-wiki", introduce "create" for creating entries

This commit is contained in:
Matthias Beyer 2018-04-14 20:23:58 +02:00
parent 2c07ad9a3a
commit caf0e63b35
2 changed files with 87 additions and 28 deletions

View file

@ -31,6 +31,7 @@ extern crate libimagentrylink;
use std::io::Write; use std::io::Write;
use clap::ArgMatches;
use regex::Regex; use regex::Regex;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
@ -41,6 +42,7 @@ use libimagerror::io::ToExitCode;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagwiki::store::WikiStore; use libimagwiki::store::WikiStore;
use libimagwiki::wiki::Wiki;
use libimagentryedit::edit::{Edit, EditHeader}; use libimagentryedit::edit::{Edit, EditHeader};
mod ui; mod ui;
@ -59,6 +61,7 @@ fn main() {
Some("ids") => ids(&rt, wiki_name), Some("ids") => ids(&rt, wiki_name),
Some("idof") => idof(&rt, wiki_name), Some("idof") => idof(&rt, wiki_name),
Some("create") => create(&rt, wiki_name), Some("create") => create(&rt, wiki_name),
Some("create-wiki") => create_wiki(&rt, wiki_name),
Some("delete") => delete(&rt, wiki_name), Some("delete") => delete(&rt, wiki_name),
Some("grep") => grep(&rt, wiki_name), Some("grep") => grep(&rt, wiki_name),
Some(other) => { Some(other) => {
@ -141,34 +144,53 @@ fn idof(rt: &Runtime, wiki_name: &str) {
fn create(rt: &Runtime, wiki_name: &str) { fn create(rt: &Runtime, wiki_name: &str) {
let scmd = rt.cli().subcommand_matches("create").unwrap(); // safed by clap 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 name = String::from(scmd.value_of("create-name").unwrap()); // safe by clap
let main = String::from(scmd.value_of("create-mainpagename").unwrap_or("main"));
let edit = !scmd.is_present("create-noedit");
let edit_header = scmd.is_present("create-editheader");
let prid = !scmd.is_present("create-printid");
let mut mainpage = rt let wiki = rt
.store() .store()
.create_wiki(&name, Some(&main)) .get_wiki(&wiki_name)
.map_err_trace_exit_unwrap(1)
.get_entry(&main)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { .unwrap_or_else(|| {
error!("Main page '{}' was not created. This seems to be a bug!", main); error!("No wiki '{}' found", wiki_name);
::std::process::exit(1); ::std::process::exit(1)
}); });
if edit { create_in_wiki(rt, &name, &wiki, scmd,
if edit_header { "create-noedit", "create-editheader", "create-printid");
let _ = mainpage.edit_header_and_content(rt).map_err_trace_exit_unwrap(1); }
fn create_wiki(rt: &Runtime, wiki_name: &str) {
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 main = String::from(scmd.value_of("create-wiki-mainpagename").unwrap_or("main"));
let wiki = rt.store().create_wiki(&wiki_name, Some(&main)).map_err_trace_exit_unwrap(1);
create_in_wiki(rt, &main, &wiki, scmd,
"create-wiki-noedit", "create-wiki-editheader", "create-wiki-printid");
}
fn create_in_wiki(rt: &Runtime,
entry_name: &str,
wiki: &Wiki,
scmd: &ArgMatches,
noedit_flag: &'static str,
editheader_flag: &'static str,
printid_flag: &'static str)
{
let mut entry = wiki.create_entry(entry_name).map_err_trace_exit_unwrap(1);
if !scmd.is_present(noedit_flag) {
if scmd.is_present(editheader_flag) {
let _ = entry.edit_header_and_content(rt).map_err_trace_exit_unwrap(1);
} else { } else {
let _ = mainpage.edit_content(rt).map_err_trace_exit_unwrap(1); let _ = entry.edit_content(rt).map_err_trace_exit_unwrap(1);
} }
} }
if scmd.is_present(printid_flag) { if scmd.is_present(printid_flag) {
let out = rt.stdout(); let out = rt.stdout();
let mut lock = out.lock(); let mut lock = out.lock();
let id = mainpage.get_location(); let id = entry.get_location();
writeln!(lock, "{}", id).to_exit_code().unwrap_or_exit() writeln!(lock, "{}", id).to_exit_code().unwrap_or_exit()
} }

View file

@ -61,10 +61,10 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.help("Add the entry under this name. The name must be unique, namespaces ('foo/bar') are allowed.")) .help("Add the entry under this name. The name must be unique, namespaces ('foo/bar') are allowed."))
) )
.subcommand(SubCommand::with_name("create") .subcommand(SubCommand::with_name("create-wiki")
.about("Add wiki entry") .about("Create wiki")
.version("0.1") .version("0.1")
.arg(Arg::with_name("create-name") .arg(Arg::with_name("create-wiki-name")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(true)
@ -72,7 +72,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.value_name("NAME") .value_name("NAME")
.help("Name of the wiki")) .help("Name of the wiki"))
.arg(Arg::with_name("create-mainpagename") .arg(Arg::with_name("create-wiki-mainpagename")
.long("mainpage") .long("mainpage")
.short("M") .short("M")
.takes_value(true) .takes_value(true)
@ -80,6 +80,43 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.help("Name of the main page. Currently only for the first page to be created. Defaults to 'main'.")) .help("Name of the main page. Currently only for the first page to be created. Defaults to 'main'."))
.arg(Arg::with_name("create-wiki-noedit")
.long("no-edit")
.short("E")
.takes_value(false)
.required(false)
.multiple(false)
.help("Do not call the editor on the newly created entry.")
.conflicts_with("create-wiki-editheader"))
.arg(Arg::with_name("create-wiki-editheader")
.long("header")
.takes_value(false)
.required(false)
.multiple(false)
.help("Do edit header when editing main page entry.")
.conflicts_with("create-wiki-noedit"))
.arg(Arg::with_name("create-wiki-printid")
.long("print-id")
.short("I")
.takes_value(false)
.required(false)
.multiple(false)
.help("Print the store id after creating"))
)
.subcommand(SubCommand::with_name("create")
.about("Add wiki entry")
.version("0.1")
.arg(Arg::with_name("create-name")
.index(1)
.takes_value(true)
.required(true)
.multiple(false)
.help("Name of the page."))
.arg(Arg::with_name("create-noedit") .arg(Arg::with_name("create-noedit")
.long("no-edit") .long("no-edit")
.short("E") .short("E")
@ -94,7 +131,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.takes_value(false) .takes_value(false)
.required(false) .required(false)
.multiple(false) .multiple(false)
.help("Do edit header when editing main page entry.") .help("Do edit header when editing entry.")
.conflicts_with("create-noedit")) .conflicts_with("create-noedit"))
.arg(Arg::with_name("create-printid") .arg(Arg::with_name("create-printid")