2
2
# -*- coding: utf-8 -*-
3
3
4
4
import argparse
5
- import logging
6
5
import os
7
- import sys
8
6
9
- from blint .lib .analysis import AnalysisRunner , report
7
+ from blint .lib .runners import run_default_mode , run_sbom_mode
8
+ from blint .lib .runners import AnalysisRunner , run_default_mode , run_sbom_mode
9
+ from blint .config import BlintOptions
10
10
from blint .logger import LOG
11
11
from blint .lib .sbom import generate
12
12
from blint .lib .utils import gen_file_list
@@ -25,10 +25,23 @@ def build_args():
25
25
"""
26
26
Constructs command line arguments for the blint tool
27
27
"""
28
+ parser = build_parser ()
29
+ return parser .parse_args ()
30
+
31
+
32
+ def build_parser ():
28
33
parser = argparse .ArgumentParser (
29
34
prog = "blint" ,
30
35
description = "Binary linter and SBOM generator." ,
31
36
)
37
+ parser .set_defaults (
38
+ deep_mode = False ,
39
+ sbom_output = "" ,
40
+ stdout_mode = False ,
41
+ exports_prefix = [],
42
+ src_dir_boms = [],
43
+ sbom_mode = False
44
+ )
32
45
parser .add_argument (
33
46
"-i" ,
34
47
"--src" ,
@@ -82,14 +95,14 @@ def build_args():
82
95
sbom_parser = subparsers .add_parser (
83
96
"sbom" , help = "Command to generate SBOM for supported binaries."
84
97
)
98
+ sbom_parser .set_defaults (sbom_mode = True )
85
99
sbom_parser .add_argument (
86
100
"-i" ,
87
101
"--src" ,
88
102
dest = "src_dir_image" ,
89
103
action = "extend" ,
90
104
nargs = "+" ,
91
- help = "Source directories, container images or binary files. Defaults "
92
- "to current directory." ,
105
+ help = "Source directories, container images or binary files. Defaults to current directory." ,
93
106
)
94
107
sbom_parser .add_argument (
95
108
"-o" ,
@@ -127,7 +140,8 @@ def build_args():
127
140
nargs = "+" ,
128
141
help = "Directories containing pre-build and build BOMs. Use to improve the precision." ,
129
142
)
130
- return parser .parse_args ()
143
+
144
+ return parser
131
145
132
146
133
147
def parse_input (src ):
@@ -153,68 +167,40 @@ def parse_input(src):
153
167
def handle_args ():
154
168
"""Handles the command-line arguments.
155
169
156
- This function parses the command-line arguments and returns the parsed
157
- arguments, reports directory, and source directory.
170
+ This function parses the command-line arguments and returns a BlintOptions object
158
171
159
172
Returns:
160
- tuple: A tuple containing the parsed arguments, reports directory, and
161
- source directory.
173
+ BlintOptions: A class containing the parsed command-line arguments
162
174
"""
163
175
args = build_args ()
164
176
if not args .no_banner and args .subcommand_name != "sbom" :
165
177
print (BLINT_LOGO )
166
- if not args .src_dir_image :
167
- args .src_dir_image = [os .getcwd ()]
168
- if not os .getenv ("CI" ):
169
- src_dirs = args .src_dir_image
170
- else :
171
- src_dirs = parse_input (args .src_dir_image )
172
-
173
- # Create reports directory
174
- reports_dir = args .reports_dir
175
-
176
- for src in src_dirs :
177
- if not os .path .exists (src ):
178
- LOG .error (f"{ src } is an invalid file or directory!" )
179
- sys .exit (1 )
180
- return args , reports_dir , src_dirs
178
+ blint_options = BlintOptions (
179
+ deep_mode = args .deep_mode ,
180
+ exports_prefix = args .exports_prefix ,
181
+ fuzzy = args .suggest_fuzzable ,
182
+ no_error = args .noerror ,
183
+ no_reviews = args .no_reviews ,
184
+ reports_dir = args .reports_dir ,
185
+ sbom_mode = args .sbom_mode ,
186
+ sbom_output = args .sbom_output ,
187
+ src_dir_boms = args .src_dir_boms ,
188
+ src_dir_image = args .src_dir_image ,
189
+ stdout_mode = args .stdout_mode
190
+ )
191
+ return blint_options
181
192
182
193
183
194
def main ():
184
195
"""Main function of the blint tool"""
185
- args , reports_dir , src_dirs = handle_args ()
196
+ blint_options = handle_args ()
186
197
187
198
# SBOM command
188
- if args .subcommand_name == "sbom" :
189
- if args .stdout_mode :
190
- sbom_output = sys .stdout
191
- LOG .setLevel (logging .ERROR )
192
- else :
193
- if args .sbom_output :
194
- sbom_output = args .sbom_output
195
- if os .path .isdir (sbom_output ):
196
- sbom_output = os .path .join (sbom_output , "bom-post-build.cdx.json" )
197
- else :
198
- sbom_output = os .path .join (os .getcwd (), "bom-post-build.cdx.json" )
199
- sbom_output_dir = os .path .dirname (sbom_output )
200
- if sbom_output_dir and not os .path .exists (sbom_output_dir ):
201
- os .makedirs (sbom_output_dir )
202
- generate (src_dirs , sbom_output , args .deep_mode , args .exports_prefix , args .src_dir_boms )
199
+ if blint_options .sbom_mode :
200
+ run_sbom_mode (blint_options )
203
201
# Default case
204
202
else :
205
- if reports_dir and not os .path .exists (reports_dir ):
206
- os .makedirs (reports_dir )
207
- files = gen_file_list (src_dirs )
208
- analyzer = AnalysisRunner ()
209
- findings , reviews , fuzzables = analyzer .start (
210
- files , reports_dir , args .no_reviews , args .suggest_fuzzable
211
- )
212
- report (src_dirs , reports_dir , findings , reviews , files , fuzzables )
213
-
214
- if os .getenv ("CI" ) and not args .noerror :
215
- for f in findings :
216
- if f ['severity' ] == 'critical' :
217
- sys .exit (1 )
203
+ run_default_mode (blint_options )
218
204
219
205
220
206
if __name__ == "__main__" :
0 commit comments