Skip to content

Commit 05f97a4

Browse files
authored
Merge pull request #162 from mattn/fix-export-env-parsing
Parse .env with godotenv in export
2 parents bf53d59 + ca50036 commit 05f97a4

1 file changed

Lines changed: 13 additions & 21 deletions

File tree

export.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9+
10+
"github.com/joho/godotenv"
911
)
1012

1113
func exportUpstart(cfg *config, path string) error {
14+
procfile, err := filepath.Abs(cfg.Procfile)
15+
if err != nil {
16+
return err
17+
}
18+
// parse .env the same way `goreman start` does (godotenv), so exported
19+
// values match the runtime environment.
20+
env, err := godotenv.Read(filepath.Join(filepath.Dir(procfile), ".env"))
21+
if err != nil {
22+
env = map[string]string{}
23+
}
24+
1225
for _, proc := range procs {
1326
f, err := os.Create(filepath.Join(path, "app-"+proc.name+".conf"))
1427
if err != nil {
@@ -20,27 +33,6 @@ func exportUpstart(cfg *config, path string) error {
2033
fmt.Fprintf(f, "respawn\n")
2134
fmt.Fprintf(f, "\n")
2235

23-
env := map[string]string{}
24-
procfile, err := filepath.Abs(cfg.Procfile)
25-
if err != nil {
26-
return err
27-
}
28-
b, err := os.ReadFile(filepath.Join(filepath.Dir(procfile), ".env"))
29-
if err == nil {
30-
for _, line := range strings.Split(string(b), "\n") {
31-
token := strings.SplitN(line, "=", 2)
32-
if len(token) != 2 {
33-
continue
34-
}
35-
if strings.HasPrefix(token[0], "export ") {
36-
token[0] = token[0][7:]
37-
}
38-
token[0] = strings.TrimSpace(token[0])
39-
token[1] = strings.TrimSpace(token[1])
40-
env[token[0]] = token[1]
41-
}
42-
}
43-
4436
if proc.setPort {
4537
fmt.Fprintf(f, "env PORT=%d\n", proc.port)
4638
}

0 commit comments

Comments
 (0)