Plume/plume-cli/src/main.rs

59 lines
1.8 KiB
Rust
Raw Normal View History

2020-01-21 07:02:03 +01:00
use dotenv;
use clap::App;
use diesel::Connection;
use plume_models::{instance::Instance, Connection as Conn, CONFIG};
use std::io::{self, prelude::*};
mod instance;
mod migration;
mod search;
2019-03-20 17:56:17 +01:00
mod users;
fn main() {
let mut app = App::new("Plume CLI")
.bin_name("plm")
.version(env!("CARGO_PKG_VERSION"))
.about("Collection of tools to manage your Plume instance.")
2018-10-05 14:09:04 +02:00
.subcommand(instance::command())
.subcommand(migration::command())
.subcommand(search::command())
.subcommand(users::command());
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(),
}
let conn = Conn::establish(CONFIG.database_url.as_str());
let _ = conn.as_ref().map(|conn| Instance::cache_local(conn));
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."))
}
("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."))
}
("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"),
};
}
pub fn ask_for(something: &str) -> String {
print!("{}: ", something);
io::stdout().flush().expect("Couldn't flush STDOUT");
let mut input = String::new();
2019-03-20 17:56:17 +01:00
io::stdin()
.read_line(&mut input)
.expect("Unable to read line");
input.retain(|c| c != '\n');
input
}