Skip to content

Commit 9f9f974

Browse files
committed
refactor to be able to write not only default
1 parent 2c7d4f2 commit 9f9f974

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/config.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,23 @@ impl ConfigOptions {
3434
}
3535
}
3636

37-
pub fn write_default<P>(path: P) -> Result<()>
37+
pub fn write<P>(self, path: P) -> Result<()>
3838
// I believe user would like to generate config with CLI flags
3939
// I mean to write disabled_fields with --disabled-fields flag
4040
where
41-
// Nah this feels really bad but rust-analyser told me to add ' + std::fmt::Display'
4241
P: AsRef<Path>,
4342
{
4443
// I dont think this can panic so i simply unwrapped it
45-
let defaults = toml::to_string(&Self::default()).unwrap();
44+
let contents = toml::to_string(&self).unwrap();
4645
match fs::create_dir_all(&path.as_ref().parent().unwrap_or(Path::new("/"))) {
47-
Ok(_) => match fs::write(&path, &defaults) {
46+
Ok(_) => match fs::write(&path, &contents) {
4847
Ok(_) => {
4948
let path = path.as_ref().display();
50-
println!("Default config file created at: {path}")
49+
println!("Config config file created at: {path}")
5150
}
5251
Err(e) => {
5352
let path = path.as_ref().display();
54-
eprintln!("Failed to write default config file at {path}: {e}")
53+
eprintln!("Failed to write config file at {path}: {e}")
5554
}
5655
},
5756
Err(e) => {

src/main.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use onefetch::config::ConfigOptions;
88
use onefetch::info::build_info;
99
use onefetch::ui::printer::Printer;
1010
use std::io;
11-
use std::process::exit;
1211

1312
fn main() -> Result<()> {
1413
setup_panic!();
@@ -33,12 +32,22 @@ fn main() -> Result<()> {
3332
}
3433

3534
if cli_options.config.generate_config {
36-
// what the actual FUCK is happening here?
37-
// why default path is EMPTY?
38-
return ConfigOptions::write_default(&cli_options.config.config_path.unwrap_or_default());
35+
return ConfigOptions::default().write(
36+
&cli_options.config.config_path.unwrap_or(
37+
dirs::config_dir()
38+
.expect("Could not find config dir!")
39+
.join("onefetch/config.toml"),
40+
),
41+
);
3942
}
4043

41-
let config_options = ConfigOptions::read(&cli_options.config.config_path.clone().unwrap_or_default());
44+
let config_options = ConfigOptions::read(
45+
&cli_options.config.config_path.as_ref().unwrap_or(
46+
&dirs::config_dir()
47+
.expect("Could not find config dir!")
48+
.join("onefetch/config.toml"),
49+
),
50+
);
4251

4352
let info = build_info(&cli_options, &config_options)?;
4453

0 commit comments

Comments
 (0)