fix: weird behaviour when config does not exist

This commit is contained in:
Jake Stanger 2022-10-16 12:58:11 +01:00
parent b66bd788b2
commit 3c43c20c6a
No known key found for this signature in database
GPG Key ID: C51FC8F9CB0BEA61
2 changed files with 24 additions and 8 deletions

View File

@ -15,6 +15,7 @@ use serde::Deserialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::{env, fs};
use tracing::instrument;
#[derive(Debug, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "kebab-case")]
@ -69,7 +70,7 @@ impl BarPosition {
}
}
#[derive(Debug, Deserialize, Clone, Default)]
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
#[serde(default = "default_bar_position")]
pub position: BarPosition,
@ -96,13 +97,17 @@ const fn default_bar_height() -> i32 {
impl Config {
/// Attempts to load the config file from file,
/// parse it and return a new instance of `Self`.
#[instrument]
pub fn load() -> Result<Self> {
let config_path = if let Ok(config_path) = env::var("IRONBAR_CONFIG") {
let path = PathBuf::from(config_path);
if path.exists() {
Ok(path)
} else {
Err(Report::msg("Specified config file does not exist")
Err(Report::msg(format!(
"Specified config file does not exist: {}",
path.display()
))
.note("Config file was specified using `IRONBAR_CONFIG` environment variable"))
}
} else {
@ -116,6 +121,7 @@ impl Config {
/// by checking each valid format's extension.
///
/// Returns the path of the first valid match, if any.
#[instrument]
fn try_find_config() -> Result<PathBuf> {
let config_dir = config_dir().wrap_err("Failed to locate user config dir")?;
@ -135,7 +141,10 @@ impl Config {
match file {
Some(file) => Ok(file),
None => Err(Report::msg("Could not find config file")),
None => Err(Report::msg("Could not find config file")
.suggestion("Ironbar does not include a configuration out of the box")
.suggestion("A guide on writing a config can be found on the wiki:")
.suggestion("https://github.com/JakeStanger/ironbar/wiki/configuration-guide")),
}
}

View File

@ -31,6 +31,13 @@ use wayland::WaylandClient;
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[repr(i32)]
enum ExitCode {
ErrGtkDisplay = 1,
ErrCreateBars = 2,
ErrConfig = 3
}
#[tokio::main]
async fn main() -> Result<()> {
// Disable backtraces by default
@ -58,7 +65,7 @@ async fn main() -> Result<()> {
|| {
let report = Report::msg("Failed to get default GTK display");
error!("{:?}", report);
exit(1)
exit(ExitCode::ErrGtkDisplay as i32)
},
|display| display,
);
@ -67,14 +74,14 @@ async fn main() -> Result<()> {
Ok(config) => config,
Err(err) => {
error!("{:?}", err);
Config::default()
exit(ExitCode::ErrConfig as i32)
}
};
debug!("Loaded config file");
if let Err(err) = create_bars(app, &display, wayland_client, &config) {
error!("{:?}", err);
exit(2);
exit(ExitCode::ErrCreateBars as i32);
}
debug!("Created bars");
@ -83,7 +90,7 @@ async fn main() -> Result<()> {
|| {
let report = Report::msg("Failed to locate user config dir");
error!("{:?}", report);
exit(3);
exit(ExitCode::ErrCreateBars as i32);
},
|dir| dir.join("ironbar").join("style.css"),
);