-
Notifications
You must be signed in to change notification settings - Fork 269
[devbox.json] support env_from dotenv files #2174
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,63 @@ | ||
package configfile | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func (c *ConfigFile) IsEnvsecEnabled() bool { | ||
// envsec for legacy. | ||
return c.EnvFrom == "envsec" || c.EnvFrom == "jetpack-cloud" | ||
// envsec for legacy. jetpack-cloud for legacy | ||
return c.EnvFrom == "envsec" || c.EnvFrom == "jetpack-cloud" || c.EnvFrom == "jetify-cloud" | ||
} | ||
|
||
func (c *ConfigFile) IsdotEnvEnabled() bool { | ||
// filename has to end with .env | ||
return filepath.Ext(c.EnvFrom) == ".env" | ||
} | ||
|
||
func (c *ConfigFile) ParseEnvsFromDotEnv() (map[string]string, error) { | ||
// This check should never happen because we call IsdotEnvEnabled | ||
// before calling this method. But having it makes it more robust | ||
// in case if anyone uses this method without the IsdotEnvEnabled | ||
if !c.IsdotEnvEnabled() { | ||
return nil, fmt.Errorf("env file does not have a .env extension") | ||
} | ||
|
||
file, err := os.Open(c.EnvFrom) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open file: %s", c.EnvFrom) | ||
} | ||
defer file.Close() | ||
|
||
envMap := map[string]string{} | ||
|
||
// Read the file line by line | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
// Ideally .env file shouldn't have empty lines and comments but | ||
// this check makes it allowed. | ||
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
parts := strings.SplitN(line, "=", 2) | ||
if len(parts) != 2 { | ||
return nil, fmt.Errorf("invalid line in .env file: %s", line) | ||
} | ||
// Also ideally, .env files should not have space in their `key=value` format | ||
// but this allows `key = value` to pass through as well | ||
key := strings.TrimSpace(parts[0]) | ||
value := strings.TrimSpace(parts[1]) | ||
|
||
// Add the parsed key-value pair to the map | ||
envMap[key] = value | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, fmt.Errorf("failed to read env file: %v", err) | ||
} | ||
return envMap, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Tests related to setting the env_from for devbox run. | ||
|
||
exec devbox run test | ||
stdout 'BAR' | ||
|
||
exec devbox run test2 | ||
stdout 'BAZ' | ||
|
||
exec devbox run test3 | ||
stdout 'BAS' | ||
|
||
exec devbox run test4 | ||
stdout '' | ||
|
||
-- test.env -- | ||
FOO=BAR | ||
FOO2 = BAZ | ||
FOO3=ToBeOverwrittenByDevboxJSON | ||
# FOO4=comment shouldn't be processed | ||
|
||
-- devbox.json -- | ||
{ | ||
"packages": [], | ||
"env": { | ||
"FOO3": "BAS" | ||
}, | ||
"shell": { | ||
"scripts": { | ||
"test": "echo $FOO", | ||
"test2": "echo $FOO2", | ||
"test3": "echo $FOO3", | ||
"test4": "echo $FOO4" | ||
} | ||
}, | ||
"env_from": "test.env" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail if devbox is call from any other directory that is not where the devbox.json is. I don't think it makes sense to make this relative from the working directory, it should be relative from the config file directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this, my project failed due to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@varunpalekar thanks for reminding on the issue. I made a PR to fix this, once merged it will be available in the next version of devbox.