Add implementation for create subcommand

This commit is contained in:
Matthias Beyer 2018-04-11 10:43:29 +02:00
parent 8dd3a57114
commit 28f882e6c7
3 changed files with 55 additions and 4 deletions

View file

@ -23,7 +23,7 @@ toml-query = "0.6"
is-match = "0.1"
version = "2.0.1"
libimagentrylink = { version = "0.7.0", path = "../../../lib/entry/libimagentrylink" }
libimagentryedit = { version = "0.7.0", path = "../../../lib/entry/libimagentryedit" }
libimagentrymarkdown = { version = "0.7.0", path = "../../../lib/entry/libimagentrymarkdown" }
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }

View file

@ -24,6 +24,7 @@ extern crate clap;
extern crate libimagerror;
extern crate libimagstore;
extern crate libimagwiki;
extern crate libimagentryedit;
use std::io::Write;
@ -34,6 +35,7 @@ use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
use libimagstore::storeid::IntoStoreId;
use libimagwiki::store::WikiStore;
use libimagentryedit::edit::{Edit, EditHeader};
mod ui;
use ui::build_ui;
@ -131,7 +133,39 @@ fn idof(rt: &Runtime, wiki_name: &str) {
}
fn create(rt: &Runtime, wiki_name: &str) {
unimplemented!()
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 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
.store()
.create_wiki(&name, Some(&main))
.map_err_trace_exit_unwrap(1)
.get_entry(&main)
.map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| {
error!("Main page '{}' was not created. This seems to be a bug!", main);
::std::process::exit(1);
});
if edit {
if edit_header {
let _ = mainpage.edit_header_and_content(rt).map_err_trace_exit_unwrap(1);
} else {
let _ = mainpage.edit_content(rt).map_err_trace_exit_unwrap(1);
}
}
if scmd.is_present(printid_flag) {
let out = rt.stdout();
let mut lock = out.lock();
let id = mainpage.get_location();
writeln!(lock, "{}", id).to_exit_code().unwrap_or_exit()
}
}
fn delete(rt: &Runtime, wiki_name: &str) {

View file

@ -70,7 +70,15 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(true)
.multiple(false)
.value_name("NAME")
.help("Create the entry under this name. Namespaces ('foo/bar') are allowed."))
.help("Name of the wiki"))
.arg(Arg::with_name("create-mainpagename")
.long("mainpage")
.short("M")
.takes_value(true)
.required(false)
.multiple(false)
.help("Name of the main page. Currently only for the first page to be created. Defaults to 'main'."))
.arg(Arg::with_name("create-noedit")
.long("no-edit")
@ -78,7 +86,16 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.takes_value(false)
.required(false)
.multiple(false)
.help("Do not call the editor on the newly created entry."))
.help("Do not call the editor on the newly created entry.")
.conflicts_with("create-editheader"))
.arg(Arg::with_name("create-editheader")
.long("header")
.takes_value(false)
.required(false)
.multiple(false)
.help("Do edit header when editing main page entry.")
.conflicts_with("create-noedit"))
.arg(Arg::with_name("create-printid")
.long("print-id")