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

Shell prelude #2079

Open
casey opened this issue May 24, 2024 · 8 comments
Open

Shell prelude #2079

casey opened this issue May 24, 2024 · 8 comments

Comments

@casey
Copy link
Owner

casey commented May 24, 2024

Users often have scripts they'd like to source, alias they'd like to define, or environment variables they'd like to set for all or most shells in a justfile.

One possible way of supporting this would be to define a snippet of shell code which should be prepended to all shell commands. This could be supported with a shell-prelude setting:

set shell-prelude := 'export FOO=bar;'

foo:
  echo $FOO

When running the recipe foo, the line echo $FOO would actually run export FOO=bar; echo $FOO.

One annoying thing is that the shell prelude would be executed before every line of a recipe, so if it were slow, it would significantly slow down execution.

@gl-yziquel
Copy link
Contributor

I agree that would be nice. I'd need a shorthand to call the various recipes I have, and an alias in a shell prelude would do wonder.

As of today, it seems there is no real other way to perform that than to hack away at the default shell invocation. Which brings to the following point: the documentation does not seem to document what the precise invocation of the default shell is.

@casey
Copy link
Owner Author

casey commented Jun 5, 2024

Good point, I documented this in #2129.

@gyreas
Copy link
Contributor

gyreas commented Jul 7, 2024

How would this work, shell injection?

@casey
Copy link
Owner Author

casey commented Jul 7, 2024

It would actually be pretty easy, just prepend the prelude to the command before executing it. So here:

set shell-prelude := 'export FOO=bar;'

foo:
  echo $FOO

foo would actual execute export FOO=bar;echo $FOO.

@gyreas
Copy link
Contributor

gyreas commented Jul 8, 2024

foo would actual execute export FOO=bar;echo $FOO.

I think newline should come after the prelude, in case someone tries to get exotic

@casey
Copy link
Owner Author

casey commented Jul 8, 2024

I actually think it should be up to the user to include whatever terminating whitespace they want. If just automatically prepends some kind of whitespace, either a space or a newline, the user won't have a way to easily override it, in case they don't want it.

@gyreas
Copy link
Contributor

gyreas commented Jul 8, 2024

I think I understand how you intend it to be used. Cool then

@gsemet
Copy link

gsemet commented Sep 17, 2024

I would like to have this prelude, that may differ dependending on the shell used.

I have a bunch of function that i would like to define to more readability in the output

In CI, I have something like:

image

And I do not really see the difference between the command git lfs install -f and its output Updated git hooks. Using the -x bash option does not make the log more readable.

I'd like to have some function always defined in every subshell (colorful echo_and_run function in CI)

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

No branches or pull requests

4 participants