#zsh scripts

1 messages · Page 1 of 1 (latest)

spice sapphire
#

Hello ZSH. this might be a question that shows that I might not yet understand the need for dagger and how to use it:
Let s say that I have a lot of zsh functions that and I would like to be able to use them with dagger:
The objectives are:

  • the functions will run the same on evey machine (cause we use dagger).
  • the functions are shared as "separate code" (ie we want to export that module to the daggerverse)

any idea/suggestion on how to do that ?

#

here is my dagger function in typescript

  @func()
  buildZshContainer(commandToRun: string): Promise<string> {
    return dag
      .container()
      .from("alpine:latest")
      .withExec(["apk", "update"])
      .withExec(["apk", "add", "--no-cache", "zsh", "curl", "git", "bash"])
      .withNewFile(
        "/scripts/custom-functions.zsh",
        `
# Custom Zsh Functions
function hello() {
  echo "Hello, $1!"
}
function detectModifiedMavenModules() {
  echo "maven/parent/subModule1"
}
`
      )
      .withExec(["/bin/zsh", "-c", "source /scripts/custom-functions.zsh; " + commandToRun]).stdout(); // execute the command and return the output
  }

I can call from this module I can call:
dagger call build-zsh-container --commandToRun='hello you' --> that will work
dagger call build-zsh-container --commandToRun=detectModifiedMavenModules--> that will aslo work.

My issue / question is that the functions are "hardCoded" here in the typescript file.
If my "zsh functions" are very complex and are in multiple files,

  • option1 : I could put the scripts on github and have dagger fetch them but I do not like so much this idea as it creates an "external depency" to the buildZshContainer.
  • option2: I could aggregate all my scripts and I hard code them in the typescript.

The option2 is definitely ugly, but in the end I get exactly what I want: a zsh container on which I can call some functions. (the issue is that it will be very ugly in the typescript file and whenever I want to make a change in my script file it will be horrible).

Any idea/suggestion ? (basically I think that I would like to include some zsh script file into a dagger module... is that possible ?)

wind cargo
#

Hello! Yes you can easily "attach" files to your module. See https://docs.dagger.io/api/arguments#directories-and-files

Dagger Functions, just like regular functions, can accept arguments. In addition to basic types (string, boolean, integer, arrays...), Dagger also defines powerful core types which Dagger Functions can use for their arguments, such as Directory, Container, Service, Secret, and many more.

#

in short,
your dagger function can take a directory full of scripts as argument. And that argument can default to for example ./scripts in your module directory (next to dagger.json).

#

However you won't be able to dynamically modify the schema of your module based on the contents of the scripts directory. That would require a custom SDK - basically a ZSH SDK instead of a Typescript, python or Go one. That is possible but considerably more work and probably not worth it for what you're trying to do.

At some point it becomes easier to just rewrite your scripts in one of those other languages instead

#

but your call build-zsh-function --commandToRun examples above can be generalized to any scripts on the fly, in a clean way 👍

spice sapphire
#

@wind cargo first thank you for your anwer. I am sorry but maybe I did not express myself properly or maybe I did not understand your answer:
if I have 2 dagger modules, lets call it shared-dagger-zsh and my-module.
If my-module "depends/import" the shared-dagger-zsh then the function buildZshContainer which is inside the shared-dagger-zsh module, cannot take take a script directory as a parameter... I mean it technically can, but if I do that, where would the zsh scripts be ? they would actually have to be inside my-module right ? (if yes then that is exactly what I do not want... I want to have the scripts actually be inside the shared-dagger-zsh module (which could be hosted on git in the daggerverse).