Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default template support #21

Merged
merged 26 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bb36c38
feat(init): :sparkles: Add default templates
ostev Jul 10, 2021
e94c0be
Merge branch 'arctic-hen7:main' into main
ostev Jul 10, 2021
605699e
feat(init): :sparkles: Add edit template file flag
ostev Jul 11, 2021
54d7531
docs(help): :memo: Update help text to add `-e`
ostev Jul 11, 2021
a9a6100
fix(init): :bug: Change template environment variable
ostev Jul 11, 2021
1886ec8
docs(help): :memo: Update help with new env var
ostev Jul 11, 2021
c7e9c29
docs(wiki): :memo: Update docs with new env var
ostev Jul 11, 2021
78b9a24
Update src/help.rs
ostev Jul 12, 2021
62d00a5
Update src/help.rs
ostev Jul 12, 2021
ccaf18d
Update src/init.rs
ostev Jul 12, 2021
91e2aef
Update src/init.rs
ostev Jul 12, 2021
dd20fda
Update src/bin/main.rs
ostev Jul 12, 2021
17d799c
Update src/bin/main.rs
ostev Jul 12, 2021
46a75db
refactor(init): :recycle: use `String`s as errors
ostev Jul 12, 2021
53ce4af
Merge branch 'main' of github.com:ostev/bonnie into main
ostev Jul 12, 2021
40e0f71
refactor: :recycle: Extract `-e` into function
ostev Jul 12, 2021
35fc4c9
refactor(init): :recycle: Remove "Initialising..." text
ostev Jul 12, 2021
92932c9
refactor(init): :recycle: Remove needles format\
ostev Jul 12, 2021
a83bb46
refactor(init): :recycle: Remove unclear names
ostev Jul 12, 2021
ece01cc
fix: :bug: Add error message if template not found
ostev Jul 12, 2021
1f79bdb
docs(wiki): :memo: Add info about `-e`
ostev Jul 12, 2021
cc193aa
fix: :bug: Add more descriptive error
ostev Jul 12, 2021
a7e9625
fix(init): :bug: Add actual path to error message
ostev Jul 12, 2021
829b4a4
Update src/template.rs
ostev Jul 12, 2021
934611a
Update src/template.rs
ostev Jul 12, 2021
600227e
style: :art: Run `cargo fmt`
ostev Jul 12, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"conventionalCommits.scopes": [
"install_scripts",
"wiki"
"wiki",
"init",
"help"
]
}
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ edition = "2018"

[dependencies]
toml = "0.5.8"
serde = { version = "1.0.125", features = ["derive"] }
serde = { version="1.0.125", features=["derive"] }
serde_json = "1.0.64"
dotenv = "0.15.0"
regex = "1.5.4"
home = "0.5.3"

[lib]
name = "lib"
Expand Down
9 changes: 8 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use lib::{cache, cache_exists, get_cfg, help, init, load_from_cache, Config, BONNIE_VERSION};
use lib::{
cache, cache_exists, get_cfg, help, init, load_from_cache, template, Config, BONNIE_VERSION,
};
use std::env;
use std::io::Write;

Expand Down Expand Up @@ -50,12 +52,17 @@ fn core() -> Result<i32, String> {
_ => None,
},
)?;

println!("A new Bonnie configuration file has been initialized at [PATH]!");
ostev marked this conversation as resolved.
Show resolved Hide resolved

return Ok(0);
} else if prog_args[0] == "-h" || prog_args[0] == "--help" {
help(stdout);
return Ok(0);
} else if prog_args[0] == "-c" || prog_args[0] == "--cache" {
should_cache = true;
} else if prog_args[0] == "-e" || prog_args[0] == "--edit-template" {
return template::edit().map(|_| 0);
}
}
// Check if there's a cache we should read from
Expand Down
4 changes: 3 additions & 1 deletion src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ 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, potentially taking a template file to use
-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
-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.
The expected location of a Bonnie cache file can be changed from the default `./.bonnie.cache.json` by setting the `BONNIE_CACHE` environment variable.
The expected location of your default template can be changed from the default `~/.bonnie/template.toml` by setting the `BONNIE_TEMPLATE` environment variable.
Further information can be found at https://github.com/arctic-hen7/bonnie/wiki.
",
Expand Down
30 changes: 19 additions & 11 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::template;
use crate::version::BONNIE_VERSION;

use std::fs;

// Creates a new Bonnie configuration file using a template, or from the default
pub fn init(template: Option<String>) -> Result<(), String> {
// Check if there's already a config file in this directory
if fs::metadata("./bonnie.toml").is_ok() {
if fs::metadata("bonnie.toml").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 @@ -22,19 +24,25 @@ pub fn init(template: Option<String>) -> Result<(), String> {
// 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 {
// Create a new `bonnie.toml` file using the default
// TODO read the default from `~/.bonnie/template.toml` if it exists
output = fs::write(
"./bonnie.toml",
format!(
"version=\"{version}\"
// 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 = match template::get_default() {
Ok(template) => Ok(template),
// Not ideal, but...
Err(err) if err == "The system cannot find the file specified. (os error 2)" => {
Ok(format!(
"version=\"{version}\"

[scripts]
start = \"echo \\\"No start script yet!\\\"\"
",
version = BONNIE_VERSION
),
);
",
version = BONNIE_VERSION
))
}
Err(err) => Err(err),
}?;

output = fs::write("bonnie.toml", template)
}

match output {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod help;
mod init;
mod raw_schema;
mod schema;
pub mod template;
mod version;

pub use crate::cache::{cache, cache_exists, load_from_cache};
Expand Down
87 changes: 87 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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(format!(
"I could not find your home directory. {}",
if cfg!(target_os = "windows") {
"That is most odd."
} else {
"Is the `HOME` environment variable set?"
}
))?;
arctic-hen7 marked this conversation as resolved.
Show resolved Hide resolved

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;

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());
} 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());
}

let result = match child {
Ok(_) => Ok(()),
Err(err) => Err(format!(
"Your editor failed to start with the following error: {:#?}",
err
)),
};

return result;
}
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from b772fd to 0c133e