Add --create-config flag to cli

This commit is contained in:
Rekai Musuka
2020-06-29 18:11:26 -05:00
parent f79fde6d16
commit 40c682515f
4 changed files with 120 additions and 110 deletions

View File

@@ -1,7 +1,7 @@
use anyhow::Result;
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;
@@ -18,8 +18,6 @@ pub struct Config {
impl Config {
pub fn new(work: u64, short_break: u64, long_break: u64) -> Self {
Self::init_directories();
Config {
work_time: Duration::from_secs(work),
short_break: Duration::from_secs(short_break),
@@ -28,26 +26,8 @@ impl Config {
}
}
fn init_directories() {
let config_dir = Self::get_config_directory();
let data_dir = Self::get_data_directory();
if !config_dir.exists() {
let _ = fs::create_dir_all(config_dir);
}
if !data_dir.exists() {
let _ = fs::create_dir_all(data_dir);
}
}
pub fn save(config: &Config) -> Result<()> {
let config_directory = Self::get_config_directory();
if !config_directory.exists() {
fs::create_dir_all(&config_directory)?;
}
Self::write_to_file(&config_directory.join(SETTINGS_FILE), config)
}
@@ -74,14 +54,14 @@ impl Config {
pub fn get_data_directory() -> PathBuf {
ProjectDirs::from("moe", "paoda", "domasi")
.unwrap()
.config_dir()
.data_dir()
.to_path_buf()
}
fn get_config_directory() -> PathBuf {
ProjectDirs::from("moe", "paoda", "domasi")
.unwrap()
.data_dir()
.config_dir()
.to_path_buf()
}
}

View File

@@ -19,6 +19,12 @@ fn main() {
.version("0.1.0")
.author("paoda <musukarekai@gmail.com>")
.about("Yet another pomodoro timer.")
.arg(
Arg::with_name("create-config")
.short("C")
.long("create-config")
.help("Creates a Settings.toml and an alert directory"),
)
.subcommand(
SubCommand::with_name("start")
.about("Start the Pomodoro Timer")
@@ -38,11 +44,48 @@ fn main() {
// _ => {}
// }
if matches.is_present("create-config") {
create_config()
}
if let ("start", Some(sub_matches)) = matches.subcommand() {
start(sub_matches);
}
}
pub fn create_config() {
let config_dir = Config::get_data_directory();
let data_dir = Config::get_data_directory().join("alert");
if !config_dir.exists() {
fs::create_dir_all(&config_dir).unwrap_or_else(|err| {
panic!("Failed to create {}: {}", config_dir.to_string_lossy(), err)
})
}
if !data_dir.exists() {
fs::create_dir_all(&data_dir).unwrap_or_else(|err| {
panic!("Failed to create {}: {}", data_dir.to_string_lossy(), err)
});
}
let config: Config = Default::default();
Config::save(&config).unwrap_or_else(|err| {
let cfg_path = config_dir.to_string_lossy();
panic!("Error while writing settings.toml to {}: {}", cfg_path, err);
});
let data_path = data_dir.to_string_lossy();
let settings_path = config_dir.join("settings.toml");
let settings_path = settings_path.to_string_lossy();
println!(
"Successfully created \"{}\" and \"{}\"",
settings_path, data_path
);
}
pub fn start(args: &ArgMatches) {
let mut pomodoro = Pomodoro::new();
@@ -87,7 +130,7 @@ pub fn start(args: &ArgMatches) {
None => {
// Look in the default locations
// check for .mp3, .wav, .ogg, .flac
let data_dir = Config::get_data_directory();
let data_dir = Config::get_data_directory().join("alert");
let data_dir_str = data_dir.to_string_lossy();
let items = fs::read_dir(&data_dir).unwrap_or_else(|_err| {
@@ -119,24 +162,11 @@ fn get_audio_file(items: std::fs::ReadDir) -> Option<PathBuf> {
match maybe_entry {
Ok(entry) => match entry.path().extension() {
Some(ext) => match ext.to_str() {
Some("mp3") => {
Some("mp3") | Some("wav") | Some("ogg") | Some("flac") => {
result = Some(entry.path());
break;
}
Some("wav") => {
result = Some(entry.path());
break;
}
Some("ogg") => {
result = Some(entry.path());
break;
}
Some("flac") => {
result = Some(entry.path());
break;
}
Some(_ext) => continue,
None => continue,
_ => continue,
},
None => continue,
},