Skip to content

Commit e7c23eb

Browse files
committed
feat: Load env file with the environ
Signed-off-by: Vincent Boutour <[email protected]>
1 parent cccc29b commit e7c23eb

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

sublime/plugins/SublimeGo/env_loader.py

+34-27
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,44 @@
99
env_regex = re.compile("^(export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)")
1010

1111

12-
def load_env(cwd, name):
12+
def load_env_file(cwd, name):
1313
env_file = join(cwd, name)
1414

15-
if exists(env_file):
16-
try:
17-
env_values = subprocess.check_output(
18-
[
19-
"sh",
20-
"--norc",
21-
"-c",
22-
"source '{}' && declare".format(env_file),
23-
],
24-
stderr=subprocess.STDOUT,
25-
cwd=cwd,
26-
)
27-
except subprocess.CalledProcessError as e:
28-
print("unable to source .env: {}".format(e.output.decode("utf8")))
29-
return None
30-
31-
env = {}
32-
for line in env_values.decode("utf8").rstrip().split("\n"):
33-
if env_regex.match(line):
34-
parts = env_regex.findall(line)
35-
env[parts[0][1]] = parts[0][2]
15+
if not exists(env_file):
16+
return {}
3617

37-
return env
18+
try:
19+
env_values = subprocess.check_output(
20+
[
21+
"bash",
22+
"-c",
23+
"set -o pipefail -o errexit; comm -13 <(declare) <(source '{}' && declare)".format(
24+
env_file
25+
),
26+
],
27+
stderr=subprocess.STDOUT,
28+
)
29+
except subprocess.CalledProcessError as e:
30+
print("unable to source .env: {}".format(e.output.decode("utf8")))
31+
return None
32+
33+
env = {}
3834

39-
return None
35+
for line in env_values.decode("utf8").rstrip().split("\n"):
36+
if env_regex.match(line):
37+
parts = env_regex.findall(line)
38+
env[parts[0][1]] = parts[0][2]
39+
40+
return env
4041

4142

4243
def load_git_root_env(cwd):
44+
env = dict(os.environ)
45+
4346
is_git = subprocess.call(["git", "rev-parse", "--is-inside-work-tree"], cwd=cwd)
4447
if is_git != 0:
4548
print("not in git")
46-
return None
49+
return env
4750

4851
try:
4952
git_root = subprocess.check_output(
@@ -53,8 +56,12 @@ def load_git_root_env(cwd):
5356
)
5457
except subprocess.CalledProcessError as e:
5558
print("unable to get root path: {}".format(e.output.decode("utf8")))
56-
return None
59+
return env
5760

5861
root = git_root.decode("utf8").rstrip()
5962

60-
return load_env(root, ".env")
63+
dot_env = load_env_file(root, ".env")
64+
if dot_env:
65+
env.update(dot_env)
66+
67+
return env

sublime/plugins/SublimeGo/test.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading
44
from .async_task import AsyncTask
55
from .env_loader import load_git_root_env
6+
import os
67

78

89
class GoTest(sublime_plugin.WindowCommand):
@@ -35,10 +36,6 @@ def run(self, kill=False):
3536
settings.set("result_base_dir", working_dir)
3637
window.run_command("show_panel", {"panel": "output.gotest"})
3738

38-
env = load_git_root_env(working_dir)
39-
40-
print(env)
41-
4239
if self.task:
4340
self.task.kill()
4441

@@ -48,7 +45,7 @@ def run(self, kill=False):
4845
command=["go", "test", "-cover", "-race"],
4946
output=self.queue_write,
5047
cwd=working_dir,
51-
env=env,
48+
env=load_git_root_env(working_dir),
5249
)
5350

5451
def queue_write(self, text):

sublime/plugins/SublimeGo/test_function.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ def run(self, kill=False):
3535
settings.set("result_base_dir", working_dir)
3636
window.run_command("show_panel", {"panel": "output.gotest"})
3737

38-
env = load_env(working_dir)
39-
4038
if self.task:
4139
self.task.kill()
4240

@@ -48,7 +46,7 @@ def run(self, kill=False):
4846
command=["go", "test", "-cover", "-race", "-run", function_name],
4947
output=self.queue_write,
5048
cwd=working_dir,
51-
env=env,
49+
env=load_git_root_env(working_dir),
5250
)
5351

5452
def queue_write(self, text):

0 commit comments

Comments
 (0)