Skip to content

Commit

Permalink
fix: create config dir if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Apr 26, 2024
1 parent 31a6555 commit fdfb3d1
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ impl Args {
fn main() -> anyhow::Result<()> {
let args = Args::parse_from_env().context("parse args")?;

let config = match args.config.map(PathBuf::from).or_else(get_config_path) {
Some(path) => {
if path.exists() {
load_config(path)?
let config = match args.config.map(PathBuf::from).or_else(get_config_dir) {
Some(config_dir) => {
let config_path = config_dir.join("config.toml");
if config_path.exists() {
load_config(config_path)?
} else {
save_default_config(path)?;
if !config_dir.exists() {
fs::create_dir_all(config_dir).context("create config dir")?;
}
save_default_config(config_path)?;
Config::default()
}
}
Expand All @@ -65,11 +69,11 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

fn get_config_path() -> Option<PathBuf> {
fn get_config_dir() -> Option<PathBuf> {
env::var("XDG_CONFIG_HOME")
.map(|config_dir| Path::new(&config_dir).to_path_buf())
.or_else(|_| env::var("HOME").map(|home_dir| Path::new(&home_dir).join(".config")))
.map(|config_dir| config_dir.join("geektray").join("config.toml"))
.map(|config_dir| config_dir.join("geektray"))
.ok()
}

Expand Down

0 comments on commit fdfb3d1

Please sign in to comment.