1-
21use std:: { collections:: HashMap , time:: Instant } ;
32
43use mongodb:: bson:: uuid;
54use serde_json:: json;
65
7- use crate :: { utils:: { common:: { execute_command, post_json_data} , file_utils:: find_files_recursively} , scans:: tools:: sca_tool:: SUPPORTED_MANIFESTS } ;
8-
6+ use crate :: {
7+ scans:: tools:: sca_tool:: SUPPORTED_MANIFESTS ,
8+ utils:: { common:: execute_command, file_utils:: find_files_recursively} ,
9+ } ;
910
1011pub struct LicenseTool ;
1112
1213impl LicenseTool {
1314 pub fn new ( ) -> Self {
1415 LicenseTool
1516 }
16-
17- pub async fn run_scan ( & self , _path : & str , _commit_id : Option < & str > , _branch : Option < & str > , verbose : bool ) {
17+
18+ pub async fn run_scan (
19+ & self ,
20+ _path : & str ,
21+ _commit_id : Option < & str > ,
22+ _branch : Option < & str > ,
23+ verbose : bool ,
24+ ) {
1825 let start_time = Instant :: now ( ) ;
1926 if verbose {
2027 println ! ( "[+] Running License compliance scan on path: {}" , _path) ;
@@ -33,11 +40,11 @@ impl LicenseTool {
3340 if let Some ( _branch) = _branch {
3441 let clone_command = format ! ( "git clone -b {} {} /tmp/app" , _branch, _path) ;
3542 execute_command ( & clone_command, false ) . await ;
36- } else {
43+ } else {
3744 let clone_command = format ! ( "git clone {} /tmp/app" , _path) ;
3845 execute_command ( & clone_command, false ) . await ;
3946 }
40- } else {
47+ } else {
4148 if verbose {
4249 println ! ( "[+] Copying project to /tmp/app..." ) ;
4350 }
@@ -58,20 +65,27 @@ impl LicenseTool {
5865 // now run secret scan on /tmp/code folder
5966 _path = format ! ( "/tmp/code" ) ;
6067 }
61- let manifests = find_files_recursively ( & _path, unsafe { SUPPORTED_MANIFESTS . to_vec ( ) } , ignore_dirs) . await ;
68+ let manifests =
69+ find_files_recursively ( & _path, unsafe { SUPPORTED_MANIFESTS . to_vec ( ) } , ignore_dirs)
70+ . await ;
6271 let mut manifest_license = HashMap :: new ( ) ;
6372 for manifest in manifests. iter ( ) {
6473 let file_name = manifest. split ( "/" ) . last ( ) . unwrap ( ) ;
6574 let folder_path = manifest. replace ( file_name, "" ) ;
6675 let random_file_name = format ! ( "{}.json" , uuid:: Uuid :: new( ) . to_string( ) ) ;
6776 // if manifest ends with pom.xml then pass -t java otherwise nothing
68- let mut license_command = format ! ( "cd {} && cdxgen -o {}" , folder_path, random_file_name) ;
77+ let mut license_command =
78+ format ! ( "cd {} && cdxgen -o {}" , folder_path, random_file_name) ;
6979 if file_name. ends_with ( "pom.xml" ) {
70- license_command = format ! ( "cd {} && cdxgen -o {} -t java" , folder_path, random_file_name) ;
80+ license_command = format ! (
81+ "cd {} && cdxgen -o {} -t java" ,
82+ folder_path, random_file_name
83+ ) ;
7184 }
7285 execute_command ( & license_command, false ) . await ;
7386 // Read JSON file and parse data
74- let license_json = std:: fs:: read_to_string ( format ! ( "{}/{}" , folder_path, random_file_name) ) . unwrap ( ) ;
87+ let license_json =
88+ std:: fs:: read_to_string ( format ! ( "{}/{}" , folder_path, random_file_name) ) . unwrap ( ) ;
7589 let json_data = serde_json:: from_str :: < serde_json:: Value > ( & license_json) . unwrap ( ) ;
7690 // extract license data from "components" key there will be list of components so grab licenses from there
7791 let components = json_data[ "components" ] . as_array ( ) . unwrap ( ) ;
@@ -87,8 +101,14 @@ impl LicenseTool {
87101 license_names. push ( license[ "id" ] . as_str ( ) . unwrap ( ) . to_string ( ) ) ;
88102 }
89103 }
90- component_licenses. insert ( format ! ( "{}@{}" , component_name, component_version) , license_names) ;
91- manifest_license. insert ( format ! ( "{}/{}" , folder_path, file_name) , component_licenses. clone ( ) ) ;
104+ component_licenses. insert (
105+ format ! ( "{}@{}" , component_name, component_version) ,
106+ license_names,
107+ ) ;
108+ manifest_license. insert (
109+ format ! ( "{}/{}" , folder_path, file_name) ,
110+ component_licenses. clone ( ) ,
111+ ) ;
92112 }
93113 }
94114 // save data in output.json and before that get json data from output.json file if it exists and then append new data to it
@@ -99,10 +119,17 @@ impl LicenseTool {
99119 output_json = serde_json:: from_str :: < serde_json:: Value > ( & output_json_data) . unwrap ( ) ;
100120 }
101121 output_json[ "license" ] = json ! ( manifest_license) ;
102- std:: fs:: write ( "/tmp/output.json" , serde_json:: to_string_pretty ( & output_json) . unwrap ( ) ) . unwrap ( ) ;
122+ std:: fs:: write (
123+ "/tmp/output.json" ,
124+ serde_json:: to_string_pretty ( & output_json) . unwrap ( ) ,
125+ )
126+ . unwrap ( ) ;
103127 let end_time = Instant :: now ( ) ;
104128 let elapsed_time = end_time - start_time;
105129 let elapsed_seconds = elapsed_time. as_secs_f64 ( ) . round ( ) ;
106- println ! ( "Execution time for License Compliance scan: {:?} seconds" , elapsed_seconds) ;
130+ println ! (
131+ "Execution time for License Compliance scan: {:?} seconds" ,
132+ elapsed_seconds
133+ ) ;
107134 }
108- }
135+ }
0 commit comments