forked from extern/nushell
Refactor nu-cli/env* (#3041)
* Revert "History, more test coverage improvements, and refactorings. (#3217)"
This reverts commit 8fc8fc89aa
.
* Add tests
* Refactor .nu-env
* Change logic of Config write to logic of read()
* Fix reload always appends to old vars
* Fix reload always takes last_modified of global config
* Add reload_config in evaluation context
* Reload config after writing to it in cfg set / cfg set_into
* Add --no-history to cli options
* Use --no-history in tests
* Add comment about maybe_print_errors
* Get ctrl_exit var from context.global_config
* Use context.global_config in command "config"
* Add Readme in engine how env vars are now handled
* Update docs from autoenv command
* Move history_path from engine to nu_data
* Move load history out of if
* No let before return
* Add import for indexmap
This commit is contained in:
@ -10,7 +10,8 @@ use futures::stream::BoxStream;
|
||||
use futures::StreamExt;
|
||||
use futures_codec::FramedRead;
|
||||
use futures_util::TryStreamExt;
|
||||
use nu_protocol::{TaggedDictBuilder, Value};
|
||||
use nu_data::config::LocalConfigDiff;
|
||||
use nu_protocol::{CommandAction, ConfigPath, TaggedDictBuilder, Value};
|
||||
use nu_source::{Span, Tag};
|
||||
use nu_stream::{Interruptible, OutputStream, ToOutputStream};
|
||||
use std::collections::HashMap;
|
||||
@ -27,9 +28,16 @@ use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Copy)]
|
||||
pub enum FilesystemShellMode {
|
||||
Cli,
|
||||
Script,
|
||||
}
|
||||
|
||||
pub struct FilesystemShell {
|
||||
pub(crate) path: String,
|
||||
pub(crate) last_path: String,
|
||||
pub(crate) mode: FilesystemShellMode,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for FilesystemShell {
|
||||
@ -43,12 +51,13 @@ impl Clone for FilesystemShell {
|
||||
FilesystemShell {
|
||||
path: self.path.clone(),
|
||||
last_path: self.path.clone(),
|
||||
mode: self.mode,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FilesystemShell {
|
||||
pub fn basic() -> Result<FilesystemShell, Error> {
|
||||
pub fn basic(mode: FilesystemShellMode) -> Result<FilesystemShell, Error> {
|
||||
let path = match std::env::current_dir() {
|
||||
Ok(path) => path,
|
||||
Err(_) => PathBuf::from("/"),
|
||||
@ -57,15 +66,23 @@ impl FilesystemShell {
|
||||
Ok(FilesystemShell {
|
||||
path: path.to_string_lossy().to_string(),
|
||||
last_path: path.to_string_lossy().to_string(),
|
||||
mode,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_location(path: String) -> Result<FilesystemShell, std::io::Error> {
|
||||
pub fn with_location(
|
||||
path: String,
|
||||
mode: FilesystemShellMode,
|
||||
) -> Result<FilesystemShell, std::io::Error> {
|
||||
let path = canonicalize(std::env::current_dir()?, &path)?;
|
||||
let path = path.display().to_string();
|
||||
let last_path = path.clone();
|
||||
|
||||
Ok(FilesystemShell { path, last_path })
|
||||
Ok(FilesystemShell {
|
||||
path,
|
||||
last_path,
|
||||
mode,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -255,6 +272,43 @@ impl Shell for FilesystemShell {
|
||||
path.to_string_lossy().to_string(),
|
||||
));
|
||||
|
||||
//Loading local configs in script mode, makes scripts behave different on different
|
||||
//filesystems and might therefore surprise users. That's why we only load them in cli mode.
|
||||
if self.mode == FilesystemShellMode::Cli {
|
||||
match dunce::canonicalize(self.path()) {
|
||||
Err(e) => {
|
||||
let err = ShellError::untagged_runtime_error(format!(
|
||||
"Could not get absolute path from current fs shell. The error was: {:?}",
|
||||
e
|
||||
));
|
||||
stream.push_back(ReturnSuccess::value(
|
||||
UntaggedValue::Error(err).into_value(Tag::unknown()),
|
||||
));
|
||||
}
|
||||
Ok(current_pwd) => {
|
||||
let (changes, errs) = LocalConfigDiff::between(current_pwd, path);
|
||||
|
||||
for err in errs {
|
||||
stream.push_back(ReturnSuccess::value(
|
||||
UntaggedValue::Error(err).into_value(Tag::unknown()),
|
||||
));
|
||||
}
|
||||
|
||||
for unload_cfg in changes.cfgs_to_unload {
|
||||
stream.push_back(ReturnSuccess::action(CommandAction::UnloadConfig(
|
||||
ConfigPath::Local(unload_cfg),
|
||||
)));
|
||||
}
|
||||
|
||||
for load_cfg in changes.cfgs_to_load {
|
||||
stream.push_back(ReturnSuccess::action(CommandAction::LoadConfig(
|
||||
ConfigPath::Local(load_cfg),
|
||||
)));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(stream.into())
|
||||
}
|
||||
|
||||
@ -777,6 +831,10 @@ impl Shell for FilesystemShell {
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_interactive(&self) -> bool {
|
||||
self.mode == FilesystemShellMode::Cli
|
||||
}
|
||||
}
|
||||
|
||||
struct TaggedPathBuf<'a>(&'a PathBuf, &'a Tag);
|
||||
|
Reference in New Issue
Block a user