From 0da5d2e9275d1e102c25671b7931ed6458452cb3 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 5 Oct 2018 12:48:45 +0100 Subject: [PATCH] Improve the plm instance new command Add long arguments Fix the help messages Actually write to StdOut when needed Fallback to BASE_URL first if domain is not specified --- plume-cli/Cargo.toml | 3 ++- plume-cli/src/instance.rs | 25 ++++++++++++++++--------- plume-cli/src/main.rs | 6 +++--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/plume-cli/Cargo.toml b/plume-cli/Cargo.toml index 3b41529e..3d5ddae9 100644 --- a/plume-cli/Cargo.toml +++ b/plume-cli/Cargo.toml @@ -1,10 +1,11 @@ [package] name = "plume-cli" -version = "0.1.0" +version = "0.2.0" authors = ["Bat' "] [[bin]] name = "plm" +path = "src/main.rs" [dependencies] clap = "2.32" diff --git a/plume-cli/src/instance.rs b/plume-cli/src/instance.rs index cb4f0f59..bc6c1b51 100644 --- a/plume-cli/src/instance.rs +++ b/plume-cli/src/instance.rs @@ -1,6 +1,7 @@ use clap::{Arg, ArgMatches, App, SubCommand}; use diesel::PgConnection; +use std::env; use plume_models::{ instance::*, safe_string::SafeString, @@ -12,20 +13,24 @@ pub fn command<'a, 'b>() -> App<'a, 'b> { .subcommand(SubCommand::with_name("new") .arg(Arg::with_name("domain") .short("d") - .takes_value(true)) + .long("domain") + .takes_value(true) .help("The domain name of your instance") - .arg(Arg::with_name("name") + ).arg(Arg::with_name("name") .short("n") - .takes_value(true)) + .long("name") + .takes_value(true) .help("The name of your instance") - .arg(Arg::with_name("default-license") + ).arg(Arg::with_name("default-license") .short("l") - .takes_value(true)) + .long("default-license") + .takes_value(true) .help("The license that will be used by default for new articles on this instance") - .arg(Arg::with_name("private") + ).arg(Arg::with_name("private") .short("p") - .help("Closes the registrations on this instance")) - .help("Create a new local instance")) + .long("private") + .help("Closes the registrations on this instance") + ).about("Create a new local instance")) } pub fn run<'a>(args: &ArgMatches<'a>, conn: &PgConnection) { @@ -37,7 +42,9 @@ pub fn run<'a>(args: &ArgMatches<'a>, conn: &PgConnection) { } 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 domain = args.value_of("domain").map(String::from) + .unwrap_or_else(|| env::var("BASE_URL") + .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"); diff --git a/plume-cli/src/main.rs b/plume-cli/src/main.rs index 83482996..2dc97b44 100644 --- a/plume-cli/src/main.rs +++ b/plume-cli/src/main.rs @@ -5,7 +5,7 @@ extern crate plume_models; use clap::App; use diesel::{Connection, PgConnection}; -use std::io::{self, Write}; +use std::io::{self, prelude::*}; use plume_models::DB_URL; mod instance; @@ -28,8 +28,8 @@ fn main() { } pub fn ask_for(something: &str) -> String { - write!(io::stdout(), "{}", something).ok(); - write!(io::stdout(), ": ").ok(); + print!("{}: ", something); + io::stdout().flush().expect("Couldn't flush STDOUT"); let mut input = String::new(); io::stdin().read_line(&mut input).expect("Unable to read line"); input.retain(|c| c != '\n');