devbox icon indicating copy to clipboard operation
devbox copied to clipboard

[Feature]: support template placeholders in scripts and init hook

Open weilbith opened this issue 1 year ago • 0 comments

Is your feature request related to a problem you're trying to solve? Please describe. I wanna write DevBox scripts or an init hook that works independently from where I open the DevBox shell (environment). So if if I use filesystem paths within them, they just work correctly.

Example

Folder structure:

  • devbox.json
  • foo.file
  • bar/ (folder)

Content of the devbox.json file:

{
  "shell": {
    "init_hook": "rm foo.file"
  }
}

Activating the DevBox shell now from within the root directory (either devbox shell or eval $(devbox shellenv --init-hook) it will properly remove the foo.file file. If I do the same from within the bar directory, it won't and fail with rm: cannot remove 'foo.file': No such file or directory (don't forget to recreate the file if you just deleted it :wink:).

The file deletion is just a very simple and trivial example (though a real one for me). I run especially into this issue for mono-repositories where I often navigate directly into a component directory of this repository and active/use DevBox from there.

Describe the solution you'd like The documentation for writing plugins mentions the support of so called template placeholders. These include for example the variable called DevboxDirRoot among others. Having such functionality already in place, it would be amazing to also support it for shell.init_hook or `shell. That avoids the need to of a complete new feature and would do the job.

The above devbox.json would then look like that:

{
  "shell": {
    "init_hook": "rm {{ .DevboxDirRoot }}/foo.file"
  }
}

Describe alternatives you've considered You tried to use ${BASH_SOURCE[0]} as an anchor of where the script/hook is defined and reverse the path (I mean just use ../../) based on knowledge where DevBox puts these files. First of all, this is not very nice to read and maintain. Second, and most importantly, it simply doesn't work. Using this example devbox.json gives me the error dirname missing operand:

{
  "shell": {
    "init_hook": "echo $(dirname ${BASH_SOURCE[0]})"
  }
}

Additional context None. Should be all in the sections above.

weilbith avatar Jan 11 '24 10:01 weilbith