Skip to content

Commit

Permalink
feat(init): ✨ allowed creating new config at custom path
Browse files Browse the repository at this point in the history
Path can be set with BONNIE_CONF as per help page.
  • Loading branch information
arctic-hen7 committed Sep 4, 2021
1 parent dd4e273 commit a81345b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ fn core() -> Result<i32, String> {
// This will panic if the first argument is not found (which is probably someone trying to fuzz us)
// TODO add a checker for the executable that offers to install Bonnie if it isn't already?
let _executable_name = prog_args.remove(0);
// Get the file we'll be using
let cfg_path = env::var("BONNIE_CONF").unwrap_or_else(|_| "./bonnie.toml".to_string());
// Check for special arguments
let mut should_cache = false;
if matches!(prog_args.get(0), Some(_)) {
Expand All @@ -51,9 +53,10 @@ fn core() -> Result<i32, String> {
}
_ => None,
},
&cfg_path
)?;

println!("A new Bonnie configuration file has been initialized at ./bonnie.toml!");
println!("A new Bonnie configuration file has been initialized at {}!", &cfg_path);

return Ok(0);
} else if prog_args[0] == "-h" || prog_args[0] == "--help" {
Expand Down
3 changes: 1 addition & 2 deletions src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ This just summarizes the functionality of this command, not the syntax of Bonnie
-h, --help prints this help page
-v, --version prints the current version of Bonnie
-i, --init [-t, --template <template-file>] creates a new `bonnie.toml` configuration, using the specified template file if provided
-e, --edit-template opens the default template in your default cli editor
-i, --init [-t, --template <template-file>] creates a new `bonnie.toml` configuration (or whatever's set in `BONNIE_CONF`), using the specified template file if provided
-c, --cache caches the Bonnie configuration file to `.bonnie.cache.json` for performance (this cache must be MANUALLY updated by re-running this command!)
The expected location of a Bonnie configuration file can be changed from the default `./bonnie.toml` by setting the `BONNIE_CONF` environment variable.
Expand Down
11 changes: 6 additions & 5 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::template::get_default_template;
use std::fs;

// Creates a new Bonnie configuration file using a template, or from the default
pub fn init(template: Option<String>) -> Result<(), String> {
// This takes the path to write to (set through 'BONNIE_CONF' or default `./bonnie.toml`)
pub fn init(template: Option<String>, cfg_path: &str) -> Result<(), String> {
// Check if there's already a config file in this directory
if fs::metadata("./bonnie.toml").is_ok() {
if fs::metadata(cfg_path).is_ok() {
Err(String::from("A Bonnie configuration file already exists in this directory. If you want to create a new one, please delete the old one first."))
} else {
// Check if a template has been given
Expand All @@ -18,20 +19,20 @@ pub fn init(template: Option<String>) -> Result<(), String> {
Ok(contents) => contents,
Err(_) => return Err(format!("An error occurred while attempting to read the given template file '{}'. Please make sure the file exists and you have the permissions necessary to read from it.", &template_path))
};
output = fs::write("./bonnie.toml", contents);
output = fs::write(cfg_path, contents);
} else if matches!(template, Some(_)) && fs::metadata(template.as_ref().unwrap()).is_err() {
// We have a template file that doesn't exist
return Err(format!("The given template file at '{}' does not exist or can't be read. Please make sure the file exists and you have the permissions necessary to read from it.", template.as_ref().unwrap()));
} else {
// Try to get the default template file from `~/.bonnie/template.toml`
// If it's not available, we'll use a pre-programmed default
let template = get_default_template()?;
output = fs::write("./bonnie.toml", template);
output = fs::write(cfg_path, template);
}

match output {
Ok(_) => Ok(()),
Err(_) => Err(String::from("Error creating new bonnie.toml, make sure you have the permissions to write to this directory."))
Err(_) => Err(format!("Error creating new {}, make sure you have the permissions to write to this directory.", cfg_path))
}
}
}

0 comments on commit a81345b

Please sign in to comment.