2019-09-19 21:10:29 +00:00
|
|
|
use crate::cli::History;
|
2019-12-04 19:52:31 +00:00
|
|
|
use crate::data::config;
|
2019-09-16 07:52:58 +00:00
|
|
|
use crate::prelude::*;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{Dictionary, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-09-16 07:52:58 +00:00
|
|
|
|
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
|
|
|
|
pub struct Env;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Env {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"env"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("env")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Get the current environment."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
env(args, registry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn get_environment(tag: Tag) -> Result<Value, Box<dyn std::error::Error>> {
|
2019-09-16 07:52:58 +00:00
|
|
|
let mut indexmap = IndexMap::new();
|
|
|
|
|
|
|
|
let path = std::env::current_dir()?;
|
2019-12-04 19:52:31 +00:00
|
|
|
indexmap.insert(
|
|
|
|
"cwd".to_string(),
|
|
|
|
UntaggedValue::path(path).into_value(&tag),
|
|
|
|
);
|
2019-09-16 07:52:58 +00:00
|
|
|
|
|
|
|
if let Some(home) = dirs::home_dir() {
|
2019-12-04 19:52:31 +00:00
|
|
|
indexmap.insert(
|
|
|
|
"home".to_string(),
|
|
|
|
UntaggedValue::path(home).into_value(&tag),
|
|
|
|
);
|
2019-09-16 07:52:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 19:39:47 +00:00
|
|
|
let config = config::default_path()?;
|
2019-12-04 19:52:31 +00:00
|
|
|
indexmap.insert(
|
|
|
|
"config".to_string(),
|
|
|
|
UntaggedValue::path(config).into_value(&tag),
|
|
|
|
);
|
2019-09-19 19:39:47 +00:00
|
|
|
|
2019-09-19 21:10:29 +00:00
|
|
|
let history = History::path();
|
2019-12-04 19:52:31 +00:00
|
|
|
indexmap.insert(
|
|
|
|
"history".to_string(),
|
|
|
|
UntaggedValue::path(history).into_value(&tag),
|
|
|
|
);
|
2019-09-19 21:10:29 +00:00
|
|
|
|
2019-09-16 07:52:58 +00:00
|
|
|
let temp = std::env::temp_dir();
|
2019-12-04 19:52:31 +00:00
|
|
|
indexmap.insert(
|
|
|
|
"temp".to_string(),
|
|
|
|
UntaggedValue::path(temp).into_value(&tag),
|
|
|
|
);
|
2019-09-16 07:52:58 +00:00
|
|
|
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(&tag);
|
2019-09-16 07:52:58 +00:00
|
|
|
for v in std::env::vars() {
|
2019-12-04 19:52:31 +00:00
|
|
|
dict.insert_untagged(v.0, UntaggedValue::string(v.1));
|
2019-09-16 07:52:58 +00:00
|
|
|
}
|
|
|
|
if !dict.is_empty() {
|
2019-11-21 14:33:14 +00:00
|
|
|
indexmap.insert("vars".to_string(), dict.into_value());
|
2019-09-16 07:52:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
Ok(UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag))
|
2019-09-16 07:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn env(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
|
|
|
|
let mut env_out = VecDeque::new();
|
2019-10-13 04:12:43 +00:00
|
|
|
let tag = args.call_info.name_tag.clone();
|
2019-09-16 07:52:58 +00:00
|
|
|
|
|
|
|
let value = get_environment(tag)?;
|
|
|
|
env_out.push_back(value);
|
|
|
|
|
|
|
|
Ok(env_out.to_output_stream())
|
|
|
|
}
|