diff --git a/src/main.rs b/src/main.rs index 2901d9e24..464ca689b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use clap::{App, Arg}; use log::LevelFilter; use std::error::Error; +use std::fs::File; +use std::io::{prelude::*, BufReader}; fn main() -> Result<(), Box> { let matches = App::new("nushell") @@ -38,6 +40,11 @@ fn main() -> Result<(), Box> { .multiple(false) .takes_value(false), ) + .arg( + Arg::with_name("script") + .help("the nu script to run") + .index(1), + ) .get_matches(); let loglevel = match matches.value_of("loglevel") { @@ -76,6 +83,8 @@ fn main() -> Result<(), Box> { } } + builder.try_init()?; + match matches.values_of("commands") { None => {} Some(values) => { @@ -89,12 +98,30 @@ fn main() -> Result<(), Box> { } } - builder.try_init()?; + match matches.value_of("script") { + Some(script) => { + let file = File::open(script)?; + let reader = BufReader::new(file); + for line in reader.lines() { + let line = line?; + if !line.starts_with('#') { + futures::executor::block_on(nu::run_pipeline_standalone( + line, + matches.is_present("stdin"), + ))?; + } + } + return Ok(()); + } + + None => { + println!( + "Welcome to Nushell {} (type 'help' for more info)", + clap::crate_version!() + ); + futures::executor::block_on(nu::cli())?; + } + } - println!( - "Welcome to Nushell {} (type 'help' for more info)", - clap::crate_version!() - ); - futures::executor::block_on(nu::cli())?; Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index c0c247f0c..0e6a3f13b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -320,6 +320,10 @@ mod tests { loc: fixtures().join("sample_data.xlsx"), at: 0 }, + Res { + loc: fixtures().join("script.nu"), + at: 0 + }, Res { loc: fixtures().join("sgml_description.json"), at: 0 diff --git a/tests/fixtures/formats/script.nu b/tests/fixtures/formats/script.nu new file mode 100755 index 000000000..b79dfdcbb --- /dev/null +++ b/tests/fixtures/formats/script.nu @@ -0,0 +1,2 @@ +#!/Users/gedge/src/github.com/nushell/nushell/target/debug/nu +echo "done" diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index f26b3fe2c..05cbdbc66 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -122,6 +122,19 @@ mod nu_commands { } } +mod nu_script { + use super::nu; + + #[test] + fn run_nu_script() { + let actual = nu!(cwd: "tests/fixtures/formats", r#" + nu script.nu + "#); + + assert_eq!(actual, "done"); + } +} + mod tilde_expansion { use super::nu;