|
1 |
| -use anyhow::Result; |
| 1 | +use anyhow::{Context, Result}; |
2 | 2 | use serde::{Deserialize, Serialize};
|
3 | 3 | use std::{fs, path::Path};
|
4 | 4 |
|
@@ -81,23 +81,19 @@ impl ConfigOptions {
|
81 | 81 | {
|
82 | 82 | // I dont think this can panic so i simply unwrapped it
|
83 | 83 | let contents = toml::to_string(&self).unwrap();
|
84 |
| - match fs::create_dir_all(&path.as_ref().parent().unwrap_or(Path::new("/"))) { |
85 |
| - Ok(_) => match fs::write(&path, &contents) { |
86 |
| - Ok(_) => { |
87 |
| - let path = path.as_ref().display(); |
88 |
| - println!("Config config file created at: {path}") |
89 |
| - } |
90 |
| - Err(e) => { |
91 |
| - let path = path.as_ref().display(); |
92 |
| - eprintln!("Failed to write config file at {path}: {e}") |
93 |
| - } |
94 |
| - }, |
95 |
| - Err(e) => { |
96 |
| - let path = path.as_ref().display(); |
97 |
| - eprintln!("Failed to create config directory {path}: {e}"); |
98 |
| - } |
| 84 | + let dir = path |
| 85 | + .as_ref() |
| 86 | + .parent() |
| 87 | + .with_context(|| format!("'{}' is not a path!", path.as_ref().display())); |
| 88 | + fs::create_dir_all(dir?)?; |
| 89 | + match fs::write(&path, contents) { |
| 90 | + Ok(_) => println!("Created default config at {}", path.as_ref().display()), |
| 91 | + Err(e) => eprintln!( |
| 92 | + "Failed to write default config at {}: {}", |
| 93 | + path.as_ref().display(), |
| 94 | + e |
| 95 | + ), |
99 | 96 | }
|
100 |
| - // Im not sure it should return simple Ok(())? |
101 | 97 | Ok(())
|
102 | 98 | }
|
103 | 99 | }
|
0 commit comments