Skip to content

Commit

Permalink
Added check for grype binary and optional installation prompt, per up…
Browse files Browse the repository at this point in the history
…stream issue popey#3.
  • Loading branch information
Shaun Setlock authored and Shaun Setlock committed Dec 14, 2024
1 parent 4f0d335 commit ae98643
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions grummage
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import json
import os
import subprocess
import sys
import tempfile
Expand All @@ -8,6 +9,34 @@ from textual.app import App
from textual.containers import Container, Horizontal, VerticalScroll
from textual.widgets import Tree, Footer, Static

def is_grype_installed():
"""Check if the grype binary is available in the system's PATH."""
return any(
os.path.isfile(os.path.join(path, "grype"))
and os.access(os.path.join(path, "grype"), os.X_OK)
for path in os.environ["PATH"].split(os.pathsep)
)

def prompt_install_grype():
"""Prompt the user to install grype if it's not installed."""
response = input(
"The grype binary is not located in $PATH. Would you like to install it now? [y/N]: "
).strip().lower()
return response == "y"

def install_grype():
"""Run the one-liner command to install grype."""
try:
# Use shell=True to execute the one-liner properly
subprocess.run(
"curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin",
shell=True,
check=True,
)
print("Grype successfully installed.")
except subprocess.CalledProcessError as e:
print(f"Failed to install grype: {e}")
sys.exit(1)

class Grummage(App):
BINDINGS = [
Expand Down Expand Up @@ -335,4 +364,12 @@ class Grummage(App):

if __name__ == "__main__":
sbom_file = sys.argv[1] if len(sys.argv) > 1 else sys.exit()
if not is_grype_installed():
if prompt_install_grype():
install_grype()
else:
print(
"The grype binary is not located in $PATH and the option to install was deferred."
)
sys.exit(0)
Grummage(sbom_file).run()

0 comments on commit ae98643

Please sign in to comment.