From 6723432e52d0ecf579f5435e44bf0a1366dfad81 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 4 Oct 2018 21:47:54 +0100 Subject: [PATCH] CLI tools - New sub-crate: plume-cli - Creates a binary called 'plm' - Uses clap to parse CLI arguments - Add a first command: plm instance new (to init the local instance) --- plume-cli/Cargo.toml | 18 +++++++++++++ plume-cli/src/instance.rs | 56 +++++++++++++++++++++++++++++++++++++++ plume-cli/src/main.rs | 37 ++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 plume-cli/Cargo.toml create mode 100644 plume-cli/src/instance.rs create mode 100644 plume-cli/src/main.rs diff --git a/plume-cli/Cargo.toml b/plume-cli/Cargo.toml new file mode 100644 index 0000000..3b41529 --- /dev/null +++ b/plume-cli/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "plume-cli" +version = "0.1.0" +authors = ["Bat' "] + +[[bin]] +name = "plm" + +[dependencies] +clap = "2.32" +dotenv = "0.13" + +[dependencies.diesel] +features = ["postgres", "r2d2", "chrono"] +version = "*" + +[dependencies.plume-models] +path = "../plume-models" diff --git a/plume-cli/src/instance.rs b/plume-cli/src/instance.rs new file mode 100644 index 0000000..cb4f0f5 --- /dev/null +++ b/plume-cli/src/instance.rs @@ -0,0 +1,56 @@ +use clap::{Arg, ArgMatches, App, SubCommand}; +use diesel::PgConnection; + +use plume_models::{ + instance::*, + safe_string::SafeString, +}; + +pub fn command<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("instance") + .about("Manage instances") + .subcommand(SubCommand::with_name("new") + .arg(Arg::with_name("domain") + .short("d") + .takes_value(true)) + .help("The domain name of your instance") + .arg(Arg::with_name("name") + .short("n") + .takes_value(true)) + .help("The name of your instance") + .arg(Arg::with_name("default-license") + .short("l") + .takes_value(true)) + .help("The license that will be used by default for new articles on this instance") + .arg(Arg::with_name("private") + .short("p") + .help("Closes the registrations on this instance")) + .help("Create a new local instance")) +} + +pub fn run<'a>(args: &ArgMatches<'a>, conn: &PgConnection) { + let conn = conn; + match args.subcommand() { + ("new", Some(x)) => new(x, conn), + _ => println!("Unknwon subcommand"), + } +} + +fn new<'a>(args: &ArgMatches<'a>, conn: &PgConnection) { + let domain = args.value_of("domain").map(String::from).unwrap_or_else(|| super::ask_for("Domain name")); + let name = args.value_of("name").map(String::from).unwrap_or_else(|| super::ask_for("Instance name")); + let license = args.value_of("default-license").map(String::from).unwrap_or(String::from("CC-0")); + let open_reg = !args.is_present("private"); + + Instance::insert(conn, NewInstance { + public_domain: domain, + name: name, + local: true, + long_description: SafeString::new(""), + short_description: SafeString::new(""), + default_license: license, + open_registrations: open_reg, + short_description_html: String::new(), + long_description_html: String::new() + }); +} diff --git a/plume-cli/src/main.rs b/plume-cli/src/main.rs new file mode 100644 index 0000000..8348299 --- /dev/null +++ b/plume-cli/src/main.rs @@ -0,0 +1,37 @@ +extern crate clap; +extern crate diesel; +extern crate dotenv; +extern crate plume_models; + +use clap::App; +use diesel::{Connection, PgConnection}; +use std::io::{self, Write}; +use plume_models::DB_URL; + +mod instance; + +fn main() { + let mut app = App::new("Plume CLI") + .bin_name("plm") + .version("0.2.0") + .about("Collection of tools to manage your Plume instance.") + .subcommand(instance::command()); + let matches = app.clone().get_matches(); + + dotenv::dotenv().ok(); + let conn = PgConnection::establish(DB_URL.as_str()); + + match matches.subcommand() { + ("instance", Some(args)) => instance::run(args, &conn.expect("Couldn't connect to the database.")), + _ => app.print_help().unwrap() + }; +} + +pub fn ask_for(something: &str) -> String { + write!(io::stdout(), "{}", something).ok(); + write!(io::stdout(), ": ").ok(); + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Unable to read line"); + input.retain(|c| c != '\n'); + input +}