2019-03-20 17:56:17 +01:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2018-10-04 22:47:54 +02:00
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
use plume_models::{instance::*, safe_string::SafeString, Connection};
|
2018-10-05 13:48:45 +02:00
|
|
|
use std::env;
|
2018-10-04 22:47:54 +02:00
|
|
|
|
|
|
|
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")
|
2018-10-05 13:48:45 +02:00
|
|
|
.long("domain")
|
|
|
|
.takes_value(true)
|
2018-10-04 22:47:54 +02:00
|
|
|
.help("The domain name of your instance")
|
2018-10-05 13:48:45 +02:00
|
|
|
).arg(Arg::with_name("name")
|
2018-10-04 22:47:54 +02:00
|
|
|
.short("n")
|
2018-10-05 13:48:45 +02:00
|
|
|
.long("name")
|
|
|
|
.takes_value(true)
|
2018-10-04 22:47:54 +02:00
|
|
|
.help("The name of your instance")
|
2018-10-05 13:48:45 +02:00
|
|
|
).arg(Arg::with_name("default-license")
|
2018-10-04 22:47:54 +02:00
|
|
|
.short("l")
|
2018-10-05 13:48:45 +02:00
|
|
|
.long("default-license")
|
|
|
|
.takes_value(true)
|
2018-10-04 22:47:54 +02:00
|
|
|
.help("The license that will be used by default for new articles on this instance")
|
2018-10-05 13:48:45 +02:00
|
|
|
).arg(Arg::with_name("private")
|
2018-10-04 22:47:54 +02:00
|
|
|
.short("p")
|
2018-10-05 13:48:45 +02:00
|
|
|
.long("private")
|
|
|
|
.help("Closes the registrations on this instance")
|
|
|
|
).about("Create a new local instance"))
|
2018-10-04 22:47:54 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 13:35:58 +02:00
|
|
|
pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
2018-10-04 22:47:54 +02:00
|
|
|
let conn = conn;
|
|
|
|
match args.subcommand() {
|
|
|
|
("new", Some(x)) => new(x, conn),
|
2019-03-21 23:25:22 +01:00
|
|
|
("", None) => command().print_help().unwrap(),
|
2019-01-29 00:09:59 +01:00
|
|
|
_ => println!("Unknown subcommand"),
|
2018-10-04 22:47:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:35:58 +02:00
|
|
|
fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
2019-03-20 17:56:17 +01:00
|
|
|
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_else(|| String::from("CC-BY-SA"));
|
2018-10-04 22:47:54 +02:00
|
|
|
let open_reg = !args.is_present("private");
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
Instance::insert(
|
|
|
|
conn,
|
|
|
|
NewInstance {
|
|
|
|
public_domain: domain,
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Couldn't save instance");
|
2021-12-05 12:22:32 +01:00
|
|
|
Instance::cache_local(conn);
|
2021-11-24 14:13:58 +01:00
|
|
|
Instance::create_local_instance_user(conn).expect("Couldn't save local instance user");
|
2018-10-04 22:47:54 +02:00
|
|
|
}
|