From cccc29bdd78a707794b15423e1802eb724ea8ed5 Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Sat, 5 Aug 2023 11:30:24 +0200 Subject: [PATCH] feat(sublime): Adding better env parsing Signed-off-by: Vincent Boutour --- sublime/plugins/SublimeGo/env_loader.py | 52 +++++++++++++++---------- sublime/plugins/SublimeGo/test.py | 6 ++- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/sublime/plugins/SublimeGo/env_loader.py b/sublime/plugins/SublimeGo/env_loader.py index 0d15912a..cea722a2 100644 --- a/sublime/plugins/SublimeGo/env_loader.py +++ b/sublime/plugins/SublimeGo/env_loader.py @@ -6,34 +6,23 @@ from os.path import exists, join -env_regex = re.compile("^(export )?([A-Za-z]\\w+)=(.*)") +env_regex = re.compile("^(export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)") -def load_env(cwd): - is_git = subprocess.call(["git", "rev-parse", "--is-inside-work-tree"], cwd=cwd) - if is_git != 0: - print("not in git") - return None - - try: - git_root = subprocess.check_output( - ["git", "rev-parse", "--show-toplevel"], - stderr=subprocess.STDOUT, - cwd=cwd, - ) - except subprocess.CalledProcessError as e: - print("unable to get root path: {}".format(e.output.decode("utf8"))) - return None - - root = git_root.decode("utf8").rstrip() - env_file = join(root, ".env") +def load_env(cwd, name): + env_file = join(cwd, name) if exists(env_file): try: env_values = subprocess.check_output( - ["sh", "-c", "cat {} | envsubst && printenv".format(env_file)], + [ + "sh", + "--norc", + "-c", + "source '{}' && declare".format(env_file), + ], stderr=subprocess.STDOUT, - cwd=root, + cwd=cwd, ) except subprocess.CalledProcessError as e: print("unable to source .env: {}".format(e.output.decode("utf8"))) @@ -48,3 +37,24 @@ def load_env(cwd): return env return None + + +def load_git_root_env(cwd): + is_git = subprocess.call(["git", "rev-parse", "--is-inside-work-tree"], cwd=cwd) + if is_git != 0: + print("not in git") + return None + + try: + git_root = subprocess.check_output( + ["git", "rev-parse", "--show-toplevel"], + stderr=subprocess.STDOUT, + cwd=cwd, + ) + except subprocess.CalledProcessError as e: + print("unable to get root path: {}".format(e.output.decode("utf8"))) + return None + + root = git_root.decode("utf8").rstrip() + + return load_env(root, ".env") diff --git a/sublime/plugins/SublimeGo/test.py b/sublime/plugins/SublimeGo/test.py index 88485318..d989aa59 100644 --- a/sublime/plugins/SublimeGo/test.py +++ b/sublime/plugins/SublimeGo/test.py @@ -2,7 +2,7 @@ import sublime_plugin import threading from .async_task import AsyncTask -from .env_loader import load_env +from .env_loader import load_git_root_env class GoTest(sublime_plugin.WindowCommand): @@ -35,7 +35,9 @@ def run(self, kill=False): settings.set("result_base_dir", working_dir) window.run_command("show_panel", {"panel": "output.gotest"}) - env = load_env(working_dir) + env = load_git_root_env(working_dir) + + print(env) if self.task: self.task.kill()