Add home subcommand

This commit is contained in:
Daniele Tricoli 2019-05-17 01:20:25 +02:00
parent 1d84088aa6
commit 0d27b67ec2
3 changed files with 54 additions and 31 deletions

View file

@ -1,10 +1,10 @@
use std::fmt;
use std::path::PathBuf;
use home::home_dir;
use log::warn;
const QUOLL_HOME: &str = ".quoll";
use crate::config::get_home;
const ICON_EXTENSIONS: [&str; 2] = ["svg", "png"];
/// A command can tell us to quit the application (Command::Quit) or to change
@ -23,8 +23,7 @@ impl Command {
match self {
Command::Quit => None,
Command::Custom(ref c) => {
let mut home = home_dir()?;
home.push(QUOLL_HOME);
let mut home = get_home()?;
// Prevent directory traversal attack.
home.push(PathBuf::from(c).file_name()?.to_str()?);
for ext in &ICON_EXTENSIONS {
@ -73,8 +72,7 @@ fn commands_creation() {
fn commands_to_path() {
assert_eq!(Command::from("quit").to_path(), None);
let mut home = home_dir().unwrap();
home.push(QUOLL_HOME);
let mut home = get_home().unwrap();
home.push("eriol.png");
assert_eq!(Command::from("eriol").to_path(), None);
}

13
src/config.rs Normal file
View file

@ -0,0 +1,13 @@
use std::path::PathBuf;
use home::home_dir;
const QUOLL_HOME: &str = ".quoll";
/// Return the path where quoll looks at resources.
pub fn get_home() -> Option<PathBuf> {
let mut home = home_dir()?;
home.push(QUOLL_HOME);
Some(home)
}

View file

@ -1,4 +1,5 @@
mod commands;
mod config;
mod gui;
mod network;
@ -35,8 +36,8 @@ fn main() {
.value_name("PORT"),
)
.subcommand(
SubCommand::with_name("serve")
.about("Starts UDP server for incoming commands"),
SubCommand::with_name("home")
.about("Displays the location of the quoll home folder"),
)
.subcommand(
SubCommand::with_name("send")
@ -48,37 +49,48 @@ fn main() {
.index(1),
),
)
.subcommand(
SubCommand::with_name("serve")
.about("Starts UDP server for incoming commands"),
)
.get_matches();
let port = matches.value_of("port").unwrap_or(DEFAULT_PORT);
let address = format!("{}:{}", DEFAULT_HOST, port);
if matches.subcommand_matches("serve").is_some() {
let command = Arc::new(RwLock::new(Command::from("black")));
match matches.subcommand() {
("home", _) => {
if let Some(home) = config::get_home() {
println!("{}", home.display());
}
}
("send", Some(matches)) => {
// It's safe to unwrap COMMAND because it is required.
let command = matches.value_of("COMMAND").unwrap();
info!("Starting UDP server on port {}...", port);
let mut server = Server::new(address.clone(), Arc::clone(&command));
thread::spawn(move || {
if let Err(e) = server.serve_forever() {
info!("Sending command: {}", command);
if let Err(e) = send_to(command, address) {
error!("{}", e);
process::exit(1);
};
});
info!("Starting GUI...");
gui::start(command, port);
info!("Exiting...");
}
if let Some(matches) = matches.subcommand_matches("send") {
// It's safe to unwrap because COMMAND is required.
let command = matches.value_of("COMMAND").unwrap();
info!("Sending command: {}", command);
if let Err(e) = send_to(command, address) {
error!("{}", e);
process::exit(1);
}
}
("serve", _) => {
let command = Arc::new(RwLock::new(Command::from("black")));
info!("Starting UDP server on port {}...", port);
let mut server = Server::new(address, Arc::clone(&command));
thread::spawn(move || {
if let Err(e) = server.serve_forever() {
error!("{}", e);
process::exit(1);
};
});
info!("Starting GUI...");
gui::start(command, port);
info!("Exiting...");
}
_ => unreachable!(),
}
}