mirror of
https://github.com/nushell/nushell
synced 2024-12-26 04:53:09 +00:00
Add alias support to scripts and -c (#1593)
This commit is contained in:
parent
bd5836e25d
commit
59d516064c
3 changed files with 74 additions and 48 deletions
|
@ -368,6 +368,54 @@ pub fn create_default_context(
|
||||||
Ok(context)
|
Ok(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn run_vec_of_pipelines(
|
||||||
|
pipelines: Vec<String>,
|
||||||
|
redirect_stdin: bool,
|
||||||
|
) -> Result<(), Box<dyn Error>> {
|
||||||
|
let mut syncer = crate::EnvironmentSyncer::new();
|
||||||
|
let mut context = crate::create_default_context(&mut syncer)?;
|
||||||
|
|
||||||
|
let _ = crate::load_plugins(&mut context);
|
||||||
|
|
||||||
|
let cc = context.ctrl_c.clone();
|
||||||
|
|
||||||
|
ctrlc::set_handler(move || {
|
||||||
|
cc.store(true, Ordering::SeqCst);
|
||||||
|
})
|
||||||
|
.expect("Error setting Ctrl-C handler");
|
||||||
|
|
||||||
|
if context.ctrl_c.load(Ordering::SeqCst) {
|
||||||
|
context.ctrl_c.store(false, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
// before we start up, let's run our startup commands
|
||||||
|
if let Ok(config) = crate::data::config::config(Tag::unknown()) {
|
||||||
|
if let Some(commands) = config.get("startup") {
|
||||||
|
match commands {
|
||||||
|
Value {
|
||||||
|
value: UntaggedValue::Table(pipelines),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
for pipeline in pipelines {
|
||||||
|
if let Ok(pipeline_string) = pipeline.as_string() {
|
||||||
|
let _ =
|
||||||
|
run_pipeline_standalone(pipeline_string, false, &mut context).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("warning: expected a table of pipeline strings as startup commands");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for pipeline in pipelines {
|
||||||
|
run_pipeline_standalone(pipeline, redirect_stdin, &mut context).await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn run_pipeline_standalone(
|
pub async fn run_pipeline_standalone(
|
||||||
pipeline: String,
|
pipeline: String,
|
||||||
redirect_stdin: bool,
|
redirect_stdin: bool,
|
||||||
|
|
|
@ -28,7 +28,9 @@ mod shell;
|
||||||
mod stream;
|
mod stream;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
pub use crate::cli::{cli, create_default_context, load_plugins, run_pipeline_standalone};
|
pub use crate::cli::{
|
||||||
|
cli, create_default_context, load_plugins, run_pipeline_standalone, run_vec_of_pipelines,
|
||||||
|
};
|
||||||
pub use crate::data::dict::TaggedListBuilder;
|
pub use crate::data::dict::TaggedListBuilder;
|
||||||
pub use crate::data::primitive;
|
pub use crate::data::primitive;
|
||||||
pub use crate::data::value;
|
pub use crate::data::value;
|
||||||
|
|
60
src/main.rs
60
src/main.rs
|
@ -3,7 +3,6 @@ use log::LevelFilter;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{prelude::*, BufReader};
|
use std::io::{prelude::*, BufReader};
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let matches = App::new("nushell")
|
let matches = App::new("nushell")
|
||||||
|
@ -89,28 +88,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
match matches.values_of("commands") {
|
match matches.values_of("commands") {
|
||||||
None => {}
|
None => {}
|
||||||
Some(values) => {
|
Some(values) => {
|
||||||
let mut syncer = nu_cli::EnvironmentSyncer::new();
|
let pipelines: Vec<String> = values.map(|x| x.to_string()).collect();
|
||||||
let mut context = nu_cli::create_default_context(&mut syncer)?;
|
futures::executor::block_on(nu_cli::run_vec_of_pipelines(
|
||||||
|
pipelines,
|
||||||
let _ = nu_cli::load_plugins(&mut context);
|
|
||||||
|
|
||||||
let cc = context.ctrl_c.clone();
|
|
||||||
|
|
||||||
ctrlc::set_handler(move || {
|
|
||||||
cc.store(true, Ordering::SeqCst);
|
|
||||||
})
|
|
||||||
.expect("Error setting Ctrl-C handler");
|
|
||||||
|
|
||||||
if context.ctrl_c.load(Ordering::SeqCst) {
|
|
||||||
context.ctrl_c.store(false, Ordering::SeqCst);
|
|
||||||
}
|
|
||||||
for item in values {
|
|
||||||
futures::executor::block_on(nu_cli::run_pipeline_standalone(
|
|
||||||
item.into(),
|
|
||||||
matches.is_present("stdin"),
|
matches.is_present("stdin"),
|
||||||
&mut context,
|
|
||||||
))?;
|
))?;
|
||||||
}
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,31 +101,25 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
Some(script) => {
|
Some(script) => {
|
||||||
let file = File::open(script)?;
|
let file = File::open(script)?;
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut syncer = nu_cli::EnvironmentSyncer::new();
|
let pipelines: Vec<String> = reader
|
||||||
let mut context = nu_cli::create_default_context(&mut syncer)?;
|
.lines()
|
||||||
|
.filter_map(|x| {
|
||||||
let _ = nu_cli::load_plugins(&mut context);
|
if let Ok(x) = x {
|
||||||
|
if !x.starts_with('#') {
|
||||||
let cc = context.ctrl_c.clone();
|
Some(x)
|
||||||
|
} else {
|
||||||
ctrlc::set_handler(move || {
|
None
|
||||||
cc.store(true, Ordering::SeqCst);
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.expect("Error setting Ctrl-C handler");
|
.collect();
|
||||||
|
|
||||||
if context.ctrl_c.load(Ordering::SeqCst) {
|
futures::executor::block_on(nu_cli::run_vec_of_pipelines(
|
||||||
context.ctrl_c.store(false, Ordering::SeqCst);
|
pipelines,
|
||||||
}
|
|
||||||
for line in reader.lines() {
|
|
||||||
let line = line?;
|
|
||||||
if !line.starts_with('#') {
|
|
||||||
futures::executor::block_on(nu_cli::run_pipeline_standalone(
|
|
||||||
line,
|
|
||||||
matches.is_present("stdin"),
|
matches.is_present("stdin"),
|
||||||
&mut context,
|
|
||||||
))?;
|
))?;
|
||||||
}
|
|
||||||
}
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue