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

Allow Tapioca commands to accept environment variables #1957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

paracycle
Copy link
Member

This change allows Tapioca commands to accept environment variables via the command line or the configuration file. This is useful for setting up the environment for the Tapioca commands, such as setting the custom environment variables that affect how an application is loaded.

Motivation

#1952 (comment)

Implementation

Add a class_option :env that allows all commands to accept environment variables.

Tests

Added a DSL CLI test

@paracycle paracycle added the enhancement New feature or request label Jul 12, 2024
@paracycle paracycle requested a review from Morriar July 12, 2024 15:02
@paracycle paracycle requested a review from a team as a code owner July 12, 2024 15:02
@paracycle paracycle requested a review from vinistock July 12, 2024 15:02
@paracycle paracycle force-pushed the uk-passthrough-env-vars branch from ebf1e69 to 58b981d Compare July 12, 2024 15:05
This change allows Tapioca commands to accept environment variables via the command line or the configuration file. This is useful for setting up the environment for the Tapioca commands, such as setting the custom environment variables that affect how an application is loaded.
@paracycle paracycle force-pushed the uk-passthrough-env-vars branch from 58b981d to cc5011f Compare July 12, 2024 15:07
sig { params(options: T.nilable(T::Hash[String, String])).void }
def set_all_environment_variables(options) # rubocop:disable Naming/AccessorMethodName
(options || {}).each do |key, value|
ENV[key] = value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the --env values are overriding the ENV, is this really what we want?

If so, I think we should document this in the option description or the README.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if this is indeed what we want, is there a reason not to use ENV.merge!(options)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I didn't think of ENV.merge!, that sounds better!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the --env values are overriding the ENV, is this really what we want?

What else could they be doing? Tapioca sets those env variables as the user has passed them and we don't care if they override some values or not. bin/tapioca dsl --env FOO:42 is the same thing that happens when someone runs FOO=42 bin/tapioca dsl, in either case we override whatever the value of the FOO env variable might have been to make it 42 for the duration of that run.

$stderr.puts "ENVIRONMENT: \#{ENV.to_h.inspect}"
RB

result = @project.tapioca("dsl --env Foo:Bar --env Baz:1")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test showing the behaviour of overriding system env?

@@ -88,6 +88,7 @@ Commands:
Options:
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: it might be my lack of familiarity with Thor, but it wasn't immediately obvious to me how we'd be passing multiple environment variables to the same command (I understood after reading the tests).

Could we add an example somewhere in the README of how to pass these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we already have a few hash type options and how they work is documented here: https://github.com/rails/thor/wiki/Method-Options#types-for-method_options

The main way is to use the --opt=key:value key2:value2 way, but if you mark the option as repeatable: true, then you can also do --opt=key:value --opt=key2:value2 too.

I guess I can add a section for how to pass env variables via command line or config file to make that more clear and link to the Thor docs for details.

sig { params(options: T.nilable(T::Hash[String, String])).void }
def set_all_environment_variables(options) # rubocop:disable Naming/AccessorMethodName
(options || {}).each do |key, value|
ENV[key] = value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if this is indeed what we want, is there a reason not to use ENV.merge!(options)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants