Skip to content

Commit

Permalink
add stopping and starting function for toolboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
13hannes11 committed May 30, 2024
1 parent d98f272 commit 8b3ca5c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 65 deletions.
51 changes: 46 additions & 5 deletions src/factories/container_list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::util::toolbox::start_toolbox_container;
use crate::util::toolbox::stop_toolbox_container;
use gtk::prelude::ButtonExt;
use relm4::adw;
use relm4::adw::prelude::ActionRowExt;
Expand All @@ -6,11 +8,11 @@ use relm4::factory::{FactoryComponent, FactorySender};
use relm4::gtk;
use relm4::gtk::prelude::WidgetExt;
use relm4_icons::icon_names;

#[derive(Debug, PartialEq)]
pub enum ContainerStatus {
Running,
NotRunning,
Refreshing,
}

#[derive(Debug)]
Expand All @@ -27,6 +29,12 @@ pub enum ContainerMsg {
OpenTerminal,
}

#[derive(Debug)]
pub enum CommandMessage {
SetStarted,
SetStopped,
}

pub struct ContainerInit {
pub name: String,
pub status: ContainerStatus,
Expand All @@ -37,7 +45,7 @@ impl FactoryComponent for Container {
type Init = ContainerInit;
type Input = ContainerMsg;
type Output = ();
type CommandOutput = ();
type CommandOutput = CommandMessage;
type Widgets = ContainerWidgets;
type ParentWidget = gtk::ListBox;
type Index = String;
Expand All @@ -53,6 +61,18 @@ impl FactoryComponent for Container {
gtk::AspectFrame{
set_ratio: 1.0,
gtk::Box{
gtk::Button {
#[watch]
set_visible: self.status == ContainerStatus::Refreshing,
#[wrap(Some)]
set_child = &gtk::Spinner {
#[watch]
set_spinning: self.status == ContainerStatus::Refreshing,
},
set_margin_top: 10,
set_margin_bottom: 10,
set_css_classes: &["circular"],
},
gtk::Button {
#[watch]
set_visible: self.status == ContainerStatus::NotRunning,
Expand Down Expand Up @@ -105,11 +125,32 @@ impl FactoryComponent for Container {
}
}

fn update(&mut self, msg: Self::Input, _sender: FactorySender<Self>) {
fn update(&mut self, msg: Self::Input, sender: FactorySender<Self>) {
match msg {
ContainerMsg::Start => {}
ContainerMsg::Stop => {}
ContainerMsg::Start => {
self.status = ContainerStatus::Refreshing;
let hash = (&self.hash).clone();
sender.spawn_oneshot_command(move || match start_toolbox_container(&hash) {
Ok(_) => CommandMessage::SetStarted,
Err(_) => CommandMessage::SetStopped,
});
}
ContainerMsg::Stop => {
self.status = ContainerStatus::Refreshing;
let hash = (&self.hash).clone();
sender.spawn_oneshot_command(move || match stop_toolbox_container(&hash) {
Ok(_) => CommandMessage::SetStopped,
Err(_) => CommandMessage::SetStarted,
});
}
ContainerMsg::OpenTerminal => {}
}
}

fn update_cmd(&mut self, message: Self::CommandOutput, sender: FactorySender<Self>) {
match message {
CommandMessage::SetStarted => self.status = ContainerStatus::Running,
CommandMessage::SetStopped => self.status = ContainerStatus::NotRunning,
};
}
}
118 changes: 58 additions & 60 deletions src/util/toolbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,71 +147,69 @@ impl ToolbxContainer {
self.status = ToolbxStatus::from_str(inspect_result.state.status.as_str())?;
Ok(())
}
}

pub fn stop(&mut self) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman")
.arg("podman")
.arg("stop")
.arg(self.name.clone())
.output();
pub fn stop_toolbox_container(hash: &str) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman")
.arg("podman")
.arg("stop")
.arg(hash)
.output();

if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();

if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();

// Success: Output { status: ExitStatus(unix_wait_status(0)), stdout: "tbx_name\n", stderr: "" }
//Fail:
// Output {
// status: ExitStatus(unix_wait_status(32000)),
// stdout: "",
// stderr: "Error: no container with name or ID \"tbx_name\" found: no such container\n"
// }

if output.status.code() == Some(0) {
self.status = ToolbxStatus::Exited;
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
// Success: Output { status: ExitStatus(unix_wait_status(0)), stdout: "tbx_name\n", stderr: "" }
//Fail:
// Output {
// status: ExitStatus(unix_wait_status(32000)),
// stdout: "",
// stderr: "Error: no container with name or ID \"tbx_name\" found: no such container\n"
// }

if output.status.code() == Some(0) {
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
}

pub fn start(&mut self) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman")
.arg("podman")
.arg("start")
.arg(self.name.clone())
.output();
pub fn start_toolbox_container(hash: &str) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman")
.arg("podman")
.arg("start")
.arg(hash)
.output();

if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();

if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();

// Success: status: Output { ExitStatus(unix_wait_status(0)), stdout: "tbx_name\n", stderr: "" }
// Fail: status:
// Output {
// status: ExitStatus(unix_wait_status(32000)),
// stdout: "",
// stderr: "Error: no container with name or ID \"tbx_name\" found: no such container\n"
// }

if output.status.code() == Some(0) {
self.status = ToolbxStatus::Running;
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
// Success: status: Output { ExitStatus(unix_wait_status(0)), stdout: "tbx_name\n", stderr: "" }
// Fail: status:
// Output {
// status: ExitStatus(unix_wait_status(32000)),
// stdout: "",
// stderr: "Error: no container with name or ID \"tbx_name\" found: no such container\n"
// }

if output.status.code() == Some(0) {
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
}

Expand Down

0 comments on commit 8b3ca5c

Please sign in to comment.