casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
21.43k stars 476 forks source link

Conditionals inside recipe body based on arguments and preview when confirm attribute is set #1835

Open NewtonChutney opened 10 months ago

NewtonChutney commented 10 months ago

I couldn't find duplicate issues until 6 pages deep..

I work in airflow repos and the folder structure is as follows:

dags
... templates
... utils
... other_files.py

plugins
... utils
... other_files.py

I wanted to make a simple just recipe for scp-ing each of those folders to a particular ssh target, given their names as arguments.

I wish the following syntax works:

# justfile

scp folder target:
    If $folder == "plugins" {
        scp -r ./plugins {{ target }}:./dev
    }
    else {
        scp -r ./dags/{{ folder }} {{ target }}:./dev/dags
    }

Doing the same in bash like such gives a lengthy incomprehensible output:

    if [ "{{ folder }}" == "plugins" ]; then \
        scp -r ./plugins {{ target }}:./dev \
    else \
        scp -r ./dags/{{ folder }} {{ target }}:./dev/dags; \
    fi

Output of just scp utils server

if [ "utils" == "plugins" ]; then scp -r ./plugins server:./dev else scp -r ./dags/utils server:./dev/dags; fi

Also, I wish the preview is given when the confirm attribute is set instead of just Run recipe 'scp'? similar to --evaluate Pun unintentional.. 😂

laniakea64 commented 10 months ago

I wish the following syntax works:

Can you put the entire conditional in an interpolation? -

# justfile

scp folder target:
    {{if folder == "plugins" { \
        "scp -r ./plugins " + target + ":./dev" \
    } \
    else { \
        "scp -r ./dags/" + folder + " " + target + ":./dev/dags" \
    } }}
NewtonChutney commented 10 months ago

Can you put the entire conditional in an interpolation? -

Okay.. I'd say that solves the first half of the issue! ♥️

Have to check the output.. Will update

casey commented 10 months ago

To make the bash version nicer, you can also do:

scp folder target:
    !#/usr/bin/env bash
    set -euxo pipefail
    if [ "{{ folder }}" == "plugins" ]; then
        scp -r ./plugins {{ target }}:./dev
    else
        scp -r ./dags/{{ folder }} {{ target }}:./dev/dags
    fi