Skip to content

Commit

Permalink
fix: attempt to always run from base directory of project
Browse files Browse the repository at this point in the history
This should make sure that it isn't possible to try to perform file
replacement when located in the wrong directory.

Also contains a small tweak to configuration loading. It will now
attempt to look in the PRJ_CONFIG_HOME directory for a configuration
file first, only falling back to the current directory (which is now the
project root) if it fails to find one.
  • Loading branch information
justinrubek committed May 13, 2024
1 parent e3786d2 commit a64418f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
16 changes: 2 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,8 @@ pub struct App {
}

impl App {
pub fn new(args: BaseArgs) -> Result<App> {
let config = match &args.config_file {
Some(path) => Config::from_ron(&path)?,
None => {
let base = project_base_directory::get_project_root()
.map_err(|_| Error::ProjectBaseDirectory)?;
match base {
Some(base) => Config::from_ron(&base.join("bomp.ron"))?,
None => Config::from_ron(&String::from("bomp.ron"))?,
}
}
};

Ok(App { args, config })
pub fn new(args: BaseArgs, config: Config) -> App {
App { args, config }
}
}

Expand Down
39 changes: 37 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bomper::config::Config;
use clap::Parser;
use std::path::PathBuf;

mod app;
use app::App;
Expand All @@ -13,8 +15,41 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init();

let args = Args::parse();
tracing::debug!("{:?}", args);
let app = App::new(args.base_args).map_err(|_| "Failed to load configuration")?;
tracing::debug!(?args);

let project = project_base_directory::Project::discover()
.map_err(|_| bomper::error::Error::ProjectBaseDirectory)?;
tracing::debug!(?project);
if let Some(base_directory) = project.root_directory {
std::env::set_current_dir(base_directory)?;
}

let config_path = match &args.base_args.config_file {
Some(path) => path.to_owned(),
None => {
let config_path = match project.config_home {
Some(dir) => {
let config_path = dir.join("bomp.ron");
if config_path.exists() {
config_path
} else {
PathBuf::from("bomp.ron")
}
}
None => PathBuf::from("bomp.ron"),
};
if !config_path.exists() {
return Err("No configuration file found".into());
}
config_path.to_owned()
}
};
let config_path = config_path.canonicalize()?;
tracing::debug!(?config_path);
let config = Config::from_ron(&config_path)?;
tracing::debug!(?config);

let app = App::new(args.base_args, config);
match args.command {
Commands::RawBump(raw_bump) => {
app.raw_bump(&raw_bump)?;
Expand Down

0 comments on commit a64418f

Please sign in to comment.