Skip to content

Add --redirect-combine option to run-external - #8918

Merged
fdncred merged 1 commit into
nushell:mainfrom
pingiun:redirect-combine
Apr 28, 2023
Merged

fdncred merged 1 commit into
nushell:mainfrom
pingiun:redirect-combine

Conversation

@pingiun

@pingiun pingiun commented Apr 18, 2023

Copy link
Copy Markdown
Contributor

Description

Add option that combines both output streams to the run-external command.

This allows you to do something like this:

let res = do -i { run-external --redirect-combine <command that prints to stdout and stderr> } | complete

if $res.exit_code != 0 {
  # Only print output when command has failed.
  print "The command has failed, these are the logs:"
  print $res.stdout
}

User-Facing Changes

No breaking changes, just an extra option.

Tests + Formatting

Added a test that checks the new option

After Submitting

@pingiun
pingiun force-pushed the redirect-combine branch 3 times, most recently from a945f99 to 4484ace Compare April 18, 2023 10:56
@codecov

codecov Bot commented Apr 18, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #8918 (1d1e322) into main (06996d8) will decrease coverage by 0.37%.
The diff coverage is 91.30%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8918      +/-   ##
==========================================
- Coverage   68.53%   68.17%   -0.37%     
==========================================
  Files         636      636              
  Lines      101713   101739      +26     
==========================================
- Hits        69711    69361     -350     
- Misses      32002    32378     +376     
Impacted Files Coverage Δ
...tes/nu-cli/src/completions/variable_completions.rs 59.81% <0.00%> (+1.89%) ⬆️
crates/nu-command/src/env/config/utils.rs 0.00% <0.00%> (ø)
crates/nu-command/src/system/exec.rs 41.33% <0.00%> (-1.14%) ⬇️
crates/nu-command/src/system/run_external.rs 79.86% <84.00%> (-0.23%) ⬇️
crates/nu-parser/src/parse_keywords.rs 76.25% <90.76%> (-0.11%) ⬇️
crates/nu-protocol/src/engine/engine_state.rs 85.88% <97.14%> (+0.18%) ⬆️
crates/nu-parser/src/parser.rs 85.45% <97.46%> (+0.04%) ⬆️
crates/nu-protocol/src/ast/block.rs 65.21% <100.00%> (-18.12%) ⬇️
crates/nu-protocol/src/engine/overlay.rs 90.24% <100.00%> (+0.30%) ⬆️

... and 3 files with indirect coverage changes

@amtoine amtoine left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've tried it the following python script

#!/usr/bin/env python

import sys

print("foo")
print("bar", file=sys.stderr)
print("baz")

sys.exit(1)

and i get

> let res = (do -i { run-external --redirect-combine ./foo.py } | complete)
>
> if $res.exit_code != 0 {
>   # Only print output when command has failed.
>   print "The command has failed, these are the logs:"
>   print $res.stdout
> }
The command has failed, these are the logs:
bar
foo
baz

which is expected with the way python does things as i understood?

TL;DR: looks it works 👍

⚠️ i'm not sure about the --redirect-combine option, i kinda liked better the --redirect-both you talked about in the discord channel 😋

and i'd like to wait for the point of view of others from the core team about

  • the new dependency on os_pipe
  • the rest of the implementation
    😌

thanks @pingiun for these first effort 👌 😋

@pingiun

pingiun commented Apr 18, 2023

Copy link
Copy Markdown
Contributor Author

Btw the python file printing in that order is due to buffering of the stdout. It can be remedied by adjusting it as follows:

#!/usr/bin/env python

import sys

print("foo")
sys.stdout.flush()
print("bar", file=sys.stderr)
print("baz")

sys.exit(1)

@amtoine

amtoine commented Apr 18, 2023

Copy link
Copy Markdown
Member

@pingiun absolutely, nice 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants