2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2017-12-02 05:37:10 -08:00
|
|
|
use target;
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
pub(crate) enum Function {
|
|
|
|
Nullary(fn(&FunctionContext) -> Result<String, String>),
|
|
|
|
Unary(fn(&FunctionContext, &str) -> Result<String, String>),
|
|
|
|
Binary(fn(&FunctionContext, &str, &str) -> Result<String, String>),
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
lazy_static! {
|
2019-11-21 10:14:10 -08:00
|
|
|
pub(crate) static ref TABLE: BTreeMap<&'static str, Function> = vec![
|
2018-12-08 14:29:41 -08:00
|
|
|
("arch", Function::Nullary(arch)),
|
|
|
|
("os", Function::Nullary(os)),
|
|
|
|
("os_family", Function::Nullary(os_family)),
|
|
|
|
("env_var", Function::Unary(env_var)),
|
|
|
|
("env_var_or_default", Function::Binary(env_var_or_default)),
|
|
|
|
(
|
|
|
|
"invocation_directory",
|
|
|
|
Function::Nullary(invocation_directory)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
2019-11-21 10:14:10 -08:00
|
|
|
pub(crate) fn argc(&self) -> usize {
|
2017-12-02 14:59:07 -08:00
|
|
|
use self::Function::*;
|
|
|
|
match *self {
|
|
|
|
Nullary(_) => 0,
|
2018-12-08 14:29:41 -08:00
|
|
|
Unary(_) => 1,
|
|
|
|
Binary(_) => 2,
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn arch(_context: &FunctionContext) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(target::arch().to_string())
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn os(_context: &FunctionContext) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(target::os().to_string())
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn os_family(_context: &FunctionContext) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(target::os_family().to_string())
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn invocation_directory(context: &FunctionContext) -> Result<String, String> {
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
Platform::to_shell_path(context.working_directory, context.invocation_directory)
|
|
|
|
.map_err(|e| format!("Error getting shell path: {}", e))
|
2018-06-19 10:04:03 -07:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn env_var(context: &FunctionContext, key: &str) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
use std::env::VarError::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
if let Some(value) = context.dotenv.get(key) {
|
|
|
|
return Ok(value.clone());
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
match env::var(key) {
|
|
|
|
Err(NotPresent) => Err(format!("environment variable `{}` not present", key)),
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(NotUnicode(os_string)) => Err(format!(
|
|
|
|
"environment variable `{}` not unicode: {:?}",
|
|
|
|
key, os_string
|
|
|
|
)),
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(value) => Ok(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
fn env_var_or_default(
|
2018-03-17 09:17:41 -07:00
|
|
|
context: &FunctionContext,
|
2018-12-08 14:29:41 -08:00
|
|
|
key: &str,
|
|
|
|
default: &str,
|
2018-03-17 09:17:41 -07:00
|
|
|
) -> Result<String, String> {
|
|
|
|
if let Some(value) = context.dotenv.get(key) {
|
|
|
|
return Ok(value.clone());
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
use std::env::VarError::*;
|
|
|
|
match env::var(key) {
|
|
|
|
Err(NotPresent) => Ok(default.to_string()),
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(NotUnicode(os_string)) => Err(format!(
|
|
|
|
"environment variable `{}` not unicode: {:?}",
|
|
|
|
key, os_string
|
|
|
|
)),
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(value) => Ok(value),
|
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|