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

Support Conditional Assignments #245

Closed
Fleshgrinder opened this issue Nov 15, 2017 · 7 comments
Closed

Support Conditional Assignments #245

Fleshgrinder opened this issue Nov 15, 2017 · 7 comments

Comments

@Fleshgrinder
Copy link

Fleshgrinder commented Nov 15, 2017

Currently just allows us to assign values to variables, however, there is no way to conditionally assign like GNU Make has it. GNU Make uses the ?= for a conditional assignment, this seems like a reasonable syntax to me.

Note that this feature is especially important for environment variables.

export MY_ENV_VAR ?= foo

env:
    echo {{MY_ENV_VAR}}
$ MY_ENV_VAR=bar just env
echo bar
bar
@casey
Copy link
Owner

casey commented Nov 15, 2017

Thanks for opening this issue!

Hmm, interesting. You can currently override assignments at the command line, so this would specifically be for conditional assignment to environment variables if that environment variable isn't already set? For example, this works:

export MY_ENV_VAR = foo

env:
    echo {{MY_ENV_VAR}}
$ just MY_ENV_VAR=bar env
echo bar
bar

Is there a use case where this isn't good enough?

@Fleshgrinder
Copy link
Author

We often use make to automate docker and docker-compose stuff. One recipe is starting the complete stack, incl. a server, database, etc. all of which expose ports on the host. This often leads to port collisions, hence, every host port is managed through an environment variable that developers may change at will.

# start the stack with defaults, e.g. MySQL on 3306
$ make
# customize the stack, because 3306 is used by something else
$ PROJ_MYSQL_PORT=3307 make

@casey
Copy link
Owner

casey commented Nov 15, 2017

You can do this currently, although you need to move the assignment after the command, so that it's processed by just and not the shell:

# start the stack with defaults, e.g. MySQL on 3306
$ just
# customize the stack, because 3306 is used by something else
$ just PROJ_MYSQL_PORT=3307

@Fleshgrinder
Copy link
Author

This is possible with make too, but it also supports the standard POSIX way of doing it. Teaching every person that is verse in POSIX and shells that this particular program works differently, for whatever reason, is time-consuming, and usually not worth the effort. It would be better to have it implemented in the standard way of things.

That being said, the environment variable might come from a previous export in the shell, a .bashrc or similar. In that case, one is lost.

@bb010g
Copy link

bb010g commented Nov 26, 2017

It sounds like you more want a way to import environment variables and use them in just expressions.

@Fleshgrinder
Copy link
Author

That’s basically part of the idea, yeah. I want to use the environment variable if it is already defined and otherwise define it. There are a couple of things with which this feature could be extended. Like support for .env files.

@casey
Copy link
Owner

casey commented Dec 2, 2017

It's a bit longer, but you can now use something like export MY_ENV_VAR = env-default("MY_ENV_VAR", "foo") to assign to an environment variable if it is not defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants