Skip to content

Commit b060a86

Browse files
committed
Refactor, introduce faker module
1 parent ca66d31 commit b060a86

File tree

13 files changed

+114
-110
lines changed

13 files changed

+114
-110
lines changed

src/application.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ use gettextrs::gettext;
2727
use gtk::{gio, glib};
2828

2929
use crate::config::VERSION;
30-
use crate::distrobox::{
31-
CommandRunner, Distrobox, DistroboxCommandRunnerResponse
32-
};
30+
use crate::distrobox::{Distrobox, DistroboxCommandRunnerResponse, FlatpakCommandRunner};
31+
use crate::fakers::{CommandRunner, RealCommandRunner};
3332
use crate::root_store::RootStore;
3433
use crate::DistroShelfWindow;
3534

@@ -223,7 +222,9 @@ impl DistroShelfApplication {
223222
}
224223
_ => {
225224
if Self::get_is_in_flatpak() {
226-
CommandRunner::new_flatpak_real()
225+
CommandRunner::new(Rc::new(FlatpakCommandRunner::new(Rc::new(
226+
RealCommandRunner::new(),
227+
))))
227228
} else {
228229
CommandRunner::new_real()
229230
}

src/container.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::{
2-
distrobox::{Command, ContainerInfo, ExportableApp, Status},
2+
distrobox::{ContainerInfo, ExportableApp, Status},
33
distrobox_task::DistroboxTask,
44
known_distros::{known_distro_by_image, KnownDistro},
55
remote_resource::RemoteResource,
66
root_store::RootStore,
7+
fakers::Command
78
};
89

910
use gtk::{

src/distrobox/mod.rs

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,46 @@
11
use std::{
2-
cell::{LazyCell, RefCell}, collections::BTreeMap, io, path::{Path, PathBuf}, process::Output, rc::Rc, str::FromStr
2+
cell::LazyCell, collections::BTreeMap, future::Future, io, path::{Path, PathBuf}, pin::Pin, process::Output, rc::Rc, str::FromStr
33
};
44
use tracing::{debug, error, info, warn};
5+
use crate::fakers::{CommandRunner, NullCommandRunnerBuilder, Command, Child, InnerCommandRunner, FdMode};
56

6-
mod command;
7-
mod command_runner;
87
mod desktop_file;
98

10-
pub use command::*;
11-
pub use command_runner::*;
129
pub use desktop_file::*;
1310

14-
#[derive(Default, Clone, Debug)]
15-
pub struct OutputTracker<T> {
16-
store: Rc<RefCell<Option<Vec<T>>>>,
17-
}
1811

19-
impl<T> OutputTracker<T> {
20-
pub fn new() -> Self {
21-
OutputTracker {
22-
store: Rc::new(RefCell::new(None)),
23-
}
12+
#[derive(Clone)]
13+
pub struct FlatpakCommandRunner {
14+
pub command_runner: Rc<dyn InnerCommandRunner>,
15+
}
16+
impl FlatpakCommandRunner {
17+
pub fn new(command_runner: Rc<dyn InnerCommandRunner>) -> Self {
18+
FlatpakCommandRunner { command_runner }
2419
}
25-
pub fn len(&self) -> usize {
26-
if let Some(v) = &*self.store.borrow() {
27-
v.len()
28-
} else {
29-
0
30-
}
20+
21+
pub fn wrap_flatpak_cmd(mut prev: Command) -> Command {
22+
let mut args = vec!["--host".into(), prev.program];
23+
args.extend(prev.args);
24+
25+
prev.args = args;
26+
prev.program = "flatpak-spawn".into();
27+
prev
3128
}
3229
}
3330

34-
impl<T: Clone + std::fmt::Debug> OutputTracker<T> {
35-
pub fn enable(&self) {
36-
let mut inner = self.store.borrow_mut();
37-
if inner.is_none() {
38-
*inner = Some(vec![]);
39-
}
40-
}
41-
pub fn push(&self, item: T) {
42-
if let Some(v) = &mut *self.store.borrow_mut() {
43-
v.push(item);
44-
}
31+
impl InnerCommandRunner for FlatpakCommandRunner {
32+
fn spawn(&self, command: Command) -> io::Result<Box<dyn Child + Send>> {
33+
self.command_runner.spawn(Self::wrap_flatpak_cmd(command))
4534
}
46-
pub fn items(&self) -> Vec<T> {
47-
if let Some(v) = &*self.store.borrow() {
48-
v.clone()
49-
} else {
50-
vec![]
51-
}
35+
fn output(
36+
&self,
37+
command: Command,
38+
) -> Pin<Box<dyn Future<Output = io::Result<std::process::Output>>>> {
39+
self.command_runner.output(Self::wrap_flatpak_cmd(command))
5240
}
5341
}
5442

43+
5544
pub struct Distrobox {
5645
cmd_runner: CommandRunner,
5746
}
@@ -497,7 +486,8 @@ impl Distrobox {
497486
}
498487

499488
pub fn cmd_spawn(&self, mut cmd: Command) -> Result<Box<dyn Child + Send>, Error> {
500-
wrap_capture_cmd(&mut cmd);
489+
cmd.stdout = FdMode::Pipe;
490+
cmd.stderr = FdMode::Pipe;
501491

502492
let program = cmd.program.to_string_lossy().to_string();
503493
let args = cmd
@@ -520,7 +510,8 @@ impl Distrobox {
520510
}
521511

522512
async fn cmd_output(&self, mut cmd: Command) -> Result<Output, Error> {
523-
wrap_capture_cmd(&mut cmd);
513+
cmd.stdout = FdMode::Pipe;
514+
cmd.stderr = FdMode::Pipe;
524515

525516
let program = cmd.program.to_string_lossy().to_string();
526517
let args = cmd

src/distrobox_task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::cell::RefCell;
1111
use std::future::Future;
1212
use tracing::{debug, error, info, warn};
1313

14-
use crate::distrobox::Child;
14+
use crate::fakers::Child;
1515

1616
mod imp {
1717
use super::*;
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,3 @@ impl From<Command> for async_process::Command {
107107
cmd
108108
}
109109
}
110-
111-
pub fn wrap_flatpak_cmd(mut prev: Command) -> Command {
112-
let mut args = vec!["--host".into(), prev.program];
113-
args.extend(prev.args);
114-
115-
prev.args = args;
116-
prev.program = "flatpak-spawn".into();
117-
prev
118-
}
119-
120-
pub fn wrap_capture_cmd(cmd: &mut Command) -> &mut Command {
121-
cmd.stdout = FdMode::Pipe;
122-
cmd.stderr = FdMode::Pipe;
123-
cmd
124-
}
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ use std::{
55
collections::HashMap, future::Future, io::{self}, os::unix::process::ExitStatusExt, pin::Pin, process::ExitStatus, rc::Rc
66
};
77

8-
use crate::distrobox::{Command, OutputTracker};
8+
use crate::fakers::{OutputTracker, Command};
9+
910
use async_process::{Command as AsyncCommand, Output};
1011
use futures::{
1112
io::{AsyncRead, AsyncWrite, Cursor},
1213
FutureExt,
1314
};
1415

15-
use super::wrap_flatpak_cmd;
16-
1716

1817
#[derive(Debug, Clone)]
1918
pub enum CommandRunnerEvent {
@@ -59,11 +58,6 @@ impl CommandRunner {
5958
pub fn new_real() -> Self {
6059
CommandRunner::new(Rc::new(RealCommandRunner {}))
6160
}
62-
pub fn new_flatpak_real() -> Self {
63-
CommandRunner::new(Rc::new(FlatpakCommandRunner::new(
64-
Rc::new(RealCommandRunner {}),
65-
)))
66-
}
6761

6862
pub fn output_tracker(&self) -> OutputTracker<CommandRunnerEvent> {
6963
self.output_tracker.enable();
@@ -117,6 +111,11 @@ pub trait InnerCommandRunner {
117111

118112
#[derive(Clone, Debug)]
119113
pub struct RealCommandRunner {}
114+
impl RealCommandRunner {
115+
pub fn new() -> Self {
116+
RealCommandRunner {}
117+
}
118+
}
120119

121120
impl InnerCommandRunner for RealCommandRunner {
122121
fn spawn(&self, command: Command) -> io::Result<Box<dyn Child + Send>> {
@@ -132,27 +131,6 @@ impl InnerCommandRunner for RealCommandRunner {
132131
}
133132
}
134133

135-
#[derive(Clone)]
136-
pub struct FlatpakCommandRunner {
137-
pub command_runner: Rc<dyn InnerCommandRunner>,
138-
}
139-
impl FlatpakCommandRunner {
140-
pub fn new(command_runner: Rc<dyn InnerCommandRunner>) -> Self {
141-
FlatpakCommandRunner { command_runner }
142-
}
143-
}
144-
145-
impl InnerCommandRunner for FlatpakCommandRunner {
146-
fn spawn(&self, command: Command) -> io::Result<Box<dyn Child + Send>> {
147-
self.command_runner.spawn(wrap_flatpak_cmd(command))
148-
}
149-
fn output(
150-
&self,
151-
command: Command,
152-
) -> Pin<Box<dyn Future<Output = io::Result<std::process::Output>>>> {
153-
self.command_runner.output(wrap_flatpak_cmd(command))
154-
}
155-
}
156134

157135

158136
#[derive(Default, Clone)]

src/fakers/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mod command_runner;
2+
mod output_tracker;
3+
mod command;
4+
5+
pub use command::{Command, FdMode};
6+
pub use output_tracker::OutputTracker;
7+
pub use command_runner::{
8+
CommandRunner, RealCommandRunner, NullCommandRunnerBuilder, Child, InnerCommandRunner,
9+
CommandRunnerEvent
10+
};

src/fakers/output_tracker.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::{cell::RefCell, rc::Rc};
2+
3+
#[derive(Default, Clone, Debug)]
4+
pub struct OutputTracker<T> {
5+
store: Rc<RefCell<Option<Vec<T>>>>,
6+
}
7+
8+
impl<T> OutputTracker<T> {
9+
pub fn new() -> Self {
10+
OutputTracker {
11+
store: Rc::new(RefCell::new(None)),
12+
}
13+
}
14+
pub fn len(&self) -> usize {
15+
if let Some(v) = &*self.store.borrow() {
16+
v.len()
17+
} else {
18+
0
19+
}
20+
}
21+
}
22+
23+
impl<T: Clone + std::fmt::Debug> OutputTracker<T> {
24+
pub fn enable(&self) {
25+
let mut inner = self.store.borrow_mut();
26+
if inner.is_none() {
27+
*inner = Some(vec![]);
28+
}
29+
}
30+
pub fn push(&self, item: T) {
31+
if let Some(v) = &mut *self.store.borrow_mut() {
32+
v.push(item);
33+
}
34+
}
35+
pub fn items(&self) -> Vec<T> {
36+
if let Some(v) = &*self.store.borrow() {
37+
v.clone()
38+
} else {
39+
vec![]
40+
}
41+
}
42+
}

src/known_distros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::distrobox::Command;
21
use glib::subclass::prelude::*;
32
use gtk::glib;
43
use gtk::glib::derived_properties;
@@ -9,6 +8,8 @@ use std::cell::RefCell;
98
use std::collections::HashMap;
109
use std::path::Path;
1110

11+
use crate::fakers::Command;
12+
1213

1314
pub const DISTROS: LazyCell<
1415
HashMap<String, KnownDistro>,

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod tasks_button;
4040
mod terminal_combo_row;
4141
mod welcome_view;
4242
mod window;
43+
mod fakers;
4344
pub use store::root_store;
4445

4546
use self::application::DistroShelfApplication;

0 commit comments

Comments
 (0)