...
This commit is contained in:
44
hosts/main/bin/direnv-diff
Executable file
44
hosts/main/bin/direnv-diff
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import shlex
|
||||
|
||||
|
||||
def read_env(data):
|
||||
return dict(
|
||||
item.split(b"=", 1)
|
||||
for item in data.split(b"\0")
|
||||
if item
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print(f"Usage: {sys.argv[0]} DIR", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
before = {
|
||||
os.fsencode(k): os.fsencode(v)
|
||||
for k, v in os.environ.items()
|
||||
}
|
||||
|
||||
result = subprocess.run(
|
||||
["direnv", "exec", sys.argv[1], "env", "-0"],
|
||||
stdout=subprocess.PIPE,
|
||||
check=True,
|
||||
)
|
||||
|
||||
after = read_env(result.stdout)
|
||||
|
||||
|
||||
for key, value in after.items():
|
||||
if before.get(key) != value:
|
||||
name = os.fsdecode(key)
|
||||
val = os.fsdecode(value)
|
||||
print(f"export {name}={shlex.quote(val)}")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user