Skip to content

Commit

Permalink
feat(sublime): Adding better env parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 5, 2023
1 parent d3639d6 commit cccc29b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
52 changes: 31 additions & 21 deletions sublime/plugins/SublimeGo/env_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -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")
6 changes: 4 additions & 2 deletions sublime/plugins/SublimeGo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit cccc29b

Please sign in to comment.