add stopping and starting function for toolboxes

This commit is contained in:
2024-05-30 18:45:50 +02:00
parent d98f27229d
commit 8b3ca5cad3
2 changed files with 102 additions and 63 deletions

View File

@@ -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;
@@ -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)]
@@ -27,6 +29,12 @@ pub enum ContainerMsg {
OpenTerminal,
}
#[derive(Debug)]
pub enum CommandMessage {
SetStarted,
SetStopped,
}
pub struct ContainerInit {
pub name: String,
pub status: ContainerStatus,
@@ -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;
@@ -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,
@@ -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,
};
}
}

View File

@@ -147,13 +147,14 @@ impl ToolbxContainer {
self.status = ToolbxStatus::from_str(inspect_result.state.status.as_str())?;
Ok(())
}
}
pub fn stop(&mut self) -> Result<(), ToolbxError> {
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(self.name.clone())
.arg(hash)
.output();
if output.is_err() {
@@ -172,7 +173,6 @@ impl ToolbxContainer {
// }
if output.status.code() == Some(0) {
self.status = ToolbxStatus::Exited;
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
@@ -181,12 +181,12 @@ impl ToolbxContainer {
}
}
pub fn start(&mut self) -> Result<(), ToolbxError> {
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(self.name.clone())
.arg(hash)
.output();
if output.is_err() {
@@ -205,7 +205,6 @@ impl ToolbxContainer {
// }
if output.status.code() == Some(0) {
self.status = ToolbxStatus::Running;
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
@@ -213,7 +212,6 @@ impl ToolbxContainer {
))
}
}
}
#[test]
fn test_start_1non_existing_container() {