@@ -30,7 +30,6 @@ use reqwest::{Client, Proxy};
30
30
use std:: collections:: HashMap ;
31
31
use std:: error:: Error ;
32
32
use std:: path:: { Path , PathBuf } ;
33
- use std:: process:: Command ;
34
33
use std:: time:: Duration ;
35
34
36
35
use crate :: downloads:: download_to_tmp_folder;
@@ -39,6 +38,7 @@ use crate::grid::GRID_NAME;
39
38
use crate :: logger:: Logger ;
40
39
use crate :: metadata:: { create_browser_metadata, get_browser_version_from_metadata} ;
41
40
use crate :: safaritp:: { SafariTPManager , SAFARITP_NAMES } ;
41
+ use crate :: shell:: { run_shell_command, run_shell_command_with_log, split_lines} ;
42
42
43
43
pub mod chrome;
44
44
pub mod config;
@@ -53,6 +53,7 @@ pub mod metadata;
53
53
pub mod mirror;
54
54
pub mod safari;
55
55
pub mod safaritp;
56
+ pub mod shell;
56
57
57
58
pub const REQUEST_TIMEOUT_SEC : u64 = 300 ; // The timeout is applied from when the request starts connecting until the response body has finished
58
59
pub const STABLE : & str = "stable" ;
@@ -84,8 +85,6 @@ pub const TTL_BROWSERS_SEC: u64 = 3600;
84
85
pub const TTL_DRIVERS_SEC : u64 = 3600 ;
85
86
pub const UNAME_COMMAND : & str = "uname -{}" ;
86
87
pub const ESCAPE_COMMAND : & str = "printf %q \" {}\" " ;
87
- pub const CRLF : & str = "\r \n " ;
88
- pub const LF : & str = "\n " ;
89
88
pub const SNAPSHOT : & str = "SNAPSHOT" ;
90
89
pub const OFFLINE_REQUEST_ERR_MSG : & str = "Unable to discover proper {} version in offline mode" ;
91
90
pub const OFFLINE_DOWNLOAD_ERR_MSG : & str = "Unable to download {} in offline mode" ;
@@ -227,10 +226,11 @@ pub trait SeleniumManager {
227
226
) ) ;
228
227
let mut browser_version: Option < String > = None ;
229
228
for command in commands. iter ( ) {
230
- let output = match self . run_shell_command_with_log ( command. to_string ( ) ) {
231
- Ok ( out) => out,
232
- Err ( _e) => continue ,
233
- } ;
229
+ let output =
230
+ match run_shell_command_with_log ( self . get_logger ( ) , self . get_os ( ) , vec ! [ command] ) {
231
+ Ok ( out) => out,
232
+ Err ( _e) => continue ,
233
+ } ;
234
234
let full_browser_version = parse_version ( output, self . get_logger ( ) ) . unwrap_or_default ( ) ;
235
235
if full_browser_version. is_empty ( ) {
236
236
continue ;
@@ -357,12 +357,16 @@ pub trait SeleniumManager {
357
357
}
358
358
359
359
fn find_driver_in_path ( & self ) -> ( Option < String > , Option < String > ) {
360
- match self . run_shell_command_with_log ( format_three_args (
361
- DASH_DASH_VERSION ,
362
- self . get_driver_name ( ) ,
363
- "" ,
364
- "" ,
365
- ) ) {
360
+ match run_shell_command_with_log (
361
+ self . get_logger ( ) ,
362
+ self . get_os ( ) ,
363
+ vec ! [ & format_three_args(
364
+ DASH_DASH_VERSION ,
365
+ self . get_driver_name( ) ,
366
+ "" ,
367
+ "" ,
368
+ ) ] ,
369
+ ) {
366
370
Ok ( output) => {
367
371
let parsed_version = parse_version ( output, self . get_logger ( ) ) . unwrap_or_default ( ) ;
368
372
if !parsed_version. is_empty ( ) {
@@ -381,7 +385,11 @@ pub trait SeleniumManager {
381
385
} else {
382
386
WHICH_COMMAND
383
387
} ;
384
- let path = match self . run_shell_command_with_log ( format_one_arg ( which_command, command) ) {
388
+ let path = match run_shell_command_with_log (
389
+ self . get_logger ( ) ,
390
+ self . get_os ( ) ,
391
+ vec ! [ & format_one_arg( which_command, command) ] ,
392
+ ) {
385
393
Ok ( path) => {
386
394
let path_vector = split_lines ( path. as_str ( ) ) ;
387
395
if path_vector. len ( ) == 1 {
@@ -529,14 +537,6 @@ pub trait SeleniumManager {
529
537
Ok ( driver_path)
530
538
}
531
539
532
- fn run_shell_command_with_log ( & self , command : String ) -> Result < String , Box < dyn Error > > {
533
- self . get_logger ( )
534
- . debug ( format ! ( "Running command: {}" , command) ) ;
535
- let output = run_shell_command_by_os ( self . get_os ( ) , command) ?;
536
- self . get_logger ( ) . debug ( format ! ( "Output: {:?}" , output) ) ;
537
- Ok ( output)
538
- }
539
-
540
540
fn get_major_version ( & self , full_version : & str ) -> Result < String , Box < dyn Error > > {
541
541
get_index_version ( full_version, 0 )
542
542
}
@@ -683,7 +683,7 @@ pub trait SeleniumManager {
683
683
escaped_path = run_shell_command (
684
684
"bash" ,
685
685
"-c" ,
686
- format_one_arg ( ESCAPE_COMMAND , escaped_path. as_str ( ) ) ,
686
+ vec ! [ format_one_arg( ESCAPE_COMMAND , escaped_path. as_str( ) ) . as_str ( ) ] ,
687
687
)
688
688
. unwrap_or_default ( ) ;
689
689
if escaped_path. is_empty ( ) {
@@ -844,29 +844,6 @@ pub fn create_http_client(timeout: u64, proxy: &str) -> Result<Client, Box<dyn E
844
844
Ok ( client_builder. build ( ) . unwrap_or_default ( ) )
845
845
}
846
846
847
- pub fn run_shell_command (
848
- shell : & str ,
849
- flag : & str ,
850
- command : String ,
851
- ) -> Result < String , Box < dyn Error > > {
852
- let output = Command :: new ( shell)
853
- . args ( [ flag, command. as_str ( ) ] )
854
- . output ( ) ?;
855
- Ok (
856
- strip_trailing_newline ( String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) . as_str ( ) )
857
- . to_string ( ) ,
858
- )
859
- }
860
-
861
- pub fn run_shell_command_by_os ( os : & str , command : String ) -> Result < String , Box < dyn Error > > {
862
- let ( shell, flag) = if WINDOWS . is ( os) {
863
- ( "cmd" , "/c" )
864
- } else {
865
- ( "sh" , "-c" )
866
- } ;
867
- run_shell_command ( shell, flag, command)
868
- }
869
-
870
847
pub fn format_one_arg ( string : & str , arg1 : & str ) -> String {
871
848
string. replacen ( "{}" , arg1, 1 )
872
849
}
@@ -893,18 +870,3 @@ fn get_index_version(full_version: &str, index: usize) -> Result<String, Box<dyn
893
870
. ok_or ( format ! ( "Wrong version: {}" , full_version) ) ?
894
871
. to_string ( ) )
895
872
}
896
-
897
- fn strip_trailing_newline ( input : & str ) -> & str {
898
- input
899
- . strip_suffix ( CRLF )
900
- . or_else ( || input. strip_suffix ( LF ) )
901
- . unwrap_or ( input)
902
- }
903
-
904
- fn split_lines ( string : & str ) -> Vec < & str > {
905
- if string. contains ( CRLF ) {
906
- string. split ( CRLF ) . collect ( )
907
- } else {
908
- string. split ( LF ) . collect ( )
909
- }
910
- }
0 commit comments