mirror of
https://git.joinplu.me/Plume/Plume.git
synced 2024-11-29 23:11:02 +00:00
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)
This commit is contained in:
parent
b464671cf0
commit
6723432e52
3 changed files with 111 additions and 0 deletions
18
plume-cli/Cargo.toml
Normal file
18
plume-cli/Cargo.toml
Normal file
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "plume-cli"
|
||||
version = "0.1.0"
|
||||
authors = ["Bat' <baptiste@gelez.xyz>"]
|
||||
|
||||
[[bin]]
|
||||
name = "plm"
|
||||
|
||||
[dependencies]
|
||||
clap = "2.32"
|
||||
dotenv = "0.13"
|
||||
|
||||
[dependencies.diesel]
|
||||
features = ["postgres", "r2d2", "chrono"]
|
||||
version = "*"
|
||||
|
||||
[dependencies.plume-models]
|
||||
path = "../plume-models"
|
56
plume-cli/src/instance.rs
Normal file
56
plume-cli/src/instance.rs
Normal file
|
@ -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()
|
||||
});
|
||||
}
|
37
plume-cli/src/main.rs
Normal file
37
plume-cli/src/main.rs
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue