Skip to content
Closed
Changes from all commits
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
28 changes: 20 additions & 8 deletions src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ impl Config {
fn load_file() -> Option<Config> {
if let Ok(config_path) = env::var("PATCH_HUB_CONFIG_PATH") {
if Path::new(&config_path).is_file() {
let file_contents = fs::read_to_string(&config_path).unwrap_or(String::new());
if let Ok(config) = serde_json::from_str(&file_contents) {
return Some(config);
match fs::read_to_string(&config_path) {
Ok(file_contents) => match serde_json::from_str(&file_contents) {
Ok(config) => return Some(config),
Err(e) => eprintln!("Failed to parse config file {}: {}", config_path, e),
},
Err(e) => {
eprintln!("Failed to read config file {}: {}", config_path, e)
}
}
}
}
Expand All @@ -95,9 +100,14 @@ impl Config {
env::var("HOME").unwrap()
);
if Path::new(&config_path).is_file() {
let file_contents = fs::read_to_string(&config_path).unwrap_or(String::new());
if let Ok(config) = serde_json::from_str(&file_contents) {
return Some(config);
match fs::read_to_string(&config_path) {
Ok(file_contents) => match serde_json::from_str(&file_contents) {
Ok(config) => return Some(config),
Err(e) => eprintln!("Failed to parse config file {}: {}", config_path, e),
},
Err(e) => {
eprintln!("Failed to read config file {}: {}", config_path, e)
}
}
}

Expand Down Expand Up @@ -128,9 +138,11 @@ impl Config {

pub fn build() -> Self {
let mut config = Self::load_file().unwrap_or_else(|| {
eprintln!("No valid config file found, using default configuration");
let config = Self::default();
// TODO: Better handle this error
let _ = config.save_patch_hub_config();
config.save_patch_hub_config().unwrap_or_else(|e| {
eprintln!("Failed to save default config: {}", e);
});
config
});

Expand Down