on master help sign in
Graceful handling of error conditions
commit40a827ebcb8de132e346c0104b419dc2b8263782 authorJosh Mickley <josh.mickley@tuta.io> dateWed 25 Mar 2020, 22:03 (7 years ago) parent376c556
2 files changed+21−20
src/bin/bat/config.rs+20−15 src/bin/bat/main.rs+1−5
src/bin/bat/config.rs+20−15
@@ -16,28 +16,31 @@ pub fn config_file() -> PathBuf {
1616 .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
1717}
1818
19−pub fn generate_config_file() {
19+pub fn generate_config_file() -> bat::errors::Result<()> {
2020 let config_file = config_file();
2121 if config_file.exists() {
2222 println!("A config file already exists at: {}", config_file.to_string_lossy());
2323
24− print!("Overwrite? (y/n): ");
25− let _ = io::stdout().flush();
24+ print!("Overwrite? (y/N): ");
25+ io::stdout().flush()?;
2626 let mut decision = String::new();
27− io::stdin().read_line(&mut decision).expect("Failed to read input");
27+ io::stdin().read_line(&mut decision)?;
2828
2929 if !decision.trim().eq_ignore_ascii_case("Y") {
30− return;
30+ return Ok(());
3131 }
3232 } else {
33− let config_dir = config_file.parent().unwrap();
34− if !config_dir.exists() {
35− fs::create_dir(config_dir).expect("Unable to create config directory");
33+ let config_dir = config_file.parent();
34+ match config_dir {
35+ Some(path) => fs::create_dir_all(path)?,
36+ None => return Ok(Err(format!("Unable to write config file to: {}", config_file.to_string_lossy()))?),
3637 }
3738 }
3839
39− let default_config = "# Specify desired theme (e.g. \"TwoDark\")
40−#--theme=\"TwoDark\"
40+ let default_config = r#"# bat config
41+
42+# Specify desired theme (e.g. "TwoDark"). Issue `bat --list-themes` for a list of all available themes
43+#--theme="TwoDark"
4144
4245# Enable this to use italic text on the terminal (not supported on all terminals):
4346#--italic-text=always
@@ -46,14 +49,16 @@ pub fn generate_config_file() {
4649#--paging=never
4750
4851# Use C++ syntax for .ino files
49−#--map-syntax \"*.ino:C++\"
52+#--map-syntax "*.ino:C++"
5053
51−# Use \".gitignore\"-style highlighting for \".ignore\" files
52−#--map-syntax \".ignore:Git Ignore\"
53−";
54+# Use ".gitignore"-style highlighting for ".ignore" files
55+#--map-syntax ".ignore:Git Ignore"
56+"#;
5457
55− fs::write(&config_file, default_config).expect("Error writing config file!");
58+ fs::write(&config_file, default_config)?;
5659 println!("Success! Config file written to {}", config_file.to_string_lossy());
60+
61+ return Ok(());
5762}
5863
5964pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
src/bin/bat/main.rs+1−5
@@ -179,19 +179,15 @@ fn run() -> Result<bool> {
179179
180180 if app.matches.is_present("list-languages") {
181181 list_languages(&config)?;
182−
183182 Ok(true)
184183 } else if app.matches.is_present("list-themes") {
185184 list_themes(&config)?;
186−
187185 Ok(true)
188186 } else if app.matches.is_present("config-file") {
189187 println!("{}", config_file().to_string_lossy());
190−
191188 Ok(true)
192189 } else if app.matches.is_present("generate-config-file") {
193− generate_config_file();
194−
190+ generate_config_file()?;
195191 Ok(true)
196192 } else if app.matches.is_present("config-dir") {
197193 writeln!(io::stdout(), "{}", config_dir())?;