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 function to check if a command is in path #2489

Closed
bew opened this issue Nov 30, 2024 · 4 comments
Closed

Add function to check if a command is in path #2489

bew opened this issue Nov 30, 2024 · 4 comments

Comments

@bew
Copy link
Contributor

bew commented Nov 30, 2024

Here is my usecase:

Let's say just is installed at /not/in/path/just.

I want my receipes to call other receipe and not care whether just is in path or not, so I use:

foo:
  {{ just_executable() }} bar

bar:
  echo foobar

I want to show the executed line in foo (so I don't prepend a @ to {{ ...).
So calling /not/in/path/just foo would output something like:

/not/in/path/just bar
foobar

👉 BUT when just is in actually in $PATH I'd like instead to show:

just bar
foobar

I was thinking about making a just variable at the top that checks whether just in in $PATH and put just if it is, or just_executable() otherwise.

Hence why I'd like a way to check if just is in $PATH.

Or is there another way to do this?

@laniakea64
Copy link
Contributor

The generic functionality you're requesting sounds covered by #2109

For your specific case, would something like this work? -

just := if replace(env("PATH"), parent_directory(just_executable()) + ':', '') != env("PATH") {
  file_name(just_executable())
} else {
  just_executable()
}

foo:
  {{quote(just)}} bar

bar:
  echo foobar

@bew
Copy link
Contributor Author

bew commented Dec 1, 2024

The generic functionality you're requesting sounds covered by #2109

Ah yes good find, missed it when I searched 👍

For your specific case, would something like this work?

For this example yes, but not for my usecase: I'm using Nix, so binary install path is symlinked a bunch of times to different places and one of those is actually in PATH (not the parent dir of just executable)


Thanks I'll close this and follow progress on #2109

@bew bew closed this as completed Dec 1, 2024
@casey
Copy link
Owner

casey commented Dec 1, 2024

You can also do:

just := if `which just || echo ''` == '' {
  just_executable()
} else {
  'just'
}

We have unstable && and || syntax, with that you could do:

just := `which just || true` && 'just' || just_executable()

Which is shorter and more inscrutable. But I do like inscrutable things.

@bew
Copy link
Contributor Author

bew commented Dec 2, 2024

Thanks 👍
Used which just 2>/dev/null tho to avoid useless 'just not found' line ;)

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

No branches or pull requests

3 participants