Cross shell env vars
I'm trying to run a cross-shell justfile (bash and powershell). I've omitted the bash portions for brevity. I have a .env file with postgres vars set.
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
set shell := ["bash", "-c"]
my-script:
PGPASSWORD=$PG_PW psql -U $PG_USER -d $PG_DB -h $PG_HOST -p $PG_PORT -f ./my_file.sql
In the bash version, psql connects. For powershell, it throws an error like 'The term 'PGPASSWORD=$PG_PW' is not recognized as the name of a cmdlet', leading me to believe the variable isn't being passed in correctly.
I know you don't access env vars with just a $ in powershell , but I thought that maybe these were somehow Just variables or something, since the script works in bash without brackets/quotes. Is there a cross-platform way to dump in values from .env? or should i make a separate 'myscript' for each OS?
I was able to fix this by using the "{{var})" function. no idea if this is the correct/intended way to use vars cross-shell, but works for me for now so documenting here for others. feel free to close if this is the correct way to achieve this.
set shell := ["bash", "-c"]
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
USER:= env('PG_USER')
PORT:= env('PG_PORT')
DB:= env('PG_DB')
HOST:= env('PG_HOST')
export PGPASSWORD := env('PG_PW')
default:
@just --choose
my-script:
psql -U "{{USER}}" -d "{{DB}}" -h "{{HOST}}" -p "{{PORT}}" -f ./my_file.sql
I think this is probably the best way to do it. The alternative is to write two recipes, one with [windows] and the other with [unix].