Skip to content

Ability to run a script from stdin #126

Description

@wiktor-k

Hi,

I just found rust-script and it's super-great! ❤️

I'm using it to generate a small Rust script and then run it like that:

sed 's/PKG/{{ pkg }}/g;s/KIND/{{ kind }}/g' > .script.rs <<< '{{ render-script }}'
chmod +x .script.rs
./.script.rs {{ output }}
rm --force .script.rs

And I thought it would be convenient if I could just pipe the script directly to rust-script without saving it to file, something like that:

sed 's/PKG/{{ pkg }}/g;s/KIND/{{ kind }}/g' | rust-script --some-kind-of-option <<< '{{ render-script }}'

I've seen that there's a --loop option but sadly this is for writing filters not entire scripts.

Would something like this be in-scope for this project?

Thanks! 👋

Activity

  1. Gjum commented on May 30, 2024

    @Gjum

    Does process substitution do what you need?

    $ rust-script <(echo 'println!("Hello '"$USER"'!");')
    Hello gjum!
  2. wiktor-k commented on Jun 3, 2024

    @wiktor-k
    Author

    🤔 Hm... maybe I'm holding it wrong or the inner shell is messing up the outer one but this fails:

    rust-script <( sed 's/PKG/ssh-openpgp-auth/g;s/KIND/shell_completions/g' <<< '//! ```cargo
    //! [dependencies]
    //! pkg = { path = "PKG", package = "PKG" }
    //! clap_allgen = "0.1.0"
    //! ```
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        clap_allgen::render_KIND::<pkg::Commands>(
            &std::env::args().collect::<Vec<_>>()[1],
        )?;
        Ok(())
    }
    ' ) '/tmp/xxz'

    The error is:

    error: failed to get `ssh-openpgp-auth` as a dependency of package `_63_d589d09bfb5f45ea9b4b85e0 v0.1.0 (/home/wiktor/.cache/rust-script/projects/d589d09bfb5f45ea9b4b85e0)`
    
    Caused by:
      failed to load source for dependency `ssh-openpgp-auth`
    
    Caused by:
      Unable to update /dev/fd/ssh-openpgp-auth
    
    Caused by:
      failed to read `/dev/fd/ssh-openpgp-auth/Cargo.toml`
    
    Caused by:
      No such file or directory (os error 2)
    error: Could not execute cargo
    

    The following works:

    sed 's/PKG/ssh-openpgp-auth/g;s/KIND/shell_completions/g' > .script.rs <<< '//! ```cargo
    //! [dependencies]
    //! pkg = { path = "PKG", package = "PKG" }
    //! clap_allgen = "0.1.0"
    //! ```
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        clap_allgen::render_KIND::<pkg::Commands>(
            &std::env::args().collect::<Vec<_>>()[1],
        )?;
        Ok(())
    }
    '
    rust-script .script.rs '/tmp/xxz'
    rm --force .script.rs

    Just for the record I'm modifying this file.

    Thanks for your time! 👋

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions