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

fix(config): Make print-config not panic without a config #5001

Merged
merged 23 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 36 additions & 32 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::configs::Palette;
use crate::context::Context;

use crate::serde_utils::{ValueDeserializer, ValueRef};
use crate::utils;
use nu_ansi_term::Color;
Expand All @@ -10,9 +11,9 @@
use std::borrow::Cow;
use std::clone::Clone;
use std::collections::HashMap;
use std::ffi::OsString;
use std::io::ErrorKind;

use std::env;
use toml::Value;

/// Root config of a module.
Expand Down Expand Up @@ -120,36 +121,41 @@
pub config: Option<toml::Table>,
}

pub fn get_config_path() -> Option<String> {
if let Ok(path) = env::var("STARSHIP_CONFIG") {
// Use $STARSHIP_CONFIG as the config path if available
log::debug!("STARSHIP_CONFIG is set: {}", &path);
Some(path)
} else {
// Default to using ~/.config/starship.toml
log::debug!("STARSHIP_CONFIG is not set");
let config_path = utils::home_dir()?.join(".config/starship.toml");
let config_path_str = config_path.to_str()?.to_owned();
log::debug!("Using default config path: {}", config_path_str);
Some(config_path_str)
}
}

impl StarshipConfig {
/// Initialize the Config struct
pub fn initialize() -> Self {
Self::config_from_file()
pub fn initialize(config_file_path: &Option<OsString>) -> Self {
Self::config_from_file(config_file_path)
.map(|config| Self {
config: Some(config),
})
.unwrap_or_default()
}

/// Create a config from a starship configuration file
fn config_from_file() -> Option<toml::Table> {
let file_path = get_config_path()?;
fn config_from_file(config_file_path: &Option<OsString>) -> Option<toml::Table> {
let toml_content = Self::read_config_content_as_str(config_file_path)?;

match toml::from_str(&toml_content) {
Ok(parsed) => {
log::debug!("Config parsed: {:?}", &parsed);
Some(parsed)
}
Err(error) => {
log::error!("Unable to parse the config file: {}", error);
None

Check warning on line 145 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L143-L145

Added lines #L143 - L145 were not covered by tests
}
}
}

let toml_content = match utils::read_file(file_path) {
pub fn read_config_content_as_str(config_file_path: &Option<OsString>) -> Option<String> {
if config_file_path.is_none() {
log::debug!(
"Unable to determine `config_file_path`. Perhaps `utils::home_dir` is not defined on your platform?"

Check warning on line 153 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L153

Added line #L153 was not covered by tests
);
return None;
}
let config_file_path = config_file_path.as_ref().unwrap();
match utils::read_file(config_file_path) {
Ok(content) => {
log::trace!("Config file content: \"\n{}\"", &content);
Some(content)
Expand All @@ -164,17 +170,6 @@
log::log!(level, "Unable to read config file content: {}", &e);
None
}
}?;

match toml::from_str(&toml_content) {
Ok(parsed) => {
log::debug!("Config parsed: {:?}", &parsed);
Some(parsed)
}
Err(error) => {
log::error!("Unable to parse the config file: {}", error);
None
}
}
}

Expand Down Expand Up @@ -921,4 +916,13 @@
// Test default behavior
assert!(get_palette(&palettes, None).is_none());
}

#[test]
fn read_config_no_config_file_path_provided() {
assert_eq!(
None,
StarshipConfig::read_config_content_as_str(&None),
"if the platform doesn't have utils::home_dir(), it should return None"

Check warning on line 925 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L925

Added line #L925 was not covered by tests
);
}
}