Skip to content

Commit

Permalink
new impl
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasmohrin committed Feb 6, 2021
1 parent e448b1b commit 11394c7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
49 changes: 30 additions & 19 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ fn map_io_err_to_config_err(e: IoError) -> TealdeerError {
}

impl Config {
pub fn load(enable_styles: bool) -> Result<Self, TealdeerError> {
pub fn load(custom_path: Option<&str>, enable_styles: bool) -> Result<Self, TealdeerError> {
debug!("Loading config");

// Determine path
let (config_file_path, _) = get_config_path()
let (config_file_path, _) = get_config_path(custom_path)
.map_err(|e| ConfigError(format!("Could not determine config path: {}", e)))?;

// Load raw config
Expand Down Expand Up @@ -297,33 +297,44 @@ pub fn get_config_dir() -> Result<(PathBuf, PathSource), TealdeerError> {
///
/// Note that this function does not verify whether the file at that location
/// exists, or is a file.
pub fn get_config_path() -> Result<(PathBuf, PathSource), TealdeerError> {
pub fn get_config_path(custom_path: Option<&str>) -> Result<(PathBuf, PathSource), TealdeerError> {
custom_path.map_or_else(get_default_config_path, |path| {
Ok((path.into(), PathSource::Override))
})
}

/// Returns the path to the default config file location.
pub fn get_default_config_path() -> Result<(PathBuf, PathSource), TealdeerError> {
let (config_dir, source) = get_config_dir()?;
let config_file_path = config_dir.join(CONFIG_FILE_NAME);
Ok((config_file_path, source))
}

/// Create default config file.
pub fn make_default_config() -> Result<PathBuf, TealdeerError> {
let (config_dir, _) = get_config_dir()?;

// Ensure that config directory exists
if !config_dir.exists() {
if let Err(e) = fs::create_dir_all(&config_dir) {
/// Create default config file. Pass `None` to create at default location.
pub fn make_default_config(path: Option<&str>) -> Result<PathBuf, TealdeerError> {
let config_file_path = if let Some(s) = path {
PathBuf::from(s)
} else {
let (config_dir, _) = get_config_dir()?;

// Ensure that config directory exists
if !config_dir.exists() {
if let Err(e) = fs::create_dir_all(&config_dir) {
return Err(ConfigError(format!(
"Could not create config directory: {}",
e
)));
}
} else if !config_dir.is_dir() {
return Err(ConfigError(format!(
"Could not create config directory: {}",
e
"Config directory could not be created: {} already exists but is not a directory",
config_dir.to_string_lossy(),
)));
}
} else if !config_dir.is_dir() {
return Err(ConfigError(format!(
"Config directory could not be created: {} already exists but is not a directory",
config_dir.to_string_lossy(),
)));
}

config_dir.join(CONFIG_FILE_NAME)
};
// Ensure that a config file doesn't get overwritten
let config_file_path = config_dir.join(CONFIG_FILE_NAME);
if config_file_path.is_file() {
return Err(ConfigError(format!(
"A configuration file already exists at {}, no action was taken.",
Expand Down
17 changes: 9 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct Args {
flag_pager: bool,
flag_quiet: bool,
flag_show_paths: bool,
flag_config: Option<String>,
flag_config_path: bool,
flag_seed_config: bool,
flag_markdown: bool,
Expand Down Expand Up @@ -181,7 +182,7 @@ fn update_cache(cache: &Cache, quietly: bool) {

/// Show the config path (DEPRECATED)
fn show_config_path() {
match get_config_path() {
match get_config_path(None) {
Ok((config_file_path, _)) => {
println!("Config path is: {}", config_file_path.to_str().unwrap());
}
Expand All @@ -197,7 +198,7 @@ fn show_config_path() {
}

/// Show file paths
fn show_paths() {
fn show_paths(custom_config_path: Option<&str>) {
let config_dir = get_config_dir().map_or_else(
|e| format!("[Error: {}]", e),
|(mut path, source)| {
Expand All @@ -208,7 +209,7 @@ fn show_paths() {
}
},
);
let config_path = get_config_path().map_or_else(
let config_path = get_config_path(custom_config_path).map_or_else(
|e| format!("[Error: {}]", e),
|(path, _)| path.to_str().unwrap_or("[Invalid]").to_string(),
);
Expand Down Expand Up @@ -236,8 +237,8 @@ fn show_paths() {
}

/// Create seed config file and exit
fn create_config_and_exit() {
match make_default_config() {
fn create_config_and_exit(custom_path: Option<&str>) {
match make_default_config(custom_path) {
Ok(config_file_path) => {
println!(
"Successfully created seed config file here: {}",
Expand Down Expand Up @@ -358,12 +359,12 @@ fn main() {
show_config_path();
}
if args.flag_show_paths {
show_paths();
show_paths(args.flag_config.as_deref());
}

// Create a basic config and exit
if args.flag_seed_config {
create_config_and_exit();
create_config_and_exit(args.flag_config.as_deref());
}

// Determine the usage of styles
Expand All @@ -387,7 +388,7 @@ fn main() {
};

// Look up config file, if none is found fall back to default config.
let config = match Config::load(enable_styles) {
let config = match Config::load(args.flag_config.as_deref(), enable_styles) {
Ok(config) => config,
Err(ConfigError(msg)) => {
eprintln!("Could not load config: {}", msg);
Expand Down
3 changes: 3 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub enum PathSource {
EnvVar,
/// Config file variable
ConfigVar,
/// CLI argument override
Override,
}

impl fmt::Display for PathSource {
Expand All @@ -121,6 +123,7 @@ impl fmt::Display for PathSource {
Self::OsConvention => "OS convention",
Self::EnvVar => "env variable",
Self::ConfigVar => "config file variable",
Self::Override => "override by flag",
}
)
}
Expand Down
1 change: 1 addition & 0 deletions src/usage.docopt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Options:
-m --markdown Display the raw markdown instead of rendering it
-q --quiet Suppress informational messages
--show-paths Show file and directory paths used by tealdeer
--config <file> Override config file location
--config-path Show config file path (deprecated)
--seed-config Create a basic config
--color <when> Control when to use color [always, auto, never] [default: auto]
Expand Down

0 comments on commit 11394c7

Please sign in to comment.