Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cve_bin_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
get_backport_supported_distros,
)
from cve_bin_tool.config import ConfigParser
from cve_bin_tool.config_generator import ConfigGenerator
from cve_bin_tool.config_generator import config_generator
from cve_bin_tool.cve_scanner import CVEScanner
from cve_bin_tool.cvedb import CVEDB, OLD_CACHE_DIR
from cve_bin_tool.data_sources import (
Expand Down Expand Up @@ -659,7 +659,7 @@ def main(argv=None):
# current format includes .yaml and .toml
for config_format in config_formats:
LOGGER.debug(f"Arguments declared in generating config file {args}")
ConfigGenerator.config_generator(config_format, organized_arguments)
config_generator(config_format, organized_arguments)

return 0

Expand Down
117 changes: 55 additions & 62 deletions cve_bin_tool/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json

config_header = """
# This is a generated configuration file from the CVE Binary Tool ConfigGenerator.
# This is a generated configuration file from the CVE Binary Tool.
# Exercise caution when editing. To generate a new config file, use --generate-config option.
# For more info, refer to the official documentation at: https://cve-bin-tool.readthedocs.io/en/latest/.
# If you support the project and wish to contribute, check the Contributor Guide:
Expand All @@ -12,67 +12,60 @@
"""


class ConfigGenerator:
def config_generator(config_format, organized_arguments):
"""
A class for generating configuration files in different formats.
"""

def config_generator(config_format, organized_arguments):
"""
Generate a configuration file in the specified format.
Generate a configuration file in the specified format.

Args:
config_format (str): The format of the configuration file (".toml" or ".yaml").
organized_arguments (dict): A dictionary containing organized arguments.
Args:
config_format (str): The format of the configuration file (".toml" or ".yaml").
organized_arguments (dict): A dictionary containing organized arguments.

Returns:
None
"""
if config_format == "toml":
first_char = "["
last_char = "]"
sign = "="
coma = '"'
elif config_format == "yaml":
first_char = ""
last_char = ":"
sign = ":"
coma = ""
else:
return
with open(f"config.{config_format}", "w") as f:
f.write(f"{config_header}\n")
for group_title, group_args in organized_arguments.items():
if group_title == "positional_arguments":
continue
group_title = group_title.lower()
if group_title == "output":
if group_args["sbom-output"]["arg_value"] == "":
group_args["sbom-type"]["arg_value"] = None
group_args["sbom-format"]["arg_value"] = None
group_args["sbom-output"]["arg_value"] = None
if group_args["vex"]["arg_value"] == "":
group_args["vex"]["arg_value"] = None
f.write(f"{first_char}{group_title}{last_char}\n")
for arg_name, arg_value_help in group_args.items():
arg_value = arg_value_help["arg_value"]
arg_help = arg_value_help["help"]
arg_name = arg_name.replace("-", "_")
if arg_name in ["config", "generate_config"]:
arg_value = None
if "\n" in arg_help:
arg_help = arg_help.replace("\n", " ")
if arg_value in [True, False] or isinstance(arg_value, list):
arg_val = (
json.dumps(arg_value).lower()
if arg_value in [True, False]
else arg_value
)
f.write(
f" # {arg_help}\n" f" {arg_name} {sign} {arg_val}\n\n"
)
elif arg_value is not None:
f.write(
f" # {arg_help}\n"
f" {arg_name} {sign} {coma}{arg_value}{coma}\n\n"
)
Returns:
None
"""
if config_format == "toml":
first_char = "["
last_char = "]"
sign = "="
coma = '"'
elif config_format == "yaml":
first_char = ""
last_char = ":"
sign = ":"
coma = ""
else:
return
with open(f"config.{config_format}", "w") as f:
f.write(f"{config_header}\n")
for group_title, group_args in organized_arguments.items():
if group_title == "positional_arguments":
continue
group_title = group_title.lower()
if group_title == "output":
if group_args["sbom-output"]["arg_value"] == "":
group_args["sbom-type"]["arg_value"] = None
group_args["sbom-format"]["arg_value"] = None
group_args["sbom-output"]["arg_value"] = None
if group_args["vex"]["arg_value"] == "":
group_args["vex"]["arg_value"] = None
f.write(f"{first_char}{group_title}{last_char}\n")
for arg_name, arg_value_help in group_args.items():
arg_value = arg_value_help["arg_value"]
arg_help = arg_value_help["help"]
arg_name = arg_name.replace("-", "_")
if arg_name in ["config", "generate_config"]:
arg_value = None
if "\n" in arg_help:
arg_help = arg_help.replace("\n", " ")
if arg_value in [True, False] or isinstance(arg_value, list):
arg_val = (
json.dumps(arg_value).lower()
if arg_value in [True, False]
else arg_value
)
f.write(f" # {arg_help}\n" f" {arg_name} {sign} {arg_val}\n\n")
elif arg_value is not None:
f.write(
f" # {arg_help}\n"
f" {arg_name} {sign} {coma}{arg_value}{coma}\n\n"
)