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

for-each with Just? #1570

Open
IngwiePhoenix opened this issue Mar 28, 2023 · 16 comments
Open

for-each with Just? #1570

IngwiePhoenix opened this issue Mar 28, 2023 · 16 comments

Comments

@IngwiePhoenix
Copy link

Hello!

I would like to automate the management of my docker-compose environments for self-hosted services to some degree, and a Justfile seems like a very clean solution. However, I couldn't find out how to dynamically create tasks.

Consider an array of ["jellyfin", "tubesync", "tvheadend"]. I would like to be able to generate apropriate tasks for -down, -pull, and -start.

Is this possible?

Thanks!

@Dowwie
Copy link

Dowwie commented Mar 28, 2023

I second this. How can I iterate over a list of values? In my case, I want to bind values to parameters interpolated within commands.

@cce-dsantos
Copy link

Hello!

I would like to automate the management of my docker-compose environments for self-hosted services to some degree, and a Justfile seems like a very clean solution. However, I couldn't find out how to dynamically create tasks.

Consider an array of ["jellyfin", "tubesync", "tvheadend"]. I would like to be able to generate apropriate tasks for -down, -pull, and -start.

Is this possible?

Thanks!

do task arguments suit your needs?
like having a parameter to target the service and another one targeting action

@cce-dsantos
Copy link

I second this. How can I iterate over a list of values? In my case, I want to bind values to parameters interpolated within commands.

there is an example on readme using bash for loop, have you tried this one?

@Dowwie
Copy link

Dowwie commented Apr 11, 2023

The bash for loop doesn't seem applicable. Here's an example. Given a collection ["one", "two", "three"], how would we iterate over its elements, using each element within string interpolated commands executed by just?

@runeimp
Copy link

runeimp commented Apr 12, 2023

Example:

recipe context +collection:
	#!/bin/sh
	if [ '{{collection}}' != '' ]; then
		for item in {{collection}}; do
			just dynamic-{{context}} $item
		done
	else
		echo "No collection to process with dynamic-{{context}}"
	fi

or something like that? That's how I tend to do it. Unless the recipe may end up on a Windows system in which case I usually use PowerShell instead of a POSIX compliant shell for both platforms.

@IngwiePhoenix
Copy link
Author

I ended up doing this:

update-dc service="all":
  #!/usr/bin/env node
  let srv = "{{service}}"
  let services = [
    "anime",
    "homebox",
    "hass",
    "tubesync",
    "photoprism",
    "tvheadend",
    "jellyfin",
    "jdownloader",
    "notes"
  ]
  console.log("==> Service: "+srv)
  function update_one(s) {
    console.log("Updating "+s+"...")
  }
  if(srv == "all") {
    services.forEach(v => update_one(v))
  } else if(services.indexOf(srv) >= 0) {
    update_one(srv)
  } else {
    console.error("Unknown service: "+srv)
    process.exit(1)
  }

It's not fully implemented, as you can see, but it got me somewhat what I need. But it also is not very elegant... ^^'

@runeimp
Copy link

runeimp commented Apr 13, 2023

The plan is to eventually have Python built into Just so that it has a multi-platform (Windows and everything else) scripting language. But until then using an external language is usually best. I'm on Mac but often need to support Windows so I installed PowerShell Core on my Mac and utilize whatever version of PowerShell is installed on the Windows system to avoid installing anything on target systems beyond the Just executable.

@syphar
Copy link

syphar commented Apr 13, 2023

The plan is to eventually have Python built into Just so that it has a multi-platform (Windows and everything else) scripting language. But until then using an external language is usually best. I'm on Mac but often need to support Windows so I installed PowerShell Core on my Mac and utilize whatever version of PowerShell is installed on the Windows system to avoid installing anything on target systems beyond the Just executable.

integrating a scripting-language sounds awesome, though I believe lua is the better choice for that, having worked on integrating both.

Is there already an issue about that with some design decisions?

Is it actually worth investing time, seeing other PRs don't get reviews? ( I fully understand lack of time on maintainer side, which is totally OK, I only want to prevent wasting time on something that won't get reviewed )

@runeimp
Copy link

runeimp commented Apr 13, 2023

It's a good idea to check in with @casey first before jumping on anything that isn't OK just for your own edification. If you want to try anything Python is definitely the preferred language. See #537 for some discussion on the matter.

@fzyzcjy
Copy link

fzyzcjy commented Nov 26, 2023

Hi, is there any updates? Iterating through a list is quite useful!

@runeimp
Copy link

runeimp commented Dec 13, 2023

Hey @casey, they claim it's not production ready but RustPython is in use by Ruff the Python linter. Maybe good enough now for most recipe scripting?

@casey
Copy link
Owner

casey commented Dec 14, 2023

I'd be a little cautious if it isn't production ready. We could land something as unstable, but if it was incompatible with python in unexpected ways, it might be frustrating.

@runeimp
Copy link

runeimp commented Dec 14, 2023

Personally I only care about the basics, as long it can accomplish the equivalent of Bourne shell with arrays or lists in a cross-platform way I'm all good. It would be wonderful if it was 100% Python 3.7+ compatible with no surprises but that's a minor benefit (to me) over just covering the shell basics. I've not investigated at all yet. Though I'm happy to investigate when a get some free time.

That or just bake in Nushell which is also Rust based and there is embed-nu - Crates.io. Anything to get cross platform basic shell scripting would be a godsend. 👼

@casey
Copy link
Owner

casey commented Dec 15, 2023

I think that exploring what Python support would look like is probably reasonable. It might not be ready to stabilize, but it would be interesting to see how it works. I'm worried that it'll drive compile times and binary size through the roof, but we can make it an optional feature.

@casey
Copy link
Owner

casey commented Dec 15, 2023

Another option is busybox. It's not pure rust, but it does come as a library, and I know busybox is very mature and widely used, so it could be a good option.

@casey
Copy link
Owner

casey commented Nov 8, 2024

See #2458, which floats the possibility of making all values lists of strings, which would easily allow a non-error-prone for loop.

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

7 participants