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

[Feature] Add support for env files #126

Merged
merged 10 commits into from
Dec 16, 2024

Conversation

jackwrfuller
Copy link
Contributor

@jackwrfuller jackwrfuller commented Dec 9, 2024

Environment variable files (i.e ".env") are a commonly used method to define and manage project specific secrets and configuration.

It is standard for yaml-like projects to allow you to access env vars you have defined in env files in your project. For example, Docker Compose will read variables defined in an ".env" file by default without any configuration.

Currently, to access a variable FOO=bar defined in a file ".env" in a command, you would need prepend a source .env to the command each and every time:

source .env && echo $FOO

This is tiresome and error-prone. In my proposed PR, we would allow the user to define global and command-level env files. For example:

ahoyapi: v2
env: .env
commands:
  bar:
    cmd: echo $BAR
  foo:
    cmd: echo $FOO
    env: .env.default

In the case where a command has both the global and command-level env files set, and these files contain conflicting variable definitions, I propose that the command-level env file overrides the global one.

Also, as it is currently implemented the filepath provided to the env: yaml key must be relative to the Ahoy source directory; see for example:

if config.Env != "" {
        globalEnvFile := filepath.Join(AhoyConf.srcDir, config.Env)
	envVars = append(envVars, getEnvironmentVars(globalEnvFile)...)
}

@ocean
Copy link
Member

ocean commented Dec 9, 2024

Thanks for these two PRs @jackwrfuller 🙌

I'll have a look in the next couple of days and make a new release.

testdata/.env Show resolved Hide resolved
ahoy.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@AlexSkrypnyk AlexSkrypnyk left a comment

Choose a reason for hiding this comment

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

LGTM

@ocean Could you please review

@ocean ocean merged commit fb5031c into ahoy-cli:master Dec 16, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants