@@ -16,6 +16,7 @@ use std::{
1616use tracing:: { debug, error, info, warn} ;
1717
1818use crate :: backends:: desktop_file:: * ;
19+ use crate :: backends:: distrobox:: command:: { CmdFactory , default_cmd_factory} ;
1920
2021const POSIX_FIND_AND_CONCAT_DESKTOP_FILES : & str =
2122 include_str ! ( "POSIX_FIND_AND_CONCAT_DESKTOP_FILES.sh" ) ;
@@ -101,6 +102,7 @@ impl DesktopFiles {
101102
102103pub struct Distrobox {
103104 cmd_runner : CommandRunner ,
105+ cmd_factory : CmdFactory ,
104106}
105107
106108#[ derive( Clone , Debug , PartialEq , Hash ) ]
@@ -427,13 +429,13 @@ impl DistroboxCommandRunnerResponse {
427429 }
428430
429431 fn build_version_response ( ) -> ( Command , String ) {
430- let mut cmd = Command :: new ( "distrobox" ) ;
432+ let mut cmd = default_cmd_factory ( ) ( ) ;
431433 cmd. arg ( "version" ) ;
432434 ( cmd, "distrobox: 1.7.2.1" . to_string ( ) )
433435 }
434436
435437 fn build_no_version_response ( ) -> ( Command , Rc < dyn Fn ( ) -> io:: Result < String > > ) {
436- let mut cmd = Command :: new ( "distrobox" ) ;
438+ let mut cmd = default_cmd_factory ( ) ( ) ;
437439 cmd. arg ( "version" ) ;
438440 ( cmd, Rc :: new ( || Err ( io:: Error :: from_raw_os_error ( 0 ) ) ) )
439441 }
@@ -451,14 +453,14 @@ impl DistroboxCommandRunnerResponse {
451453 output. push_str ( & container. image ) ;
452454 output. push ( '\n' ) ;
453455 }
454- let mut cmd = Command :: new ( "distrobox" ) ;
456+ let mut cmd = default_cmd_factory ( ) ( ) ;
455457 cmd. arg ( "ls" ) . arg ( "--no-color" ) ;
456458 ( cmd, output. clone ( ) )
457459 }
458460
459461 fn build_compatibility_response ( images : & [ String ] ) -> ( Command , String ) {
460462 let output = images. join ( "\n " ) ;
461- let mut cmd = Command :: new ( "distrobox" ) ;
463+ let mut cmd = default_cmd_factory ( ) ( ) ;
462464 cmd. arg ( "create" ) . arg ( "--compatibility" ) ;
463465 ( cmd, output)
464466 }
@@ -517,20 +519,16 @@ impl DistroboxCommandRunnerResponse {
517519
518520 toml. push_str ( "[user]\n " ) ;
519521
520- commands. push ( (
521- Command :: new_with_args (
522- "distrobox" ,
523- [
524- "enter" ,
525- box_name,
526- "--" ,
527- "sh" ,
528- "-c" ,
529- POSIX_FIND_AND_CONCAT_DESKTOP_FILES ,
530- ] ,
531- ) ,
532- toml,
533- ) ) ;
522+ let mut db_cmd = default_cmd_factory ( ) ( ) ;
523+ db_cmd. args ( [
524+ "enter" ,
525+ box_name,
526+ "--" ,
527+ "sh" ,
528+ "-c" ,
529+ POSIX_FIND_AND_CONCAT_DESKTOP_FILES ,
530+ ] ) ;
531+ commands. push ( ( db_cmd, toml) ) ;
534532
535533 commands
536534 }
@@ -565,12 +563,16 @@ impl DistroboxCommandRunnerResponse {
565563}
566564
567565impl Distrobox {
568- pub fn new ( cmd_runner : CommandRunner ) -> Self {
569- Self { cmd_runner }
566+ // The command factory ensures we can customize the distrobox executable path, e.g. to use a bundled version.
567+ pub fn new ( cmd_runner : CommandRunner , cmd_factory : CmdFactory ) -> Self {
568+ Self {
569+ cmd_runner,
570+ cmd_factory,
571+ }
570572 }
571573
572574 fn dbcmd ( & self ) -> Command {
573- Command :: new ( "distrobox" )
575+ ( self . cmd_factory ) ( )
574576 }
575577
576578 pub fn null_command_runner ( responses : & [ DistroboxCommandRunnerResponse ] ) -> CommandRunner {
@@ -1083,7 +1085,7 @@ impl Distrobox {
10831085
10841086impl Default for Distrobox {
10851087 fn default ( ) -> Self {
1086- Self :: new ( CommandRunner :: new_null ( ) )
1088+ Self :: new ( CommandRunner :: new_null ( ) , default_cmd_factory ( ) )
10871089 }
10881090}
10891091
@@ -1122,6 +1124,7 @@ d24405b14180 | ubuntu | Created | ghcr.io/ublue-os/ubun
11221124 NullCommandRunnerBuilder :: new ( )
11231125 . cmd ( & [ "distrobox" , "ls" , "--no-color" ] , output)
11241126 . build ( ) ,
1127+ default_cmd_factory ( )
11251128 ) ;
11261129 assert_eq ! (
11271130 db. list( ) . await ?,
@@ -1147,6 +1150,8 @@ d24405b14180 | ubuntu | Created | ghcr.io/ublue-os/ubun
11471150 NullCommandRunnerBuilder :: new ( )
11481151 . cmd ( & [ "distrobox" , "version" ] , output)
11491152 . build ( ) ,
1153+ default_cmd_factory ( )
1154+
11501155 ) ;
11511156 assert_eq ! ( db. version( ) . await ?, "1.7.2.1" . to_string( ) , ) ;
11521157 Ok ( ( ) )
@@ -1201,6 +1206,8 @@ Categories=Utility;Network;";
12011206 & desktop_files_toml,
12021207 )
12031208 . build ( ) ,
1209+ default_cmd_factory ( )
1210+
12041211 ) ;
12051212
12061213 let apps = block_on ( db. list_apps ( "ubuntu" ) ) ?;
@@ -1253,6 +1260,8 @@ Categories=Utility;Security;";
12531260 & desktop_files_toml,
12541261 )
12551262 . build ( ) ,
1263+ default_cmd_factory ( )
1264+
12561265 ) ;
12571266
12581267 let apps = block_on ( db. list_apps ( "ubuntu" ) ) ?;
@@ -1270,7 +1279,7 @@ Categories=Utility;Security;";
12701279 #[ test]
12711280 fn create ( ) -> Result < ( ) , Error > {
12721281 let _ = tracing_subscriber:: fmt ( ) . with_test_writer ( ) . try_init ( ) ;
1273- let db = Distrobox :: new ( CommandRunner :: new_null ( ) ) ;
1282+ let db = Distrobox :: new ( CommandRunner :: new_null ( ) , default_cmd_factory ( ) ) ;
12741283 let output_tracker = db. cmd_runner . output_tracker ( ) ;
12751284 debug ! ( "Testing container creation" ) ;
12761285 let args = CreateArgs {
@@ -1294,7 +1303,7 @@ Categories=Utility;Security;";
12941303 }
12951304 #[ test]
12961305 fn assemble ( ) -> Result < ( ) , Error > {
1297- let db = Distrobox :: new ( CommandRunner :: new_null ( ) ) ;
1306+ let db = Distrobox :: new ( CommandRunner :: new_null ( ) , default_cmd_factory ( ) ) ;
12981307 let output_tracker = db. cmd_runner . output_tracker ( ) ;
12991308 db. assemble ( "/path/to/assemble.yml" ) ?;
13001309 assert_eq ! (
@@ -1306,7 +1315,7 @@ Categories=Utility;Security;";
13061315
13071316 #[ test]
13081317 fn remove ( ) -> Result < ( ) , Error > {
1309- let db = Distrobox :: new ( CommandRunner :: new_null ( ) ) ;
1318+ let db = Distrobox :: new ( CommandRunner :: new_null ( ) , default_cmd_factory ( ) ) ;
13101319 let output_tracker = db. cmd_runner . output_tracker ( ) ;
13111320 block_on ( db. remove ( "ubuntu" ) ) ?;
13121321 assert_eq ! (
0 commit comments