Merge 846dd480ec
into 4e83ccdf86
This commit is contained in:
commit
17ce40be49
|
@ -5,6 +5,8 @@ use reedline::{
|
|||
SqliteBackedHistory,
|
||||
};
|
||||
|
||||
use crate::config_files::get_history_path;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct History;
|
||||
|
||||
|
@ -44,89 +46,76 @@ impl Command for History {
|
|||
};
|
||||
|
||||
// todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history`
|
||||
if let Some(config_path) = nu_path::config_dir() {
|
||||
let clear = call.has_flag(engine_state, stack, "clear")?;
|
||||
let long = call.has_flag(engine_state, stack, "long")?;
|
||||
let signals = engine_state.signals().clone();
|
||||
let Some(history_path) = get_history_path(history.file_format) else {
|
||||
return Err(ShellError::ConfigDirNotFound { span: Some(head) });
|
||||
};
|
||||
let clear = call.has_flag(engine_state, stack, "clear")?;
|
||||
let long = call.has_flag(engine_state, stack, "long")?;
|
||||
let signals = engine_state.signals().clone();
|
||||
|
||||
let mut history_path = config_path;
|
||||
history_path.push("nushell");
|
||||
match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
history_path.push("history.sqlite3");
|
||||
}
|
||||
HistoryFileFormat::PlainText => {
|
||||
history_path.push("history.txt");
|
||||
}
|
||||
}
|
||||
|
||||
if clear {
|
||||
let _ = std::fs::remove_file(history_path);
|
||||
// TODO: FIXME also clear the auxiliary files when using sqlite
|
||||
Ok(PipelineData::empty())
|
||||
} else {
|
||||
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
SqliteBackedHistory::with_file(history_path.clone().into(), None, None)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
|
||||
HistoryFileFormat::PlainText => FileBackedHistory::with_file(
|
||||
history.max_size as usize,
|
||||
history_path.clone().into(),
|
||||
)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok(),
|
||||
};
|
||||
|
||||
match history.file_format {
|
||||
HistoryFileFormat::PlainText => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
Value::record(
|
||||
record! {
|
||||
"command" => Value::string(entry.command_line, head),
|
||||
"index" => Value::int(idx as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
create_history_record(idx, entry, long, head)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
}
|
||||
}
|
||||
if clear {
|
||||
let _ = std::fs::remove_file(history_path);
|
||||
// TODO: FIXME also clear the auxiliary files when using sqlite
|
||||
Ok(PipelineData::empty())
|
||||
} else {
|
||||
Err(ShellError::ConfigDirNotFound { span: Some(head) })
|
||||
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
SqliteBackedHistory::with_file(history_path.clone(), None, None)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
|
||||
HistoryFileFormat::PlainText => {
|
||||
FileBackedHistory::with_file(history.max_size as usize, history_path.clone())
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
};
|
||||
match history.file_format {
|
||||
HistoryFileFormat::PlainText => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
Value::record(
|
||||
record! {
|
||||
"command" => Value::string(entry.command_line, head),
|
||||
"index" => Value::int(idx as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(move |(idx, entry)| create_history_record(idx, entry, long, head))
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,15 +16,8 @@ const PLUGIN_FILE: &str = "plugin.msgpackz";
|
|||
#[cfg(feature = "plugin")]
|
||||
const OLD_PLUGIN_FILE: &str = "plugin.nu";
|
||||
|
||||
const HISTORY_FILE_TXT: &str = "history.txt";
|
||||
const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn read_plugin_file(
|
||||
engine_state: &mut EngineState,
|
||||
plugin_file: Option<Spanned<String>>,
|
||||
storage_path: &str,
|
||||
) {
|
||||
pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
|
||||
use nu_protocol::ShellError;
|
||||
use std::path::Path;
|
||||
|
||||
|
@ -52,7 +45,7 @@ pub fn read_plugin_file(
|
|||
let mut start_time = std::time::Instant::now();
|
||||
// Reading signatures from plugin registry file
|
||||
// The plugin.msgpackz file stores the parsed signature collected from each registered plugin
|
||||
add_plugin_file(engine_state, plugin_file.clone(), storage_path);
|
||||
add_plugin_file(engine_state, plugin_file.clone());
|
||||
perf!(
|
||||
"add plugin file to engine_state",
|
||||
start_time,
|
||||
|
@ -70,8 +63,7 @@ pub fn read_plugin_file(
|
|||
log::warn!("Plugin file not found: {}", plugin_path.display());
|
||||
|
||||
// Try migration of an old plugin file if this wasn't a custom plugin file
|
||||
if plugin_file.is_none() && migrate_old_plugin_file(engine_state, storage_path)
|
||||
{
|
||||
if plugin_file.is_none() && migrate_old_plugin_file(engine_state) {
|
||||
let Ok(file) = std::fs::File::open(&plugin_path) else {
|
||||
log::warn!("Failed to load newly migrated plugin file");
|
||||
return;
|
||||
|
@ -159,11 +151,7 @@ pub fn read_plugin_file(
|
|||
}
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn add_plugin_file(
|
||||
engine_state: &mut EngineState,
|
||||
plugin_file: Option<Spanned<String>>,
|
||||
storage_path: &str,
|
||||
) {
|
||||
pub fn add_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
|
||||
use std::path::Path;
|
||||
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
|
@ -189,9 +177,8 @@ pub fn add_plugin_file(
|
|||
),
|
||||
);
|
||||
}
|
||||
} else if let Some(mut plugin_path) = nu_path::config_dir() {
|
||||
} else if let Some(plugin_path) = nu_path::nu_config_dir() {
|
||||
// Path to store plugins signatures
|
||||
plugin_path.push(storage_path);
|
||||
let mut plugin_path =
|
||||
canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path.into());
|
||||
plugin_path.push(PLUGIN_FILE);
|
||||
|
@ -241,19 +228,15 @@ pub fn eval_config_contents(
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> Option<PathBuf> {
|
||||
nu_path::config_dir().map(|mut history_path| {
|
||||
history_path.push(storage_path);
|
||||
history_path.push(match mode {
|
||||
HistoryFileFormat::PlainText => HISTORY_FILE_TXT,
|
||||
HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE,
|
||||
});
|
||||
pub(crate) fn get_history_path(mode: HistoryFileFormat) -> Option<PathBuf> {
|
||||
nu_path::nu_config_dir().map(|mut history_path| {
|
||||
history_path.push(mode.default_file_name());
|
||||
history_path.into()
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool {
|
||||
pub fn migrate_old_plugin_file(engine_state: &EngineState) -> bool {
|
||||
use nu_protocol::{
|
||||
PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature,
|
||||
ShellError,
|
||||
|
@ -266,10 +249,9 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -
|
|||
return false;
|
||||
};
|
||||
|
||||
let Some(config_dir) = nu_path::config_dir().and_then(|mut dir| {
|
||||
dir.push(storage_path);
|
||||
nu_path::canonicalize_with(dir, &cwd).ok()
|
||||
}) else {
|
||||
let Some(config_dir) =
|
||||
nu_path::nu_config_dir().and_then(|dir| nu_path::canonicalize_with(dir, &cwd).ok())
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
|
||||
|
|
|
@ -52,7 +52,6 @@ use sysinfo::System;
|
|||
pub fn evaluate_repl(
|
||||
engine_state: &mut EngineState,
|
||||
stack: Stack,
|
||||
nushell_path: &str,
|
||||
prerun_command: Option<Spanned<String>>,
|
||||
load_std_lib: Option<Spanned<String>>,
|
||||
entire_start_time: Instant,
|
||||
|
@ -99,7 +98,7 @@ pub fn evaluate_repl(
|
|||
|
||||
unique_stack.add_env_var("LAST_EXIT_CODE".into(), Value::int(0, Span::unknown()));
|
||||
|
||||
let mut line_editor = get_line_editor(engine_state, nushell_path, use_color)?;
|
||||
let mut line_editor = get_line_editor(engine_state, use_color)?;
|
||||
let temp_file = temp_dir().join(format!("{}.nu", uuid::Uuid::new_v4()));
|
||||
|
||||
if let Some(s) = prerun_command {
|
||||
|
@ -200,7 +199,7 @@ pub fn evaluate_repl(
|
|||
}
|
||||
Err(_) => {
|
||||
// line_editor is lost in the error case so reconstruct a new one
|
||||
line_editor = get_line_editor(engine_state, nushell_path, use_color)?;
|
||||
line_editor = get_line_editor(engine_state, use_color)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -208,11 +207,7 @@ pub fn evaluate_repl(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn get_line_editor(
|
||||
engine_state: &mut EngineState,
|
||||
nushell_path: &str,
|
||||
use_color: bool,
|
||||
) -> Result<Reedline> {
|
||||
fn get_line_editor(engine_state: &mut EngineState, use_color: bool) -> Result<Reedline> {
|
||||
let mut start_time = std::time::Instant::now();
|
||||
let mut line_editor = Reedline::create();
|
||||
|
||||
|
@ -223,7 +218,7 @@ fn get_line_editor(
|
|||
if let Some(history) = engine_state.history_config() {
|
||||
start_time = std::time::Instant::now();
|
||||
|
||||
line_editor = setup_history(nushell_path, engine_state, line_editor, history)?;
|
||||
line_editor = setup_history(engine_state, line_editor, history)?;
|
||||
|
||||
perf!("setup history", start_time, use_color);
|
||||
}
|
||||
|
@ -1037,7 +1032,6 @@ fn flush_engine_state_repl_buffer(engine_state: &mut EngineState, line_editor: &
|
|||
/// Setup history management for Reedline
|
||||
///
|
||||
fn setup_history(
|
||||
nushell_path: &str,
|
||||
engine_state: &mut EngineState,
|
||||
line_editor: Reedline,
|
||||
history: HistoryConfig,
|
||||
|
@ -1049,7 +1043,7 @@ fn setup_history(
|
|||
None
|
||||
};
|
||||
|
||||
if let Some(path) = crate::config_files::get_history_path(nushell_path, history.file_format) {
|
||||
if let Some(path) = crate::config_files::get_history_path(history.file_format) {
|
||||
return update_line_editor_history(
|
||||
engine_state,
|
||||
path,
|
||||
|
@ -1317,8 +1311,7 @@ fn trailing_slash_looks_like_path() {
|
|||
fn are_session_ids_in_sync() {
|
||||
let engine_state = &mut EngineState::new();
|
||||
let history = engine_state.history_config().unwrap();
|
||||
let history_path =
|
||||
crate::config_files::get_history_path("nushell", history.file_format).unwrap();
|
||||
let history_path = crate::config_files::get_history_path(history.file_format).unwrap();
|
||||
let line_editor = reedline::Reedline::create();
|
||||
let history_session_id = reedline::Reedline::create_history_session_id();
|
||||
let line_editor = update_line_editor_history(
|
||||
|
|
|
@ -30,3 +30,11 @@ pub fn config_dir() -> Option<AbsolutePathBuf> {
|
|||
.or_else(|| dirs::config_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
|
||||
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
|
||||
}
|
||||
|
||||
/// Return the nushell config directory.
|
||||
pub fn nu_config_dir() -> Option<AbsolutePathBuf> {
|
||||
config_dir().map(|mut p| {
|
||||
p.push("nushell");
|
||||
p
|
||||
})
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ mod trailing_slash;
|
|||
|
||||
pub use components::components;
|
||||
pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs};
|
||||
pub use helpers::{cache_dir, config_dir, data_dir, home_dir};
|
||||
pub use helpers::{cache_dir, config_dir, data_dir, home_dir, nu_config_dir};
|
||||
pub use path::*;
|
||||
pub use tilde::expand_tilde;
|
||||
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{Record, ShellError, Span, Value};
|
||||
use std::{collections::HashMap, fmt::Display, str::FromStr};
|
||||
use std::{collections::HashMap, fmt::Display, str::FromStr, sync::Arc};
|
||||
|
||||
pub(super) trait ReconstructVal {
|
||||
fn reconstruct_value(&self, span: Span) -> Value;
|
||||
|
@ -88,6 +88,34 @@ pub(super) fn process_int_config(
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn process_opt_str_config(
|
||||
value: &mut Value,
|
||||
errors: &mut Vec<ShellError>,
|
||||
config_point: &mut Option<Arc<str>>,
|
||||
) {
|
||||
if value.is_nothing() {
|
||||
*config_point = None;
|
||||
return;
|
||||
}
|
||||
match value.coerce_str() {
|
||||
Ok(s) => *config_point = Some(s.into()),
|
||||
Err(e) => {
|
||||
errors.push(ShellError::GenericError {
|
||||
error: "Error while applying config changes".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(value.span()),
|
||||
help: Some("This value will be ignored.".into()),
|
||||
inner: vec![],
|
||||
});
|
||||
// Reconstruct
|
||||
*value = match config_point {
|
||||
Some(s) => Value::string(s.as_ref(), value.span()),
|
||||
None => Value::nothing(value.span()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn report_invalid_key(keys: &[&str], span: Span, errors: &mut Vec<ShellError>) {
|
||||
// Because Value::Record discards all of the spans of its
|
||||
// column names (by storing them as Strings), the key name cannot be provided
|
||||
|
|
|
@ -8,8 +8,12 @@ use self::table::*;
|
|||
|
||||
use crate::engine::Closure;
|
||||
use crate::{record, ShellError, Span, Value};
|
||||
use serde::Deserializer;
|
||||
use serde::Serializer;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::MAIN_SEPARATOR;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use self::completer::{CompletionAlgorithm, CompletionSort};
|
||||
pub use self::helper::extract_value;
|
||||
|
@ -29,12 +33,63 @@ mod plugin_gc;
|
|||
mod reedline;
|
||||
mod table;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
fn deserialize_config_path<'de, D>(deserializer: D) -> Result<Option<Arc<str>>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s: Option<String> = Deserialize::deserialize(deserializer)?;
|
||||
Ok(s.map(|s| s.into()))
|
||||
}
|
||||
|
||||
fn serialize_config_path<S>(v: &Option<Arc<str>>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match v {
|
||||
None => serializer.serialize_none(),
|
||||
Some(s) => serializer.serialize_str(s),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct HistoryConfig {
|
||||
pub max_size: i64,
|
||||
pub sync_on_enter: bool,
|
||||
pub file_format: HistoryFileFormat,
|
||||
pub isolation: bool,
|
||||
|
||||
// History path. Can be either a directory (ending with a path separator), in which case a
|
||||
// default file name will be appended - or a full file path.
|
||||
//
|
||||
// PathBuf would be more correct, but it causes conversion issues down the line. String is more
|
||||
// strict in terms of characters, but less strict in terms of semantics. We use Arc<str> for
|
||||
// cheap clone.
|
||||
#[serde(
|
||||
serialize_with = "serialize_config_path",
|
||||
deserialize_with = "deserialize_config_path"
|
||||
)]
|
||||
path: Option<Arc<str>>,
|
||||
}
|
||||
|
||||
impl HistoryConfig {
|
||||
#[allow(clippy::question_mark)]
|
||||
pub fn file_path(&self) -> Option<String> {
|
||||
let Some(path) = &self.path else {
|
||||
return None;
|
||||
};
|
||||
if path.ends_with(MAIN_SEPARATOR) {
|
||||
let mut path = path.to_string();
|
||||
path.push_str(match self.file_format {
|
||||
HistoryFileFormat::PlainText => "history.txt",
|
||||
HistoryFileFormat::Sqlite => "history.sqlite3",
|
||||
});
|
||||
Some(path)
|
||||
} else if !path.is_empty() {
|
||||
Some(path.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for HistoryConfig {
|
||||
|
@ -44,6 +99,17 @@ impl Default for HistoryConfig {
|
|||
sync_on_enter: true,
|
||||
file_format: HistoryFileFormat::PlainText,
|
||||
isolation: false,
|
||||
// TODO: This reading an env var is not great. Ideally we'd plumb config location
|
||||
// explicitly, but there's already lots of uses of Config::default and converting them
|
||||
// all is not trivial.
|
||||
path: nu_path::nu_config_dir().and_then(|dir| {
|
||||
let s = dir.into_os_string().into_string();
|
||||
s.map(|mut s| {
|
||||
s.push(MAIN_SEPARATOR);
|
||||
Arc::from(s)
|
||||
})
|
||||
.ok()
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -301,6 +367,9 @@ impl Value {
|
|||
value,
|
||||
&mut errors);
|
||||
}
|
||||
"path" => {
|
||||
process_opt_str_config(value, &mut errors, &mut history.path);
|
||||
}
|
||||
_ => {
|
||||
report_invalid_key(&[key, key2], span, &mut errors);
|
||||
return false;
|
||||
|
|
|
@ -79,6 +79,16 @@ pub enum HistoryFileFormat {
|
|||
PlainText,
|
||||
}
|
||||
|
||||
impl HistoryFileFormat {
|
||||
pub fn default_file_name(self) -> std::path::PathBuf {
|
||||
match self {
|
||||
HistoryFileFormat::PlainText => "history.txt",
|
||||
HistoryFileFormat::Sqlite => "history.sqlite3",
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for HistoryFileFormat {
|
||||
type Err = &'static str;
|
||||
|
||||
|
|
|
@ -762,7 +762,11 @@ impl EngineState {
|
|||
|
||||
/// Returns the configuration settings for command history or `None` if history is disabled
|
||||
pub fn history_config(&self) -> Option<HistoryConfig> {
|
||||
self.history_enabled.then(|| self.config.history)
|
||||
self.history_enabled.then(|| self.config.history.clone())
|
||||
}
|
||||
|
||||
pub fn history_file_path(&self) -> Option<String> {
|
||||
self.config.history.file_path()
|
||||
}
|
||||
|
||||
pub fn get_var(&self, var_id: VarId) -> &Variable {
|
||||
|
|
|
@ -7,37 +7,50 @@ use crate::{
|
|||
debugger::{DebugContext, WithoutDebug},
|
||||
engine::{EngineState, StateWorkingSet},
|
||||
eval_base::Eval,
|
||||
record, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, Value, VarId,
|
||||
record, Config, PipelineData, Record, ShellError, Span, Value, VarId,
|
||||
};
|
||||
use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name};
|
||||
use std::{
|
||||
io,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
/// Create a Value for `$nu`.
|
||||
pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Value {
|
||||
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
|
||||
fn canonicalize_path_fallible(engine_state: &EngineState, path: &Path) -> io::Result<PathBuf> {
|
||||
#[allow(deprecated)]
|
||||
let cwd = engine_state.current_work_dir();
|
||||
|
||||
if path.exists() {
|
||||
match nu_path::canonicalize_with(path, cwd) {
|
||||
Ok(canon_path) => canon_path,
|
||||
Err(_) => path.to_owned(),
|
||||
match nu_path::canonicalize_with(path, &cwd) {
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => {
|
||||
// If the path does not exist, try to canonicalize the parent (directory) alone.
|
||||
// Some tests (and perhaps, users) rely on path to non-existent files being
|
||||
// canonicalized to some extent. This works for the most part due to how those
|
||||
// paths happen to be constructed, but in some cases there is no intermediate
|
||||
// canonicalization and we have to do it here.
|
||||
match (path.parent(), path.file_name()) {
|
||||
(Some(dir), Some(file)) => {
|
||||
let mut path = nu_path::canonicalize_with(dir, cwd)?;
|
||||
path.push(file);
|
||||
Ok(path)
|
||||
}
|
||||
_ => Err(e),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
path.to_owned()
|
||||
Err(e) => Err(e),
|
||||
Ok(p) => Ok(p),
|
||||
}
|
||||
}
|
||||
|
||||
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
|
||||
canonicalize_path_fallible(engine_state, path).unwrap_or_else(|_| path.to_owned())
|
||||
}
|
||||
|
||||
let mut record = Record::new();
|
||||
|
||||
let config_path = match nu_path::config_dir() {
|
||||
Some(mut path) => {
|
||||
path.push("nushell");
|
||||
Ok(canonicalize_path(engine_state, path.as_ref()))
|
||||
}
|
||||
let config_path = match nu_path::nu_config_dir() {
|
||||
Some(path) => Ok(canonicalize_path(engine_state, path.as_ref())),
|
||||
None => Err(Value::error(
|
||||
ShellError::ConfigDirNotFound { span: Some(span) },
|
||||
span,
|
||||
|
@ -88,18 +101,10 @@ pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Valu
|
|||
|
||||
record.push(
|
||||
"history-path",
|
||||
config_path.clone().map_or_else(
|
||||
|e| e,
|
||||
|mut path| {
|
||||
match engine_state.config.history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
path.push("history.sqlite3");
|
||||
}
|
||||
HistoryFileFormat::PlainText => {
|
||||
path.push("history.txt");
|
||||
}
|
||||
}
|
||||
let canon_hist_path = canonicalize_path(engine_state, &path);
|
||||
engine_state.history_file_path().map_or_else(
|
||||
|| Value::error(ShellError::ConfigDirNotFound { span: Some(span) }, span),
|
||||
|path| {
|
||||
let canon_hist_path = canonicalize_path(engine_state, Path::new(&path));
|
||||
Value::string(canon_hist_path.to_string_lossy(), span)
|
||||
},
|
||||
),
|
||||
|
|
|
@ -17,7 +17,6 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
|
||||
pub(crate) const NUSHELL_FOLDER: &str = "nushell";
|
||||
const CONFIG_FILE: &str = "config.nu";
|
||||
const ENV_FILE: &str = "env.nu";
|
||||
const LOGINSHELL_FILE: &str = "login.nu";
|
||||
|
@ -49,9 +48,7 @@ pub(crate) fn read_config_file(
|
|||
report_error(&working_set, &e);
|
||||
}
|
||||
}
|
||||
} else if let Some(mut config_path) = nu_path::config_dir() {
|
||||
config_path.push(NUSHELL_FOLDER);
|
||||
|
||||
} else if let Some(mut config_path) = nu_path::nu_config_dir() {
|
||||
// Create config directory if it does not exist
|
||||
if !config_path.exists() {
|
||||
if let Err(err) = std::fs::create_dir_all(&config_path) {
|
||||
|
@ -134,8 +131,7 @@ pub(crate) fn read_loginshell_file(engine_state: &mut EngineState, stack: &mut S
|
|||
);
|
||||
|
||||
// read and execute loginshell file if exists
|
||||
if let Some(mut config_path) = nu_path::config_dir() {
|
||||
config_path.push(NUSHELL_FOLDER);
|
||||
if let Some(mut config_path) = nu_path::nu_config_dir() {
|
||||
config_path.push(LOGINSHELL_FILE);
|
||||
|
||||
warn!("loginshell_file: {}", config_path.display());
|
||||
|
@ -268,7 +264,7 @@ pub(crate) fn setup_config(
|
|||
);
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
#[cfg(feature = "plugin")]
|
||||
read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER);
|
||||
read_plugin_file(engine_state, plugin_file);
|
||||
|
||||
read_config_file(engine_state, stack, env_file, true);
|
||||
read_config_file(engine_state, stack, config_file, false);
|
||||
|
@ -300,8 +296,7 @@ pub(crate) fn set_config_path(
|
|||
);
|
||||
let config_path = match config_file {
|
||||
Some(s) => canonicalize_with(&s.item, cwd).ok(),
|
||||
None => nu_path::config_dir().map(|mut p| {
|
||||
p.push(NUSHELL_FOLDER);
|
||||
None => nu_path::nu_config_dir().map(|p| {
|
||||
let mut p = canonicalize_with(&p, cwd).unwrap_or(p.into());
|
||||
p.push(default_config_name);
|
||||
canonicalize_with(&p, cwd).unwrap_or(p)
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
#[cfg(feature = "plugin")]
|
||||
use crate::config_files::NUSHELL_FOLDER;
|
||||
use crate::{
|
||||
command,
|
||||
config_files::{self, setup_config},
|
||||
|
@ -37,7 +35,7 @@ pub(crate) fn run_commands(
|
|||
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
|
||||
if parsed_nu_cli_args.no_config_file.is_none() {
|
||||
#[cfg(feature = "plugin")]
|
||||
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
|
||||
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
|
||||
|
||||
perf!("read plugins", start_time, use_color);
|
||||
|
||||
|
@ -125,7 +123,7 @@ pub(crate) fn run_file(
|
|||
if parsed_nu_cli_args.no_config_file.is_none() {
|
||||
let start_time = std::time::Instant::now();
|
||||
#[cfg(feature = "plugin")]
|
||||
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
|
||||
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
|
||||
perf!("read plugins", start_time, use_color);
|
||||
|
||||
let start_time = std::time::Instant::now();
|
||||
|
@ -216,7 +214,6 @@ pub(crate) fn run_repl(
|
|||
let ret_val = evaluate_repl(
|
||||
engine_state,
|
||||
stack,
|
||||
config_files::NUSHELL_FOLDER,
|
||||
parsed_nu_cli_args.execute,
|
||||
parsed_nu_cli_args.no_std_lib,
|
||||
entire_start_time,
|
||||
|
|
|
@ -151,6 +151,7 @@ fn test_default_config_path() {
|
|||
fn test_default_symlinked_config_path_empty() {
|
||||
Playground::setup("symlinked_empty_config_dir", |_, playground| {
|
||||
let config_dir_nushell = setup_fake_config(playground);
|
||||
println!("config_dir_nushell = {}", config_dir_nushell.display());
|
||||
test_config_path_helper(playground, config_dir_nushell);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user