-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ added support for default template (#21)
* feat(init): ✨ Add default templates This is used for `bonnie --init`. It fetches the file at `$BONNIE_TEMPLATE_PATH`, falling back to `~/.bonnie/template.toml` if necessary. The `home` crate was introduced to handle the finding of the user's home directory on Windows, which is surprisingly complicated and **can fail**. * feat(init): ✨ Add edit template file flag Using the `-e` or `--edit-template` flag, the user can now open the template file using the editor in the `EDITOR` environment variable or, on Windows, `start`. * docs(help): 📝 Update help text to add `-e` * fix(init): 🐛 Change template environment variable It is now `BONNIE_TEMPLATE` to match `BONNIE_CONFIG`. This breaks with the previous implementation. * docs(help): 📝 Update help with new env var * docs(wiki): 📝 Update docs with new env var Changed `BONNIE_TEMPLATE_PATH` to `BONNIE_TEMPLATE`. * Update src/help.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * Update src/help.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * Update src/init.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * Update src/init.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * Update src/bin/main.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * Update src/bin/main.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * refactor(init): ♻️ use `String`s as errors * refactor: ♻️ Extract `-e` into function * refactor(init): ♻️ Remove "Initialising..." text Copying a file doesn't take that long. * refactor(init): ♻️ Remove needles format\ It added quotes around a string. * refactor(init): ♻️ Remove unclear names * fix: 🐛 Add error message if template not found * docs(wiki): 📝 Add info about `-e` * fix: 🐛 Add more descriptive error * fix(init): 🐛 Add actual path to error message * Update src/template.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * Update src/template.rs Co-authored-by: arctic_hen7 <arctic_hen7@pm.me> * style: 🎨 Run `cargo fmt` Co-authored-by: arctic_hen7 <arctic_hen7@pm.me>
- Loading branch information
1 parent
8c56d3e
commit 7c3eec6
Showing
9 changed files
with
157 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{ | ||
"conventionalCommits.scopes": [ | ||
"install_scripts", | ||
"wiki" | ||
"wiki", | ||
"init", | ||
"help" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use home::home_dir; | ||
|
||
use std::env; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
use std::process::Command as OsCommand; | ||
|
||
pub fn get_template_path() -> Result<PathBuf, String> { | ||
let default_template_path = home_dir() | ||
.map(|path| path.join(".bonnie").join("template.toml")) | ||
.ok_or(String::from( | ||
"Your home directory couldn't be found. Please check your system configuration.", | ||
))?; | ||
|
||
Ok(env::var("BONNIE_TEMPLATE") | ||
.map(|value| PathBuf::from(value)) | ||
.unwrap_or(default_template_path)) | ||
} | ||
|
||
pub fn get_default() -> Result<String, String> { | ||
let path = get_template_path()?; | ||
|
||
let template = fs::read_to_string(path); | ||
|
||
template.map_err(|err| err.to_string()) | ||
} | ||
|
||
pub fn edit() -> Result<(), String> { | ||
// This can take a little while with with `start` on Windows | ||
println!("Opening template file..."); | ||
|
||
let template_path: String = match get_template_path() { | ||
Ok(path) => path | ||
.to_str() | ||
.map(String::from) | ||
.ok_or(String::from("The path provided is not valid Unicode.")), | ||
Err(err) => Err(format!( | ||
"Failed to get template path with the following error: {}", | ||
err | ||
)), | ||
}?; | ||
|
||
let template_exists = fs::metadata(&template_path).is_ok(); | ||
|
||
if !template_exists { | ||
return Err(format!( | ||
"I could not find a template file to edit at {}.", | ||
template_path | ||
)); | ||
} | ||
|
||
let child; | ||
|
||
let command; | ||
|
||
if cfg!(target_os = "windows") { | ||
// We need to spawn a `powershell` process to make `start` available. | ||
child = OsCommand::new("powershell") | ||
.arg(format!("start '{}'", template_path)) | ||
.spawn() | ||
.map(|mut x| x.wait()); | ||
|
||
command = format!("powershell -command 'start {}'", template_path); | ||
} else { | ||
let editor = PathBuf::from(env::var("EDITOR").unwrap_or("nano".to_string())); | ||
|
||
let safe_editor = editor.to_str().ok_or( | ||
"The value given in the 'EDITOR' environment variable couldn't be parsed as a valid path.", | ||
)?; | ||
|
||
child = OsCommand::new(safe_editor) | ||
.arg(&template_path) | ||
.spawn() | ||
.map(|mut x| x.wait()); | ||
|
||
command = format!("{} {}", safe_editor, template_path); | ||
} | ||
|
||
let result = match child { | ||
Ok(_) => Ok(()), | ||
Err(err) => Err(format!( | ||
"The specified editor failed to start with the following error: '{}' when the command '{}' was run.", | ||
err, command | ||
)), | ||
}; | ||
|
||
return result; | ||
} |
Submodule wiki
updated
from b772fd to 5cdd38