-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 7 commits
bb36c38
e94c0be
605699e
54d7531
a9a6100
1886ec8
c7e9c29
78b9a24
62d00a5
ccaf18d
91e2aef
dd20fda
17d799c
46a75db
53ce4af
40e0f71
35fc4c9
92932c9
a83bb46
ece01cc
1f79bdb
cc193aa
a7e9625
829b4a4
934611a
600227e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
use lib::{cache, cache_exists, get_cfg, help, init, load_from_cache, Config, BONNIE_VERSION}; | ||
use lib::{ | ||
cache, cache_exists, get_cfg, get_template_path, help, init, load_from_cache, Config, | ||
BONNIE_VERSION, | ||
}; | ||
use std::env; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use std::process::Command as OsCommand; | ||
|
||
// All this does is run the program and terminate with the acquired exit code | ||
fn main() { | ||
|
@@ -41,6 +46,8 @@ fn core() -> Result<i32, String> { | |
writeln!(stdout, "You are currently running Bonnie v{}! You can see the latest release at https://github.com/arctic-hen7/bonnie/releases.", BONNIE_VERSION).expect("Failed to write version."); | ||
return Ok(0); | ||
} else if prog_args[0] == "-i" || prog_args[0] == "--init" { | ||
print!("Initialising..."); | ||
|
||
init( | ||
// See if a template was provided with the `--template`/`-t` flag | ||
match prog_args.get(1).as_ref() { | ||
|
@@ -50,12 +57,57 @@ fn core() -> Result<i32, String> { | |
_ => None, | ||
}, | ||
)?; | ||
|
||
println!(" Done!"); | ||
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" { | ||
let template_path: String = match get_template_path() { | ||
Ok(path) => Ok(path.to_str().unwrap().to_string()), | ||
Err(err) => Err(format!( | ||
"Failed to get template path with the following error: {:#?}", | ||
err | ||
)), | ||
}?; | ||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please be clearer about variable naming here. |
||
} else { | ||
let editor = PathBuf::from(env::var("EDITOR").unwrap_or("nano".to_string())); | ||
|
||
let safe_editor = editor.to_str().ok_or( | ||
"Failed to parse the value within the `EDITOR` environment variable as a path/", | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)?; | ||
|
||
child = OsCommand::new(safe_editor) | ||
.arg(template_path) | ||
.spawn() | ||
.map(|mut x| x.wait()); | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
let result = match child { | ||
Ok(_) => { | ||
println!("Opening template file..."); | ||
arctic-hen7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(0) | ||
} | ||
Err(err) => Err(format!( | ||
"Your editor failed to start with the following error: {:#?}", | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err | ||
)), | ||
}; | ||
|
||
return result; | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
// Check if there's a cache we should read from | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
use crate::template; | ||
use crate::version::BONNIE_VERSION; | ||
|
||
use std::fs; | ||
use std::io; | ||
|
||
// 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 | ||
|
@@ -22,24 +25,29 @@ 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}\" | ||
// get t | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let template = match template::get_default() { | ||
Ok(template) => Ok(template), | ||
Err(template::Error::Other(err)) if err.kind() == io::ErrorKind::NotFound => { | ||
Ok(format!( | ||
"version=\"{version}\" | ||
|
||
[scripts] | ||
start = \"echo \\\"No start script yet!\\\"\" | ||
", | ||
version = BONNIE_VERSION | ||
), | ||
); | ||
", | ||
version = BONNIE_VERSION | ||
)) | ||
} | ||
Err(err) => Err(err), | ||
} | ||
.map_err(|err| format!("{:#?}", err))?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use well-formed, informative string errors at the site of the error. Then propagate them up to |
||
|
||
output = fs::write("bonnie.toml", 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 bonnie.toml, make sure you have the permissions to write to this directory.")) | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use home::home_dir; | ||
|
||
use std::env; | ||
use std::fs; | ||
use std::io; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
CouldNotGetHomeDirectory, | ||
Other(io::Error), | ||
} | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pub fn get_template_path() -> Result<PathBuf, Error> { | ||
let default_template_path = home_dir() | ||
.map(|x| x.join(".bonnie").join("template.toml")) | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.ok_or(Error::CouldNotGetHomeDirectory)?; | ||
|
||
Ok(env::var("BONNIE_TEMPLATE") | ||
.map(|x| PathBuf::from(x)) | ||
ostev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.unwrap_or(default_template_path)) | ||
} | ||
|
||
pub fn get_default() -> Result<String, Error> { | ||
let path = get_template_path()?; | ||
|
||
let template = fs::read_to_string(path); | ||
|
||
return template.map_err(|err| Error::Other(err)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice touch! We are however just copying from a file, so maybe just the second part!