Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ~ and $HOME components in path option #278

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{env, fs, io};

/// This struct is the actual config type consumed through the codebase. It is boostrapped via its
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Config {
fn from_entry_config(entry_config: &EntryConfig) -> io::Result<Self> {
uncenter marked this conversation as resolved.
Show resolved Hide resolved
Ok(Config {
path: match &entry_config.path {
Some(path) => path.clone(),
Some(path) => normalize_path(path),
None => env::current_dir()?.canonicalize()?,
},
display_mode: match &entry_config.display_mode {
Expand All @@ -79,6 +79,17 @@ impl Config {
}
}

fn normalize_path(path: &Path) -> PathBuf {
let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
match path
.strip_prefix("~")
.or_else(|_| path.strip_prefix("$HOME"))
{
Ok(stripped) => user_dirs::home_dir().unwrap().join(stripped),
uncenter marked this conversation as resolved.
Show resolved Hide resolved
Err(_) => path,
}
}

/// This struct is a reflection of [`Config`] with its fields wrapped with [`Option`], which
/// ensures that we can deserialize from partial config file contents and populate empty fields
/// with defaults. Moreover, enum fields cannot set defaults values currently, so we need to
Expand Down
Loading