1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 10:41:03 +02:00

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::collections::HashMap;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{env, fs}; use std::{env, fs};
use tracing::instrument;
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "kebab-case")] #[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 { pub struct Config {
#[serde(default = "default_bar_position")] #[serde(default = "default_bar_position")]
pub position: BarPosition, pub position: BarPosition,
@ -96,13 +97,17 @@ const fn default_bar_height() -> i32 {
impl Config { impl Config {
/// Attempts to load the config file from file, /// Attempts to load the config file from file,
/// parse it and return a new instance of `Self`. /// parse it and return a new instance of `Self`.
#[instrument]
pub fn load() -> Result<Self> { pub fn load() -> Result<Self> {
let config_path = if let Ok(config_path) = env::var("IRONBAR_CONFIG") { let config_path = if let Ok(config_path) = env::var("IRONBAR_CONFIG") {
let path = PathBuf::from(config_path); let path = PathBuf::from(config_path);
if path.exists() { if path.exists() {
Ok(path) Ok(path)
} else { } 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")) .note("Config file was specified using `IRONBAR_CONFIG` environment variable"))
} }
} else { } else {
@ -116,6 +121,7 @@ impl Config {
/// by checking each valid format's extension. /// by checking each valid format's extension.
/// ///
/// Returns the path of the first valid match, if any. /// Returns the path of the first valid match, if any.
#[instrument]
fn try_find_config() -> Result<PathBuf> { fn try_find_config() -> Result<PathBuf> {
let config_dir = config_dir().wrap_err("Failed to locate user config dir")?; let config_dir = config_dir().wrap_err("Failed to locate user config dir")?;
@ -135,7 +141,10 @@ impl Config {
match file { match file {
Some(file) => Ok(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"); const VERSION: &str = env!("CARGO_PKG_VERSION");
#[repr(i32)]
enum ExitCode {
ErrGtkDisplay = 1,
ErrCreateBars = 2,
ErrConfig = 3
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
// Disable backtraces by default // Disable backtraces by default
@ -58,7 +65,7 @@ async fn main() -> Result<()> {
|| { || {
let report = Report::msg("Failed to get default GTK display"); let report = Report::msg("Failed to get default GTK display");
error!("{:?}", report); error!("{:?}", report);
exit(1) exit(ExitCode::ErrGtkDisplay as i32)
}, },
|display| display, |display| display,
); );
@ -67,14 +74,14 @@ async fn main() -> Result<()> {
Ok(config) => config, Ok(config) => config,
Err(err) => { Err(err) => {
error!("{:?}", err); error!("{:?}", err);
Config::default() exit(ExitCode::ErrConfig as i32)
} }
}; };
debug!("Loaded config file"); debug!("Loaded config file");
if let Err(err) = create_bars(app, &display, wayland_client, &config) { if let Err(err) = create_bars(app, &display, wayland_client, &config) {
error!("{:?}", err); error!("{:?}", err);
exit(2); exit(ExitCode::ErrCreateBars as i32);
} }
debug!("Created bars"); debug!("Created bars");
@ -83,7 +90,7 @@ async fn main() -> Result<()> {
|| { || {
let report = Report::msg("Failed to locate user config dir"); let report = Report::msg("Failed to locate user config dir");
error!("{:?}", report); error!("{:?}", report);
exit(3); exit(ExitCode::ErrCreateBars as i32);
}, },
|dir| dir.join("ironbar").join("style.css"), |dir| dir.join("ironbar").join("style.css"),
); );