2020-01-21 07:02:03 +01:00
|
|
|
use dotenv;
|
2018-10-04 22:47:54 +02:00
|
|
|
|
|
|
|
use clap::App;
|
2018-10-06 13:35:58 +02:00
|
|
|
use diesel::Connection;
|
2019-05-10 22:59:34 +02:00
|
|
|
use plume_models::{instance::Instance, Connection as Conn, CONFIG};
|
2018-10-05 13:48:45 +02:00
|
|
|
use std::io::{self, prelude::*};
|
2018-10-04 22:47:54 +02:00
|
|
|
|
|
|
|
mod instance;
|
2019-04-29 16:30:20 +02:00
|
|
|
mod migration;
|
2018-12-02 17:37:51 +01:00
|
|
|
mod search;
|
2019-03-20 17:56:17 +01:00
|
|
|
mod users;
|
2018-10-04 22:47:54 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut app = App::new("Plume CLI")
|
|
|
|
.bin_name("plm")
|
2018-10-06 19:33:10 +02:00
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
2018-10-04 22:47:54 +02:00
|
|
|
.about("Collection of tools to manage your Plume instance.")
|
2018-10-05 14:09:04 +02:00
|
|
|
.subcommand(instance::command())
|
2019-04-29 16:30:20 +02:00
|
|
|
.subcommand(migration::command())
|
|
|
|
.subcommand(search::command())
|
|
|
|
.subcommand(users::command());
|
2018-10-04 22:47:54 +02:00
|
|
|
let matches = app.clone().get_matches();
|
|
|
|
|
2019-07-03 09:30:44 +02:00
|
|
|
match dotenv::dotenv() {
|
|
|
|
Ok(path) => println!("Configuration read from {}", path.display()),
|
|
|
|
Err(ref e) if e.not_found() => eprintln!("no .env was found"),
|
|
|
|
e => e.map(|_| ()).unwrap(),
|
|
|
|
}
|
2019-03-21 10:30:33 +01:00
|
|
|
let conn = Conn::establish(CONFIG.database_url.as_str());
|
2019-05-10 22:59:34 +02:00
|
|
|
let _ = conn.as_ref().map(|conn| Instance::cache_local(conn));
|
2018-10-04 22:47:54 +02:00
|
|
|
|
|
|
|
match matches.subcommand() {
|
2019-03-20 17:56:17 +01:00
|
|
|
("instance", Some(args)) => {
|
|
|
|
instance::run(args, &conn.expect("Couldn't connect to the database."))
|
|
|
|
}
|
2019-04-29 16:30:20 +02:00
|
|
|
("migration", Some(args)) => {
|
|
|
|
migration::run(args, &conn.expect("Couldn't connect to the database."))
|
2019-03-20 17:56:17 +01:00
|
|
|
}
|
|
|
|
("search", Some(args)) => {
|
|
|
|
search::run(args, &conn.expect("Couldn't connect to the database."))
|
|
|
|
}
|
2019-04-29 16:30:20 +02:00
|
|
|
("users", Some(args)) => {
|
|
|
|
users::run(args, &conn.expect("Couldn't connect to the database."))
|
|
|
|
}
|
2019-03-20 17:56:17 +01:00
|
|
|
_ => app.print_help().expect("Couldn't print help"),
|
2018-10-04 22:47:54 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ask_for(something: &str) -> String {
|
2018-10-05 13:48:45 +02:00
|
|
|
print!("{}: ", something);
|
|
|
|
io::stdout().flush().expect("Couldn't flush STDOUT");
|
2018-10-04 22:47:54 +02:00
|
|
|
let mut input = String::new();
|
2019-03-20 17:56:17 +01:00
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut input)
|
|
|
|
.expect("Unable to read line");
|
2018-10-04 22:47:54 +02:00
|
|
|
input.retain(|c| c != '\n');
|
|
|
|
input
|
|
|
|
}
|