Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ? guard sigil #2547

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1542,24 +1542,58 @@ exported `just` variables cannot be used. However, this allows shell expanded
strings to be used in places like settings and import paths, which cannot
depend on `just` variables and `.env` files.

### Ignoring Errors
### Sigils

Normally, if a command returns a non-zero exit status, execution will stop. To
continue execution after a command, even if it fails, prefix the command with
`-`:
Commands in linewise recipes may be prefixed with any combination of the sigils
`-`, `@`, and `?`.

The `@` sigil toggles command echoing:

```just
foo:
-cat foo
echo 'Done!'
@echo "This line won't be echoed!"
echo "This line will be echoed!"

@bar:
@echo "This line will be echoed!"
echo "This line won't be echoed!"
```

The `-` sigil cause recipe execution to continue even if the command returns a
nonzero exit status:

```just
# execution will continue, even if bar doesn't exist
foo:
-rmdir bar
mkdir bar
echo 'so much good stuff' > bar/stuff.txt
```

The `?` sigil<sup>master</sup> causes the current recipe to stop executing if
the command returns a nonzero exit status, but execution of other recipes will
continue.

If the `guards` settings is unset or false, `?` sigils are ignored.

```just
set guards

@foo: bar
echo FOO

@bar:
?[[ -f baz ]]
echo BAR
```

```console
$ just foo
cat foo
cat: foo: No such file or directory
echo 'Done!'
Done!
FOO
$ touch baz
$ just foo
BAR
FOO
```

### Functions
Expand Down
1 change: 1 addition & 0 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) enum Keyword {
Export,
Fallback,
False,
Guards,
If,
IgnoreComments,
Import,
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl<'src> Lexer<'src> {

/// True if `text` could be an identifier
pub(crate) fn is_identifier(text: &str) -> bool {
if !text.chars().next().map_or(false, Self::is_identifier_start) {
if !text.chars().next().is_some_and(Self::is_identifier_start) {
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub(crate) use {
settings::Settings,
shebang::Shebang,
show_whitespace::ShowWhitespace,
sigil::Sigil,
source::Source,
string_delimiter::StringDelimiter,
string_kind::StringKind,
Expand Down Expand Up @@ -255,6 +256,7 @@ mod setting;
mod settings;
mod shebang;
mod show_whitespace;
mod sigil;
mod source;
mod string_delimiter;
mod string_kind;
Expand Down
33 changes: 21 additions & 12 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ impl Line<'_> {
}
}

pub(crate) fn sigils(&self, settings: &Settings) -> BTreeSet<Sigil> {
let mut sigils = BTreeSet::new();

if let Some(first) = self.first() {
for c in first.chars() {
let sigil = match c {
'-' => Sigil::Infallible,
'?' if settings.guards => Sigil::Guard,
'@' => Sigil::Quiet,
_ => break,
};

if !sigils.insert(sigil) {
break;
}
}
}

sigils
}

pub(crate) fn is_comment(&self) -> bool {
self.first().is_some_and(|text| text.starts_with('#'))
}
Expand All @@ -33,18 +54,6 @@ impl Line<'_> {
self.fragments.is_empty()
}

pub(crate) fn is_infallible(&self) -> bool {
self
.first()
.is_some_and(|text| text.starts_with('-') || text.starts_with("@-"))
}

pub(crate) fn is_quiet(&self) -> bool {
self
.first()
.is_some_and(|text| text.starts_with('@') || text.starts_with("-@"))
}

pub(crate) fn is_shebang(&self) -> bool {
self.first().is_some_and(|text| text.starts_with("#!"))
}
Expand Down
1 change: 1 addition & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ impl<'src> Node<'src> for Set<'src> {
| Setting::DotenvRequired(value)
| Setting::Export(value)
| Setting::Fallback(value)
| Setting::Guards(value)
| Setting::PositionalArguments(value)
| Setting::Quiet(value)
| Setting::Unstable(value)
Expand Down
5 changes: 3 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ impl<'run, 'src> Parser<'run, 'src> {

let body = self.parse_body()?;

let shebang = body.first().map_or(false, Line::is_shebang);
let shebang = body.first().is_some_and(Line::is_shebang);
let script = attributes.contains(AttributeDiscriminant::Script);

if shebang && script {
Expand Down Expand Up @@ -1016,7 +1016,7 @@ impl<'run, 'src> Parser<'run, 'src> {
}
}

while lines.last().map_or(false, Line::is_empty) {
while lines.last().is_some_and(Line::is_empty) {
lines.pop();
}

Expand Down Expand Up @@ -1067,6 +1067,7 @@ impl<'run, 'src> Parser<'run, 'src> {
Keyword::DotenvRequired => Some(Setting::DotenvRequired(self.parse_set_bool()?)),
Keyword::Export => Some(Setting::Export(self.parse_set_bool()?)),
Keyword::Fallback => Some(Setting::Fallback(self.parse_set_bool()?)),
Keyword::Guards => Some(Setting::Guards(self.parse_set_bool()?)),
Keyword::IgnoreComments => Some(Setting::IgnoreComments(self.parse_set_bool()?)),
Keyword::PositionalArguments => Some(Setting::PositionalArguments(self.parse_set_bool()?)),
Keyword::Quiet => Some(Setting::Quiet(self.parse_set_bool()?)),
Expand Down
37 changes: 19 additions & 18 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,19 @@ impl<'src, D> Recipe<'src, D> {
mut evaluator: Evaluator<'src, 'run>,
) -> RunResult<'src, ()> {
let config = &context.config;
let settings = &context.module.settings;

let mut lines = self.body.iter().peekable();
let mut line_number = self.line_number() + 1;
loop {
if lines.peek().is_none() {
let Some(line) = lines.peek() else {
return Ok(());
}
};

let mut evaluated = String::new();
let mut continued = false;
let quiet_line = lines.peek().map_or(false, |line| line.is_quiet());
let infallible_line = lines.peek().map_or(false, |line| line.is_infallible());

let comment_line = context.module.settings.ignore_comments
&& lines.peek().map_or(false, |line| line.is_comment());
let comment_line = settings.ignore_comments && line.is_comment();
let sigils = line.sigils(settings);

loop {
if lines.peek().is_none() {
Expand All @@ -233,17 +232,15 @@ impl<'src, D> Recipe<'src, D> {

let mut command = evaluated.as_str();

let sigils = usize::from(infallible_line) + usize::from(quiet_line);

command = &command[sigils..];
command = &command[sigils.len()..];

if command.is_empty() {
continue;
}

if config.dry_run
|| config.verbosity.loquacious()
|| !((quiet_line ^ self.quiet)
|| !((sigils.contains(&Sigil::Quiet) ^ self.quiet)
|| (context.module.settings.quiet && !self.no_quiet())
|| config.verbosity.quiet())
{
Expand Down Expand Up @@ -299,13 +296,17 @@ impl<'src, D> Recipe<'src, D> {
match InterruptHandler::guard(|| cmd.status()) {
Ok(exit_status) => {
if let Some(code) = exit_status.code() {
if code != 0 && !infallible_line {
return Err(Error::Code {
recipe: self.name(),
line_number: Some(line_number),
code,
print_message: self.print_exit_message(),
});
if code != 0 {
if sigils.contains(&Sigil::Guard) {
return Ok(());
} else if !sigils.contains(&Sigil::Infallible) {
return Err(Error::Code {
recipe: self.name(),
line_number: Some(line_number),
code,
print_message: self.print_exit_message(),
});
}
}
} else {
return Err(error_from_signal(
Expand Down
2 changes: 2 additions & 0 deletions src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) enum Setting<'src> {
DotenvRequired(bool),
Export(bool),
Fallback(bool),
Guards(bool),
IgnoreComments(bool),
PositionalArguments(bool),
Quiet(bool),
Expand All @@ -31,6 +32,7 @@ impl Display for Setting<'_> {
| Self::DotenvRequired(value)
| Self::Export(value)
| Self::Fallback(value)
| Self::Guards(value)
| Self::IgnoreComments(value)
| Self::PositionalArguments(value)
| Self::Quiet(value)
Expand Down
4 changes: 4 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) struct Settings<'src> {
pub(crate) dotenv_required: bool,
pub(crate) export: bool,
pub(crate) fallback: bool,
pub(crate) guards: bool,
pub(crate) ignore_comments: bool,
pub(crate) positional_arguments: bool,
pub(crate) quiet: bool,
Expand Down Expand Up @@ -58,6 +59,9 @@ impl<'src> Settings<'src> {
Setting::Fallback(fallback) => {
settings.fallback = fallback;
}
Setting::Guards(guards) => {
settings.guards = guards;
}
Setting::IgnoreComments(ignore_comments) => {
settings.ignore_comments = ignore_comments;
}
Expand Down
6 changes: 6 additions & 0 deletions src/sigil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(Eq, Ord, PartialEq, PartialOrd)]
pub(crate) enum Sigil {
Guard,
Infallible,
Quiet,
}
32 changes: 32 additions & 0 deletions tests/guards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::*;

#[test]
fn guard_lines_halt_executation() {
Test::new()
.justfile(
"
set guards

@foo:
?[[ 'foo' == 'bar' ]]
echo baz
",
)
.run();
}

#[test]
fn guard_lines_have_no_effect_if_successful() {
Test::new()
.justfile(
"
set guards

@foo:
?[[ 'foo' == 'foo' ]]
echo baz
",
)
.stdout("baz\n")
.run();
}
1 change: 1 addition & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct Settings<'a> {
dotenv_required: bool,
export: bool,
fallback: bool,
guards: bool,
ignore_comments: bool,
positional_arguments: bool,
quiet: bool,
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod functions;
#[cfg(unix)]
mod global;
mod groups;
mod guards;
mod ignore_comments;
mod imports;
mod init;
Expand Down
Loading