Files
PMOCrazycoder/opt/dev-tools/list_imports.py

28 lines
842 B
Python
Raw Normal View History

#!/usr/bin/env python3
import sys, json
def get_imports_go(filepath: str) -> list[str]:
try:
with open(filepath) as f:
lines = f.readlines()
imports = []
in_import_block = False
for line in lines:
stripped = line.strip()
if stripped == "import (":
in_import_block = True
continue
if stripped == ")" and in_import_block:
break
if in_import_block and stripped.startswith('"'):
imports.append(stripped.strip('"'))
return imports
except Exception as e:
return [f"error: {e}"]
if __name__ == "__main__":
args = json.loads(sys.argv[1])
filepath = args.get("file", "")
result = get_imports_go(filepath)
print(json.dumps({"result": result}, indent=2))