-
Notifications
You must be signed in to change notification settings - Fork 106
Refactor env loading, appDir resolution, and exec shell #302
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e3f553e
first attempt at readlink
nonrational 8d60d82
load env in go and pass it to shell
nonrational 2c0a7d4
load env entirely in go before passing to bash
nonrational 4de7e0b
remove neko
nonrational 374cbce
go mod tidy
nonrational af70b01
use SHELL to supervise puma process
nonrational 50b2a1c
build puma CLI args in go
nonrational a8f5df4
Merge remote-tracking branch 'puma/master' into resolve-full-path
nonrational 6da2960
put back shell sourcing as a fallback
nonrational 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
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,27 @@ | ||
package dev | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildPumaCommand_withEnv(t *testing.T) { | ||
t.Setenv("CONFIG", "-") | ||
t.Setenv("WORKERS", "4") | ||
t.Setenv("THREADS", "5") | ||
|
||
cmd, _ := BuildPumaCommand("foo", "/tmp/tmp/foo.sock", "/tmp") | ||
|
||
assert.Regexp(t, " -w4 ", cmd.Args[4]) | ||
assert.Regexp(t, " -t0:5 ", cmd.Args[4]) | ||
assert.Regexp(t, " -C- ", cmd.Args[4]) | ||
} | ||
|
||
func TestBuildPumaCommand_withoutEnv(t *testing.T) { | ||
cmd, _ := BuildPumaCommand("foo", "/tmp/tmp/foo.sock", "/tmp") | ||
|
||
assert.NotRegexp(t, " -w", cmd.Args[4]) | ||
assert.NotRegexp(t, " -t", cmd.Args[4]) | ||
assert.NotRegexp(t, " -C", cmd.Args[4]) | ||
} |
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,74 @@ | ||
package dev | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/puma/puma-dev/homedir" | ||
) | ||
|
||
// `.pumaenv` has highest priority, `~/.powconfig` has lowest. | ||
// Be sure to keep these in sync with the pumaShellScriptTemplate in app.go | ||
var AppEnvPaths = []string{".env", ".powrc", ".powenv", ".pumaenv"} | ||
var HomeEnvPaths = []string{"~/.powconfig", "~/.pumaenv"} | ||
|
||
func LoadEnv(baseEnv map[string]string, dir string) (envMap map[string]string, err error) { | ||
// build a list of all the supported env files that exist on the filesystem to load | ||
var envPaths []string | ||
|
||
for _, path := range AppEnvPaths { | ||
fullPath := filepath.Join(dir, path) | ||
if _, err := os.Stat(fullPath); err == nil { | ||
envPaths = append(envPaths, fullPath) | ||
} | ||
} | ||
|
||
for _, path := range HomeEnvPaths { | ||
if fullPath, err := homedir.Expand(path); err == nil { | ||
if _, err := os.Stat(fullPath); err == nil { | ||
envPaths = append(envPaths, fullPath) | ||
} | ||
} | ||
} | ||
|
||
envMap = baseEnv | ||
|
||
// if we have any optional env paths to source, source them | ||
if len(envPaths) > 0 { | ||
// load the env into a map | ||
appEnv, err := godotenv.Read(envPaths...) | ||
if err != nil { | ||
// if any path fails to load, bail out. | ||
return nil, fmt.Errorf("%v in %v", err, envPaths) | ||
} | ||
|
||
// merge contents from appEnv into envMap | ||
// this way dotenv values will supersede os env values | ||
for k, v := range appEnv { | ||
envMap[k] = v | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func ToCmdEnv(envMap map[string]string) (env []string) { | ||
for k, v := range envMap { | ||
env = append(env, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return | ||
} | ||
|
||
func GetMapEnviron() (envMap map[string]string) { | ||
envMap = make(map[string]string) | ||
|
||
for _, keyval := range os.Environ() { | ||
pair := strings.Split(keyval, "=") | ||
envMap[pair[0]] = pair[1] | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.