Run 'cargo fmt' to format code (#489)
This commit is contained in:
committed by
Baptiste Gelez
parent
732f514da7
commit
b945d1f602
+29
-22
@@ -1,11 +1,7 @@
|
||||
use clap::{Arg, ArgMatches, App, SubCommand};
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
|
||||
use plume_models::{instance::*, safe_string::SafeString, Connection};
|
||||
use std::env;
|
||||
use plume_models::{
|
||||
Connection,
|
||||
instance::*,
|
||||
safe_string::SafeString,
|
||||
};
|
||||
|
||||
pub fn command<'a, 'b>() -> App<'a, 'b> {
|
||||
SubCommand::with_name("instance")
|
||||
@@ -42,22 +38,33 @@ pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
}
|
||||
|
||||
fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
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"));
|
||||
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"));
|
||||
let open_reg = !args.is_present("private");
|
||||
|
||||
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");
|
||||
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");
|
||||
}
|
||||
|
||||
+15
-7
@@ -6,12 +6,12 @@ extern crate rpassword;
|
||||
|
||||
use clap::App;
|
||||
use diesel::Connection;
|
||||
use plume_models::{Connection as Conn, DATABASE_URL};
|
||||
use std::io::{self, prelude::*};
|
||||
use plume_models::{DATABASE_URL, Connection as Conn};
|
||||
|
||||
mod instance;
|
||||
mod users;
|
||||
mod search;
|
||||
mod users;
|
||||
|
||||
fn main() {
|
||||
let mut app = App::new("Plume CLI")
|
||||
@@ -27,10 +27,16 @@ fn main() {
|
||||
let conn = Conn::establish(DATABASE_URL.as_str());
|
||||
|
||||
match matches.subcommand() {
|
||||
("instance", Some(args)) => instance::run(args, &conn.expect("Couldn't connect to the database.")),
|
||||
("users", Some(args)) => users::run(args, &conn.expect("Couldn't connect to the database.")),
|
||||
("search", Some(args)) => search::run(args, &conn.expect("Couldn't connect to the database.")),
|
||||
_ => app.print_help().expect("Couldn't print help")
|
||||
("instance", Some(args)) => {
|
||||
instance::run(args, &conn.expect("Couldn't connect to the database."))
|
||||
}
|
||||
("users", Some(args)) => {
|
||||
users::run(args, &conn.expect("Couldn't connect to the database."))
|
||||
}
|
||||
("search", Some(args)) => {
|
||||
search::run(args, &conn.expect("Couldn't connect to the database."))
|
||||
}
|
||||
_ => app.print_help().expect("Couldn't print help"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -38,7 +44,9 @@ pub fn ask_for(something: &str) -> String {
|
||||
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");
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("Unable to read line");
|
||||
input.retain(|c| c != '\n');
|
||||
input
|
||||
}
|
||||
|
||||
+62
-46
@@ -1,47 +1,56 @@
|
||||
use clap::{Arg, ArgMatches, App, SubCommand};
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use plume_models::{posts::Post, schema::posts, search::Searcher, Connection};
|
||||
use std::fs::{read_dir, remove_file};
|
||||
use std::io::ErrorKind;
|
||||
use std::path::Path;
|
||||
use plume_models::{
|
||||
Connection,
|
||||
posts::Post,
|
||||
schema::posts,
|
||||
search::Searcher,
|
||||
};
|
||||
|
||||
pub fn command<'a, 'b>() -> App<'a, 'b> {
|
||||
SubCommand::with_name("search")
|
||||
.about("Manage search index")
|
||||
.subcommand(SubCommand::with_name("init")
|
||||
.arg(Arg::with_name("path")
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.help("Path to Plume's working directory"))
|
||||
.arg(Arg::with_name("force")
|
||||
.short("f")
|
||||
.long("force")
|
||||
.help("Ignore already using directory")
|
||||
).about("Initialize Plume's internal search engine"))
|
||||
.subcommand(SubCommand::with_name("refill")
|
||||
.arg(Arg::with_name("path")
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.help("Path to Plume's working directory")
|
||||
).about("Regenerate Plume's search index"))
|
||||
.subcommand(SubCommand::with_name("unlock")
|
||||
.arg(Arg::with_name("path")
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.help("Path to Plume's working directory")
|
||||
).about("Release lock on search directory"))
|
||||
.subcommand(
|
||||
SubCommand::with_name("init")
|
||||
.arg(
|
||||
Arg::with_name("path")
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.help("Path to Plume's working directory"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("force")
|
||||
.short("f")
|
||||
.long("force")
|
||||
.help("Ignore already using directory"),
|
||||
)
|
||||
.about("Initialize Plume's internal search engine"),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("refill")
|
||||
.arg(
|
||||
Arg::with_name("path")
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.help("Path to Plume's working directory"),
|
||||
)
|
||||
.about("Regenerate Plume's search index"),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("unlock")
|
||||
.arg(
|
||||
Arg::with_name("path")
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.help("Path to Plume's working directory"),
|
||||
)
|
||||
.about("Release lock on search directory"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
@@ -59,19 +68,25 @@ fn init<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
let force = args.is_present("force");
|
||||
let path = Path::new(path).join("search_index");
|
||||
|
||||
let can_do = match read_dir(path.clone()) { // try to read the directory specified
|
||||
Ok(mut contents) => contents.next().is_none(),
|
||||
Err(e) => if e.kind() == ErrorKind::NotFound {
|
||||
true
|
||||
} else {
|
||||
panic!("Error while initialising search index : {}", e);
|
||||
let can_do = match read_dir(path.clone()) {
|
||||
// try to read the directory specified
|
||||
Ok(mut contents) => contents.next().is_none(),
|
||||
Err(e) => {
|
||||
if e.kind() == ErrorKind::NotFound {
|
||||
true
|
||||
} else {
|
||||
panic!("Error while initialising search index : {}", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
if can_do || force {
|
||||
let searcher = Searcher::create(&path).unwrap();
|
||||
refill(args, conn, Some(searcher));
|
||||
} else {
|
||||
eprintln!("Can't create new index, {} exist and is not empty", path.to_str().unwrap());
|
||||
eprintln!(
|
||||
"Can't create new index, {} exist and is not empty",
|
||||
path.to_str().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,15 +101,16 @@ fn refill<'a>(args: &ArgMatches<'a>, conn: &Connection, searcher: Option<Searche
|
||||
.expect("Post::get_recents: loading error");
|
||||
|
||||
let len = posts.len();
|
||||
for (i,post) in posts.iter().enumerate() {
|
||||
println!("Importing {}/{} : {}", i+1, len, post.title);
|
||||
searcher.update_document(conn, &post).expect("Couldn't import post");
|
||||
for (i, post) in posts.iter().enumerate() {
|
||||
println!("Importing {}/{} : {}", i + 1, len, post.title);
|
||||
searcher
|
||||
.update_document(conn, &post)
|
||||
.expect("Couldn't import post");
|
||||
}
|
||||
println!("Commiting result");
|
||||
searcher.commit();
|
||||
}
|
||||
|
||||
|
||||
fn unlock<'a>(args: &ArgMatches<'a>) {
|
||||
let path = args.value_of("path").unwrap_or(".");
|
||||
let path = Path::new(path).join("search_index/.tantivy-indexer.lock");
|
||||
|
||||
+112
-70
@@ -1,62 +1,78 @@
|
||||
use clap::{Arg, ArgMatches, App, SubCommand};
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
|
||||
use plume_models::{instance::Instance, users::*, Connection};
|
||||
use rpassword;
|
||||
use std::io::{self, Write};
|
||||
use plume_models::{
|
||||
Connection,
|
||||
instance::Instance,
|
||||
users::*,
|
||||
};
|
||||
|
||||
pub fn command<'a, 'b>() -> App<'a, 'b> {
|
||||
SubCommand::with_name("users")
|
||||
.about("Manage users")
|
||||
.subcommand(SubCommand::with_name("new")
|
||||
.arg(Arg::with_name("name")
|
||||
.short("n")
|
||||
.long("name")
|
||||
.alias("username")
|
||||
.takes_value(true)
|
||||
.help("The username of the new user")
|
||||
).arg(Arg::with_name("display-name")
|
||||
.short("N")
|
||||
.long("display-name")
|
||||
.takes_value(true)
|
||||
.help("The display name of the new user")
|
||||
).arg(Arg::with_name("biography")
|
||||
.short("b")
|
||||
.long("bio")
|
||||
.alias("biography")
|
||||
.takes_value(true)
|
||||
.help("The biography of the new user")
|
||||
).arg(Arg::with_name("email")
|
||||
.short("e")
|
||||
.long("email")
|
||||
.takes_value(true)
|
||||
.help("Email address of the new user")
|
||||
).arg(Arg::with_name("password")
|
||||
.short("p")
|
||||
.long("password")
|
||||
.takes_value(true)
|
||||
.help("The password of the new user")
|
||||
).arg(Arg::with_name("admin")
|
||||
.short("a")
|
||||
.long("admin")
|
||||
.help("Makes the user an administrator of the instance")
|
||||
).about("Create a new user on this instance"))
|
||||
.subcommand(SubCommand::with_name("reset-password")
|
||||
.arg(Arg::with_name("name")
|
||||
.short("u")
|
||||
.long("user")
|
||||
.alias("username")
|
||||
.takes_value(true)
|
||||
.help("The username of the user to reset password to")
|
||||
).arg(Arg::with_name("password")
|
||||
.short("p")
|
||||
.long("password")
|
||||
.takes_value(true)
|
||||
.help("The password new for the user")
|
||||
).about("Reset user password"))
|
||||
.subcommand(
|
||||
SubCommand::with_name("new")
|
||||
.arg(
|
||||
Arg::with_name("name")
|
||||
.short("n")
|
||||
.long("name")
|
||||
.alias("username")
|
||||
.takes_value(true)
|
||||
.help("The username of the new user"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("display-name")
|
||||
.short("N")
|
||||
.long("display-name")
|
||||
.takes_value(true)
|
||||
.help("The display name of the new user"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("biography")
|
||||
.short("b")
|
||||
.long("bio")
|
||||
.alias("biography")
|
||||
.takes_value(true)
|
||||
.help("The biography of the new user"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("email")
|
||||
.short("e")
|
||||
.long("email")
|
||||
.takes_value(true)
|
||||
.help("Email address of the new user"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("password")
|
||||
.short("p")
|
||||
.long("password")
|
||||
.takes_value(true)
|
||||
.help("The password of the new user"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("admin")
|
||||
.short("a")
|
||||
.long("admin")
|
||||
.help("Makes the user an administrator of the instance"),
|
||||
)
|
||||
.about("Create a new user on this instance"),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("reset-password")
|
||||
.arg(
|
||||
Arg::with_name("name")
|
||||
.short("u")
|
||||
.long("user")
|
||||
.alias("username")
|
||||
.takes_value(true)
|
||||
.help("The username of the user to reset password to"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("password")
|
||||
.short("p")
|
||||
.long("password")
|
||||
.takes_value(true)
|
||||
.help("The password new for the user"),
|
||||
)
|
||||
.about("Reset user password"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
@@ -69,16 +85,28 @@ pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
}
|
||||
|
||||
fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
let username = args.value_of("name").map(String::from).unwrap_or_else(|| super::ask_for("Username"));
|
||||
let display_name = args.value_of("display-name").map(String::from).unwrap_or_else(|| super::ask_for("Display name"));
|
||||
let username = args
|
||||
.value_of("name")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| super::ask_for("Username"));
|
||||
let display_name = args
|
||||
.value_of("display-name")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| super::ask_for("Display name"));
|
||||
let admin = args.is_present("admin");
|
||||
let bio = args.value_of("biography").unwrap_or("").to_string();
|
||||
let email = args.value_of("email").map(String::from).unwrap_or_else(|| super::ask_for("Email address"));
|
||||
let password = args.value_of("password").map(String::from).unwrap_or_else(|| {
|
||||
print!("Password: ");
|
||||
io::stdout().flush().expect("Couldn't flush STDOUT");
|
||||
rpassword::read_password().expect("Couldn't read your password.")
|
||||
});
|
||||
let email = args
|
||||
.value_of("email")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| super::ask_for("Email address"));
|
||||
let password = args
|
||||
.value_of("password")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| {
|
||||
print!("Password: ");
|
||||
io::stdout().flush().expect("Couldn't flush STDOUT");
|
||||
rpassword::read_password().expect("Couldn't read your password.")
|
||||
});
|
||||
|
||||
NewUser::new_local(
|
||||
conn,
|
||||
@@ -88,17 +116,31 @@ fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
&bio,
|
||||
email,
|
||||
User::hash_pass(&password).expect("Couldn't hash password"),
|
||||
).expect("Couldn't save new user");
|
||||
)
|
||||
.expect("Couldn't save new user");
|
||||
}
|
||||
|
||||
fn reset_password<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
let username = args.value_of("name").map(String::from).unwrap_or_else(|| super::ask_for("Username"));
|
||||
let user = User::find_by_name(conn, &username, Instance::get_local(conn).expect("Failed to get local instance").id)
|
||||
.expect("Failed to get user");
|
||||
let password = args.value_of("password").map(String::from).unwrap_or_else(|| {
|
||||
print!("Password: ");
|
||||
io::stdout().flush().expect("Couldn't flush STDOUT");
|
||||
rpassword::read_password().expect("Couldn't read your password.")
|
||||
});
|
||||
user.reset_password(conn, &password).expect("Failed to reset password");
|
||||
let username = args
|
||||
.value_of("name")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| super::ask_for("Username"));
|
||||
let user = User::find_by_name(
|
||||
conn,
|
||||
&username,
|
||||
Instance::get_local(conn)
|
||||
.expect("Failed to get local instance")
|
||||
.id,
|
||||
)
|
||||
.expect("Failed to get user");
|
||||
let password = args
|
||||
.value_of("password")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| {
|
||||
print!("Password: ");
|
||||
io::stdout().flush().expect("Couldn't flush STDOUT");
|
||||
rpassword::read_password().expect("Couldn't read your password.")
|
||||
});
|
||||
user.reset_password(conn, &password)
|
||||
.expect("Failed to reset password");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user