Skip to content

Commit

Permalink
feat: #18 ✨ Add new options for commands and to enter shell
Browse files Browse the repository at this point in the history
  • Loading branch information
ddanier committed May 20, 2024
1 parent 96a86c6 commit 0a2f763
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
53 changes: 42 additions & 11 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use crate::commands::Nur;
use crate::errors::{NurError, NurResult};
use crate::names::NUR_NAME;
use nu_engine::{get_full_help, CallExt};
use nu_parser::escape_for_script_arg;
use nu_parser::parse;
use nu_protocol::report_error;
use nu_parser::{escape_for_script_arg, escape_quote_string};
use nu_protocol::ast::Expression;
use nu_protocol::{
ast::Expr,
engine::{Command, EngineState, Stack, StateWorkingSet},
ShellError,
};
use nu_protocol::{report_error, Spanned};
use nu_utils::stdout_write_all_and_flush;

pub(crate) fn is_safe_taskname(name: &str) -> bool {
// This is basically similar to string_should_be_quoted
// in nushell/crates/nu-parser/src/deparse.rs:1,
// BUT may change as the requirements are different.
// Also I added "#" and "^", as seem in
// Also I added "#" and "^", as seen in
// nushell/crates/nu-parser/src/parse_keywords.rs:175
!name.starts_with('$')
&& !(name.chars().any(|c| {
Expand Down Expand Up @@ -55,16 +56,17 @@ pub(crate) fn gather_commandline_args(
break;
}

// let flag_value = match arg.as_ref() {
// // "--some-file" => args.next().map(|a| escape_quote_string(&a)),
// _ => None,
// };
let flag_value = match arg.as_ref() {
// "--some-file" => args.next().map(|a| escape_quote_string(&a)),
"--commands" | "-c" => args_iter.next().map(|a| escape_quote_string(a)),
_ => None,
};

args_to_nur.push(arg.clone());

// if let Some(flag_value) = flag_value {
// args_to_nur.push(flag_value);
// }
if let Some(flag_value) = flag_value {
args_to_nur.push(flag_value);
}
}

if has_task_call {
Expand Down Expand Up @@ -105,11 +107,13 @@ pub(crate) fn parse_commandline_args(
// We should have a successful parse now
if let Some(pipeline) = block.pipelines.first() {
if let Some(Expr::Call(call)) = pipeline.elements.first().map(|e| &e.expr.expr) {
// let config_file = call.get_flag_expr("config");
// let config_file = call.get_flag_expr("some-flag");
let list_tasks = call.has_flag(engine_state, &mut stack, "list")?;
let quiet_execution = call.has_flag(engine_state, &mut stack, "quiet")?;
let attach_stdin = call.has_flag(engine_state, &mut stack, "stdin")?;
let show_help = call.has_flag(engine_state, &mut stack, "help")?;
let run_commands = call.get_flag_expr("commands");
let enter_shell = call.has_flag(engine_state, &mut stack, "enter-shell")?;
#[cfg(feature = "debug")]
let debug_output = call.has_flag(engine_state, &mut stack, "debug")?;

Expand All @@ -122,11 +126,36 @@ pub(crate) fn parse_commandline_args(
std::process::exit(0);
}

fn extract_contents(
expression: Option<&Expression>,
) -> Result<Option<Spanned<String>>, ShellError> {
if let Some(expr) = expression {
let str = expr.as_string();
if let Some(str) = str {
Ok(Some(Spanned {
item: str,
span: expr.span,
}))
} else {
Err(ShellError::TypeMismatch {
err_message: "string".into(),
span: expr.span,
})
}
} else {
Ok(None)
}
}

let run_commands = extract_contents(run_commands)?;

return Ok(NurArgs {
list_tasks,
quiet_execution,
attach_stdin,
show_help,
run_commands,
enter_shell,
#[cfg(feature = "debug")]
debug_output,
});
Expand All @@ -151,6 +180,8 @@ pub(crate) struct NurArgs {
pub(crate) quiet_execution: bool,
pub(crate) attach_stdin: bool,
pub(crate) show_help: bool,
pub(crate) run_commands: Option<Spanned<String>>,
pub(crate) enter_shell: bool,
#[cfg(feature = "debug")]
pub(crate) debug_output: bool,
}
Expand Down
17 changes: 14 additions & 3 deletions src/commands/nur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ impl Command for Nur {

signature = signature
.usage("nur - a taskrunner based on nu shell.")
.switch("version", "Output version number and exit", None)
.switch("list", "List available tasks and then just exit", None)
.switch("version", "Output version number and exit", Some('v'))
.switch("list", "List available tasks and then just exit", Some('l'))
.switch(
"quiet",
"Do not output anything but what the task produces",
None,
)
.switch("stdin", "Attach stdin to called nur task", None)
.named(
"commands",
SyntaxShape::String,
"run the given commands and then exit",
Some('c'),
)
.switch(
"enter-shell",
"enter nu shell with nur being setup (use this for debugging)",
Some('e'),
)
.optional(
"task name",
SyntaxShape::String,
Expand All @@ -40,7 +51,7 @@ impl Command for Nur {

#[cfg(feature = "debug")]
{
signature = signature.switch("debug", "Show debug details", None);
signature = signature.switch("debug", "Show debug details", Some('d'));
}

signature
Expand Down

0 comments on commit 0a2f763

Please sign in to comment.