Skip to content

Commit 2be02b9

Browse files
committed
Refactor config print and save
1 parent 2d23587 commit 2be02b9

File tree

1 file changed

+49
-168
lines changed

1 file changed

+49
-168
lines changed

src/config.rs

Lines changed: 49 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use std::fs::{create_dir_all, read_to_string, File};
2-
use std::path::PathBuf;
1+
use std::{
2+
fmt::Display,
3+
fs::{create_dir_all, read_to_string, File},
4+
path::PathBuf,
5+
};
36

47
use anyhow::{bail, Result};
58
use colored::*;
@@ -65,12 +68,12 @@ impl Config {
6568
serde_json::to_writer(config_file, &self)?;
6669
}
6770
None => {
68-
bail!("Unable to save config file.");
71+
bail!("Error saving config file");
6972
}
7073
}
7174
}
7275
None => {
73-
bail!("Unable to save config file.");
76+
bail!("Error saving config file");
7477
}
7578
}
7679

@@ -80,9 +83,7 @@ impl Config {
8083
pub fn save_creator(value: String) -> Result<()> {
8184
if let Ok(mut config) = Config::load_config() {
8285
config.creator = Some(value);
83-
if config.save().is_err() {
84-
raise_error()?;
85-
};
86+
config.save()?;
8687
} else {
8788
raise_error()?;
8889
}
@@ -93,9 +94,7 @@ impl Config {
9394
pub fn save_creator_email(value: String) -> Result<()> {
9495
if let Ok(mut config) = Config::load_config() {
9596
config.creator_email = Some(value);
96-
if config.save().is_err() {
97-
raise_error()?;
98-
};
97+
config.save()?;
9998
} else {
10099
raise_error()?;
101100
}
@@ -106,9 +105,7 @@ impl Config {
106105
pub fn save_license(value: LicenseType) -> Result<()> {
107106
if let Ok(mut config) = Config::load_config() {
108107
config.license = Some(value);
109-
if config.save().is_err() {
110-
raise_error()?;
111-
};
108+
config.save()?;
112109
} else {
113110
raise_error()?;
114111
}
@@ -119,9 +116,7 @@ impl Config {
119116
pub fn save_python_version(value: String) -> Result<()> {
120117
if let Ok(mut config) = Config::load_config() {
121118
config.python_version = Some(value);
122-
if config.save().is_err() {
123-
raise_error()?;
124-
};
119+
config.save()?;
125120
} else {
126121
raise_error()?;
127122
}
@@ -132,9 +127,7 @@ impl Config {
132127
pub fn save_min_python_version(value: String) -> Result<()> {
133128
if let Ok(mut config) = Config::load_config() {
134129
config.min_python_version = Some(value);
135-
if config.save().is_err() {
136-
raise_error()?;
137-
};
130+
config.save()?;
138131
} else {
139132
raise_error()?;
140133
}
@@ -158,9 +151,7 @@ impl Config {
158151
pub fn save_is_async_project(value: bool) -> Result<()> {
159152
if let Ok(mut config) = Config::load_config() {
160153
config.is_async_project = Some(value);
161-
if config.save().is_err() {
162-
raise_error()?;
163-
};
154+
config.save()?;
164155
} else {
165156
raise_error()?;
166157
}
@@ -171,9 +162,7 @@ impl Config {
171162
pub fn save_is_application(value: bool) -> Result<()> {
172163
if let Ok(mut config) = Config::load_config() {
173164
config.is_application = Some(value);
174-
if config.save().is_err() {
175-
raise_error()?;
176-
};
165+
config.save()?;
177166
} else {
178167
raise_error()?;
179168
}
@@ -196,9 +185,7 @@ impl Config {
196185

197186
if let Ok(mut config) = Config::load_config() {
198187
config.github_actions_python_test_versions = Some(versions);
199-
if config.save().is_err() {
200-
raise_error()?;
201-
};
188+
config.save()?;
202189
} else {
203190
raise_error()?;
204191
}
@@ -209,9 +196,7 @@ impl Config {
209196
pub fn save_max_line_length(value: u8) -> Result<()> {
210197
if let Ok(mut config) = Config::load_config() {
211198
config.max_line_length = Some(value);
212-
if config.save().is_err() {
213-
raise_error()?;
214-
};
199+
config.save()?;
215200
} else {
216201
raise_error()?;
217202
}
@@ -222,9 +207,7 @@ impl Config {
222207
pub fn save_use_dependabot(value: bool) -> Result<()> {
223208
if let Ok(mut config) = Config::load_config() {
224209
config.use_dependabot = Some(value);
225-
if config.save().is_err() {
226-
raise_error()?;
227-
};
210+
config.save()?;
228211
} else {
229212
raise_error()?;
230213
}
@@ -235,9 +218,7 @@ impl Config {
235218
pub fn save_dependabot_schedule(value: DependabotSchedule) -> Result<()> {
236219
if let Ok(mut config) = Config::load_config() {
237220
config.dependabot_schedule = Some(value);
238-
if config.save().is_err() {
239-
raise_error()?;
240-
};
221+
config.save()?;
241222
} else {
242223
raise_error()?;
243224
}
@@ -248,9 +229,7 @@ impl Config {
248229
pub fn save_dependabot_day(value: Day) -> Result<()> {
249230
if let Ok(mut config) = Config::load_config() {
250231
config.dependabot_day = Some(value);
251-
if config.save().is_err() {
252-
raise_error()?;
253-
};
232+
config.save()?;
254233
} else {
255234
raise_error()?;
256235
}
@@ -261,9 +240,7 @@ impl Config {
261240
pub fn save_use_continuous_deployment(value: bool) -> Result<()> {
262241
if let Ok(mut config) = Config::load_config() {
263242
config.use_continuous_deployment = Some(value);
264-
if config.save().is_err() {
265-
raise_error()?;
266-
};
243+
config.save()?;
267244
} else {
268245
raise_error()?;
269246
}
@@ -274,9 +251,7 @@ impl Config {
274251
pub fn save_use_release_drafter(value: bool) -> Result<()> {
275252
if let Ok(mut config) = Config::load_config() {
276253
config.use_release_drafter = Some(value);
277-
if config.save().is_err() {
278-
raise_error()?;
279-
};
254+
config.save()?;
280255
} else {
281256
raise_error()?;
282257
}
@@ -287,9 +262,7 @@ impl Config {
287262
pub fn save_use_multi_os_ci(value: bool) -> Result<()> {
288263
if let Ok(mut config) = Config::load_config() {
289264
config.use_multi_os_ci = Some(value);
290-
if config.save().is_err() {
291-
raise_error()?;
292-
};
265+
config.save()?;
293266
} else {
294267
raise_error()?;
295268
}
@@ -300,9 +273,7 @@ impl Config {
300273
pub fn save_download_latest_packages(value: bool) -> Result<()> {
301274
if let Ok(mut config) = Config::load_config() {
302275
config.download_latest_packages = Some(value);
303-
if config.save().is_err() {
304-
raise_error()?;
305-
};
276+
config.save()?;
306277
} else {
307278
raise_error()?;
308279
}
@@ -312,40 +283,11 @@ impl Config {
312283

313284
pub fn show() {
314285
let config = Config::load_config().unwrap_or_default();
315-
let creator_label = "Creator";
316-
if let Some(creator) = config.creator {
317-
println!("{}: {creator}", creator_label.blue());
318-
} else {
319-
println!("{}: null", creator_label.blue());
320-
}
321-
322-
let creator_email_label = "Creator Email";
323-
if let Some(creator_email) = config.creator_email {
324-
println!("{}: {creator_email}", creator_email_label.blue());
325-
} else {
326-
println!("{}: null", creator_email_label.blue());
327-
}
328-
329-
let license_label = "License";
330-
if let Some(license) = config.license {
331-
println!("{}: {}", license_label.blue(), license);
332-
} else {
333-
println!("{}: null", license_label.blue());
334-
}
335-
336-
let python_version_label = "Python Version";
337-
if let Some(python_version) = config.python_version {
338-
println!("{}: {python_version}", python_version_label.blue());
339-
} else {
340-
println!("{}: null", python_version_label.blue());
341-
}
342-
343-
let min_python_version_label = "Min Python Version";
344-
if let Some(min_python_version) = config.min_python_version {
345-
println!("{}: {min_python_version}", min_python_version_label.blue());
346-
} else {
347-
println!("{}: null", min_python_version_label.blue());
348-
}
286+
print_config_value("Creator", &config.creator);
287+
print_config_value("Creator Email", &config.creator_email);
288+
print_config_value("License", &config.license);
289+
print_config_value("Python Version", &config.python_version);
290+
print_config_value("Min Python Version", &config.min_python_version);
349291

350292
let is_application_label = "Application or Library";
351293
if let Some(is_application) = config.is_application {
@@ -366,88 +308,19 @@ impl Config {
366308
println!("{}: null", gha_python_label.blue());
367309
}
368310

369-
let project_manager_label = "Project Manager";
370-
if let Some(project_manager) = config.project_manager {
371-
println!("{}: {}", project_manager_label.blue(), project_manager);
372-
} else {
373-
println!("{}: null", project_manager_label.blue());
374-
}
375-
376-
let async_label = "Async Project";
377-
if let Some(is_async_project) = config.is_async_project {
378-
println!("{}: {}", async_label.blue(), is_async_project);
379-
} else {
380-
println!("{}: null", async_label.blue());
381-
}
382-
383-
let max_line_length_label = "Max Line Length";
384-
if let Some(max_line_length) = config.max_line_length {
385-
println!("{}: {max_line_length}", max_line_length_label.blue());
386-
} else {
387-
println!("{}: null", max_line_length_label.blue());
388-
}
389-
390-
let use_dependabot_label = "Use Dependabot";
391-
if let Some(use_dependabot) = config.use_dependabot {
392-
println!("{}: {use_dependabot}", use_dependabot_label.blue());
393-
} else {
394-
println!("{}: null", use_dependabot_label.blue());
395-
}
396-
397-
let dependabot_schedule_label = "Dependabot Schedule";
398-
if let Some(dependabot_schedule) = config.dependabot_schedule {
399-
println!(
400-
"{}: {}",
401-
dependabot_schedule_label.blue(),
402-
dependabot_schedule
403-
);
404-
} else {
405-
println!("{}: null", dependabot_schedule_label.blue());
406-
}
407-
408-
let dependabot_day_label = "Dependabot Day";
409-
if let Some(dependabot_day) = config.dependabot_day {
410-
println!("{}: {}", dependabot_day_label.blue(), dependabot_day);
411-
} else {
412-
println!("{}: null", dependabot_day_label.blue());
413-
}
414-
415-
let use_continuous_deployment_label = "Use Continuous Deployment";
416-
if let Some(use_continuous_deployment) = config.use_continuous_deployment {
417-
println!(
418-
"{}: {use_continuous_deployment}",
419-
use_continuous_deployment_label.blue()
420-
);
421-
} else {
422-
println!("{}: null", use_continuous_deployment_label.blue());
423-
}
424-
425-
let use_release_drafter_label = "Use Release Drafter";
426-
if let Some(use_release_drafter) = config.use_release_drafter {
427-
println!(
428-
"{}: {use_release_drafter}",
429-
use_release_drafter_label.blue()
430-
);
431-
} else {
432-
println!("{}: null", use_release_drafter_label.blue());
433-
}
434-
435-
let use_multi_os_ci_label = "Use Multi OS CI";
436-
if let Some(use_multi_os_ci) = config.use_multi_os_ci {
437-
println!("{}: {use_multi_os_ci}", use_multi_os_ci_label.blue());
438-
} else {
439-
println!("{}: null", use_multi_os_ci_label.blue());
440-
}
441-
442-
let download_latest_packages_label = "Download Latest Packages";
443-
if let Some(download_latest_packages) = config.download_latest_packages {
444-
println!(
445-
"{}: {download_latest_packages}",
446-
download_latest_packages_label.blue()
447-
);
448-
} else {
449-
println!("{}: null", download_latest_packages_label.blue());
450-
}
311+
print_config_value("Project Manager", &config.project_manager);
312+
print_config_value("Async Project", &config.is_async_project);
313+
print_config_value("Max Line Length", &config.max_line_length);
314+
print_config_value("Use Dependabot", &config.use_dependabot);
315+
print_config_value("Dependabot Schedule", &config.dependabot_schedule);
316+
print_config_value("Dependabot Day", &config.dependabot_day);
317+
print_config_value(
318+
"Use Continuous Deployment",
319+
&config.use_continuous_deployment,
320+
);
321+
print_config_value("Use Release Drafter", &config.use_release_drafter);
322+
print_config_value("Use Multi OS CI", &config.use_multi_os_ci);
323+
print_config_value("Download Latest Packages", &config.download_latest_packages);
451324
}
452325
}
453326

@@ -475,6 +348,14 @@ fn raise_error() -> Result<()> {
475348
bail!("Error saving config")
476349
}
477350

351+
fn print_config_value<T: Display>(label: &str, value: &Option<T>) {
352+
if let Some(v) = value {
353+
println!("{}: {}", label.blue(), v);
354+
} else {
355+
println!("{}: null", label.blue());
356+
}
357+
}
358+
478359
#[cfg(test)]
479360
mod tests {
480361
use super::*;

0 commit comments

Comments
 (0)