mirror of
https://github.com/nushell/nushell
synced 2024-12-26 13:03:07 +00:00
commit
60dbca8bc6
4 changed files with 75 additions and 0 deletions
|
@ -244,6 +244,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
|||
whole_stream_command(Tags),
|
||||
whole_stream_command(First),
|
||||
whole_stream_command(Last),
|
||||
whole_stream_command(Env),
|
||||
whole_stream_command(FromCSV),
|
||||
whole_stream_command(FromTSV),
|
||||
whole_stream_command(FromINI),
|
||||
|
|
|
@ -13,6 +13,7 @@ pub(crate) mod date;
|
|||
pub(crate) mod debug;
|
||||
pub(crate) mod echo;
|
||||
pub(crate) mod enter;
|
||||
pub(crate) mod env;
|
||||
pub(crate) mod exit;
|
||||
pub(crate) mod fetch;
|
||||
pub(crate) mod first;
|
||||
|
@ -78,6 +79,7 @@ pub(crate) use date::Date;
|
|||
pub(crate) use debug::Debug;
|
||||
pub(crate) use echo::Echo;
|
||||
pub(crate) use enter::Enter;
|
||||
pub(crate) use env::Env;
|
||||
pub(crate) use exit::Exit;
|
||||
pub(crate) use fetch::Fetch;
|
||||
pub(crate) use first::First;
|
||||
|
|
68
src/commands/env.rs
Normal file
68
src/commands/env.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use crate::data::{Dictionary, Value};
|
||||
use crate::errors::ShellError;
|
||||
use crate::prelude::*;
|
||||
use crate::TaggedDictBuilder;
|
||||
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::parser::registry::Signature;
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_environment(tag: Tag) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
|
||||
let mut indexmap = IndexMap::new();
|
||||
|
||||
let path = std::env::current_dir()?;
|
||||
indexmap.insert("cwd".to_string(), Value::path(path).tagged(tag));
|
||||
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
indexmap.insert("home".to_string(), Value::path(home).tagged(tag));
|
||||
}
|
||||
|
||||
let temp = std::env::temp_dir();
|
||||
indexmap.insert("temp".to_string(), Value::path(temp).tagged(tag));
|
||||
|
||||
let mut dict = TaggedDictBuilder::new(tag);
|
||||
for v in std::env::vars() {
|
||||
dict.insert(v.0, Value::string(v.1));
|
||||
}
|
||||
if !dict.is_empty() {
|
||||
indexmap.insert("vars".to_string(), dict.into_tagged_value());
|
||||
}
|
||||
|
||||
Ok(Value::Row(Dictionary::from(indexmap)).tagged(tag))
|
||||
}
|
||||
|
||||
pub fn env(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
|
||||
let mut env_out = VecDeque::new();
|
||||
let tag = args.call_info.name_tag;
|
||||
|
||||
let value = get_environment(tag)?;
|
||||
env_out.push_back(value);
|
||||
|
||||
Ok(env_out.to_output_stream())
|
||||
}
|
|
@ -169,6 +169,10 @@ impl TaggedDictBuilder {
|
|||
pub fn into_tagged_dict(self) -> Tagged<Dictionary> {
|
||||
Dictionary { entries: self.dict }.tagged(self.tag)
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.dict.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TaggedDictBuilder> for Tagged<Value> {
|
||||
|
|
Loading…
Reference in a new issue