Skip to content

Commit 5af9e8b

Browse files
author
Gabriel Francisco Mandaji
committed
go: support audit command
1 parent 192996e commit 5af9e8b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

scfw/package_managers/go.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,27 @@ def _normalize_command(self, command: list[str]) -> list[str]:
342342

343343
return [self._executable] + command[1:]
344344

345+
def list_installed_packages(self) -> list[Package]:
346+
"""
347+
List all installed packages.
348+
349+
Returns:
350+
A `list[Package]` representing all currently installed packages.
351+
"""
352+
def line_to_package(line: str) -> Optional[Package]:
353+
# All supported versions adhere to this format
354+
components = line.strip().split()
355+
if len(components) == 2:
356+
return Package(self.ecosystem(), components[0], components[1])
357+
return None
358+
359+
try:
360+
go_list_cmd = [self._executable, "list", "-m", "all"]
361+
list_cmd = subprocess.run(go_list_cmd, check=True, text=True, capture_output=True)
362+
return list(filter(None, map(line_to_package, list_cmd.stdout.split('\n'))))
363+
except subprocess.CalledProcessError:
364+
raise RuntimeError("Failed to list go installed packages")
365+
345366

346367
class GoModNotFoundError(Exception):
347368
"""

tests/package_managers/test_go_class.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def test_go_command_would_install_local(new_go_project):
8484
assert base_go.get_go_dir_contents() == global_init_state
8585

8686

87+
def test_go_list_installed_packages(new_go_project):
88+
"""
89+
Test that `Go.list_installed_packages` correctly parses `go` output.
90+
"""
91+
target = Package(ECOSYSTEM.Go, TARGET, LATEST_VERSION)
92+
_install_target(new_go_project)
93+
assert [target] == PACKAGE_MANAGER.list_installed_packages()
94+
95+
8796
def _install_target(project: GoProject):
8897
"""
8998
Install the target package to the provided directory and write a dummy source file.

0 commit comments

Comments
 (0)