Add plugin CLI argument (#6064)

* Add plugin CLI argument

While working on supporting CustomValues in Plugins I stumbled upon the
test utilities defined in [nu-test-support][nu-test-support]
and thought these will come in handy, but they end up being outdated.
They haven't been used or since engine-q's was merged, so they are
currently using the old way engine-q handled plugins, where it would
just look into a specific folder for plugins and call them without
signatures or registration. While fixing that I realized that there is
currently no way to tell nushell to load and save signatures into a
specific path, and so those integration tests could end up potentially
conflicting with each other and with the local plugins the person
running them is using.

So this adds a new CLI argument to specify where to store and load
plugin signatures from

I am not super sure of the way I implemented this, mainly
I was a bit confused about the distinction between
[src/config_files.rs][src/config_files.rs] and
[crates/nu-cli/src/config_files.rs][crates/nu-cli/src/config_files.rs].
Should I be moving the plugin loading function from the `nu-cli` one to
the root one?

[nu-test-support]: 9d0be7d96f/crates/nu-test-support/src/macros.rs (L106)
[src/config_files.rs]: 9d0be7d96f/src/config_files.rs
[crates/nu-cli/src/config_files.rs]: 9d0be7d96f/crates/nu-cli/src/config_files.rs

* Gate new CLI option behind plugin feature

* Rename option to plugin-config
This commit is contained in:
Mathspy 2022-07-17 14:29:19 -04:00 committed by GitHub
parent 9ced5915ff
commit 9aabafeb41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 6 deletions

View File

@ -1,7 +1,13 @@
use crate::util::{eval_source, report_error};
#[cfg(feature = "plugin")]
use log::info;
#[cfg(feature = "plugin")]
use nu_parser::ParseError;
#[cfg(feature = "plugin")]
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
#[cfg(feature = "plugin")]
use nu_protocol::Spanned;
use nu_protocol::{HistoryFileFormat, PipelineData, Span};
use std::path::PathBuf;
@ -15,12 +21,13 @@ const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
pub fn read_plugin_file(
engine_state: &mut EngineState,
stack: &mut Stack,
plugin_file: Option<Spanned<String>>,
storage_path: &str,
is_perf_true: bool,
) {
// Reading signatures from signature file
// The plugin.nu file stores the parsed signature collected from each registered plugin
add_plugin_file(engine_state, storage_path);
add_plugin_file(engine_state, plugin_file, storage_path);
let plugin_path = engine_state.plugin_signatures.clone();
if let Some(plugin_path) = plugin_path {
@ -43,8 +50,23 @@ pub fn read_plugin_file(
}
#[cfg(feature = "plugin")]
pub fn add_plugin_file(engine_state: &mut EngineState, storage_path: &str) {
if let Some(mut plugin_path) = nu_path::config_dir() {
pub fn add_plugin_file(
engine_state: &mut EngineState,
plugin_file: Option<Spanned<String>>,
storage_path: &str,
) {
if let Some(plugin_file) = plugin_file {
let working_set = StateWorkingSet::new(engine_state);
let cwd = working_set.get_cwd();
match canonicalize_with(&plugin_file.item, cwd) {
Ok(path) => engine_state.plugin_signatures = Some(path),
Err(_) => {
let e = ParseError::FileNotFound(plugin_file.item, plugin_file.span);
report_error(&working_set, &e);
}
}
} else if let Some(mut plugin_path) = nu_path::config_dir() {
// Path to store plugins signatures
plugin_path.push(storage_path);
plugin_path.push(PLUGIN_FILE);

View File

@ -103,6 +103,8 @@ fn main() -> Result<()> {
args.next().map(|a| escape_quote_string(&a))
}
"--config" | "--env-config" => args.next().map(|a| escape_quote_string(&a)),
#[cfg(feature = "plugin")]
"--plugin-config" => args.next().map(|a| escape_quote_string(&a)),
"--log-level" | "--testbin" | "--threads" | "-t" => args.next(),
_ => None,
};
@ -204,6 +206,7 @@ fn main() -> Result<()> {
read_plugin_file(
&mut engine_state,
&mut stack,
binary_args.plugin_file,
NUSHELL_FOLDER,
is_perf_true(),
);
@ -256,6 +259,7 @@ fn main() -> Result<()> {
read_plugin_file(
&mut engine_state,
&mut stack,
binary_args.plugin_file,
NUSHELL_FOLDER,
is_perf_true(),
);
@ -304,6 +308,8 @@ fn main() -> Result<()> {
setup_config(
&mut engine_state,
&mut stack,
#[cfg(feature = "plugin")]
binary_args.plugin_file,
binary_args.config_file,
binary_args.env_file,
binary_args.login_shell.is_some(),
@ -329,12 +335,19 @@ fn main() -> Result<()> {
fn setup_config(
engine_state: &mut EngineState,
stack: &mut Stack,
#[cfg(feature = "plugin")] plugin_file: Option<Spanned<String>>,
config_file: Option<Spanned<String>>,
env_file: Option<Spanned<String>>,
is_login_shell: bool,
) {
#[cfg(feature = "plugin")]
read_plugin_file(engine_state, stack, NUSHELL_FOLDER, is_perf_true());
read_plugin_file(
engine_state,
stack,
plugin_file,
NUSHELL_FOLDER,
is_perf_true(),
);
if is_perf_true() {
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
@ -398,6 +411,8 @@ fn parse_commandline_args(
let commands: Option<Expression> = call.get_flag_expr("commands");
let testbin: Option<Expression> = call.get_flag_expr("testbin");
let perf = call.has_flag("perf");
#[cfg(feature = "plugin")]
let plugin_file: Option<Expression> = call.get_flag_expr("plugin-config");
let config_file: Option<Expression> = call.get_flag_expr("config");
let env_file: Option<Expression> = call.get_flag_expr("env-config");
let log_level: Option<Expression> = call.get_flag_expr("log-level");
@ -425,6 +440,8 @@ fn parse_commandline_args(
let commands = extract_contents(commands)?;
let testbin = extract_contents(testbin)?;
#[cfg(feature = "plugin")]
let plugin_file = extract_contents(plugin_file)?;
let config_file = extract_contents(config_file)?;
let env_file = extract_contents(env_file)?;
let log_level = extract_contents(log_level)?;
@ -455,6 +472,8 @@ fn parse_commandline_args(
interactive_shell,
commands,
testbin,
#[cfg(feature = "plugin")]
plugin_file,
config_file,
env_file,
log_level,
@ -478,6 +497,8 @@ struct NushellCliArgs {
interactive_shell: Option<Spanned<String>>,
commands: Option<Spanned<String>>,
testbin: Option<Spanned<String>>,
#[cfg(feature = "plugin")]
plugin_file: Option<Spanned<String>>,
config_file: Option<Spanned<String>>,
env_file: Option<Spanned<String>>,
log_level: Option<Spanned<String>>,
@ -495,7 +516,7 @@ impl Command for Nu {
}
fn signature(&self) -> Signature {
Signature::build("nu")
let signature = Signature::build("nu")
.usage("The nushell language and shell.")
.switch("stdin", "redirect the stdin", None)
.switch("login", "start as a login shell", Some('l'))
@ -558,7 +579,22 @@ impl Command for Nu {
SyntaxShape::String,
"parameters to the script file",
)
.category(Category::System)
.category(Category::System);
#[cfg(feature = "plugin")]
{
signature.named(
"plugin-config",
SyntaxShape::String,
"start with an alternate plugin signature file",
None,
)
}
#[cfg(not(feature = "plugin"))]
{
signature
}
}
fn usage(&self) -> &str {