mirror of
https://github.com/13hannes11/toolbx-tuner.git
synced 2024-09-03 23:21:00 +02:00
add populate factory using toolboxes on machine
This commit is contained in:
14
Cargo.lock
generated
14
Cargo.lock
generated
@@ -1032,18 +1032,18 @@ checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.197"
|
version = "1.0.199"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
|
checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.197"
|
version = "1.0.199"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
|
checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -1052,9 +1052,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.114"
|
version = "1.0.116"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0"
|
checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"ryu",
|
"ryu",
|
||||||
@@ -1260,6 +1260,8 @@ dependencies = [
|
|||||||
"gettext-rs",
|
"gettext-rs",
|
||||||
"relm4",
|
"relm4",
|
||||||
"relm4-icons",
|
"relm4-icons",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -14,3 +14,5 @@ tracing = "0.1"
|
|||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
relm4 = { version = "0.8.1", features = ["libadwaita", "gnome_45"] }
|
relm4 = { version = "0.8.1", features = ["libadwaita", "gnome_45"] }
|
||||||
relm4-icons = { version = "0.8.1" }
|
relm4-icons = { version = "0.8.1" }
|
||||||
|
serde = { version = "1.0.199", features = ["derive"] }
|
||||||
|
serde_json = "1.0.116"
|
||||||
|
|||||||
@@ -9,13 +9,14 @@
|
|||||||
],
|
],
|
||||||
"command": "toolbox-tuner",
|
"command": "toolbox-tuner",
|
||||||
"finish-args": [
|
"finish-args": [
|
||||||
"--share=ipc",
|
"--talk-name=org.freedesktop.Flatpak",
|
||||||
"--socket=fallback-x11",
|
"--socket=fallback-x11",
|
||||||
"--socket=wayland",
|
"--socket=wayland",
|
||||||
"--device=dri",
|
"--device=dri",
|
||||||
"--env=RUST_LOG=toolbox_tuner=debug",
|
"--env=RUST_LOG=toolbxtuner=debug",
|
||||||
"--env=G_MESSAGES_DEBUG=none",
|
"--env=G_MESSAGES_DEBUG=none",
|
||||||
"--env=RUST_BACKTRACE=1"
|
"--env=RUST_BACKTRACE=1",
|
||||||
|
"--share=ipc"
|
||||||
],
|
],
|
||||||
"build-options": {
|
"build-options": {
|
||||||
"append-path": "/usr/lib/sdk/rust-stable/bin:/usr/lib/sdk/llvm15/bin",
|
"append-path": "/usr/lib/sdk/rust-stable/bin:/usr/lib/sdk/llvm15/bin",
|
||||||
|
|||||||
25
src/app.rs
25
src/app.rs
@@ -1,4 +1,5 @@
|
|||||||
use crate::gtk::Align;
|
use crate::gtk::Align;
|
||||||
|
use crate::util::toolbox::ToolbxContainer;
|
||||||
use relm4::factory::FactoryHashMap;
|
use relm4::factory::FactoryHashMap;
|
||||||
use relm4::gtk::PolicyType;
|
use relm4::gtk::PolicyType;
|
||||||
use relm4::RelmWidgetExt;
|
use relm4::RelmWidgetExt;
|
||||||
@@ -39,6 +40,7 @@ relm4::new_action_group!(pub(super) WindowActionGroup, "win");
|
|||||||
//relm4::new_stateless_action!(PreferencesAction, WindowActionGroup, "preferences");
|
//relm4::new_stateless_action!(PreferencesAction, WindowActionGroup, "preferences");
|
||||||
relm4::new_stateless_action!(pub(super) ShortcutsAction, WindowActionGroup, "show-help-overlay");
|
relm4::new_stateless_action!(pub(super) ShortcutsAction, WindowActionGroup, "show-help-overlay");
|
||||||
relm4::new_stateless_action!(AboutAction, WindowActionGroup, "about");
|
relm4::new_stateless_action!(AboutAction, WindowActionGroup, "about");
|
||||||
|
use crate::factories::container_list::ContainerInit;
|
||||||
|
|
||||||
#[relm4::component(pub)]
|
#[relm4::component(pub)]
|
||||||
impl Component for App {
|
impl Component for App {
|
||||||
@@ -130,20 +132,17 @@ impl Component for App {
|
|||||||
UnsupportedDialogOutput::CloseApplication => AppMsg::Quit,
|
UnsupportedDialogOutput::CloseApplication => AppMsg::Quit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let toolboxes = ToolbxContainer::get_toolboxes();
|
||||||
|
|
||||||
let mut containers = FactoryHashMap::builder().launch_default().detach();
|
let mut containers = FactoryHashMap::builder().launch_default().detach();
|
||||||
containers.insert("123".to_string(), 2);
|
toolboxes.iter().for_each(|toolbox| {
|
||||||
containers.insert("abc".to_string(), 3);
|
&containers.insert(
|
||||||
containers.insert("45435".to_string(), 3);
|
toolbox.id.clone(),
|
||||||
containers.insert("1dsal;k1;23".to_string(), 3);
|
ContainerInit {
|
||||||
containers.insert("afdsaf".to_string(), 3);
|
name: toolbox.name.clone(),
|
||||||
containers.insert("5344".to_string(), 3);
|
},
|
||||||
containers.insert("1242344".to_string(), 3);
|
);
|
||||||
containers.insert("1265464".to_string(), 3);
|
});
|
||||||
containers.insert(
|
|
||||||
"126222222222222222222222222222222222222222233333333333333333333333333333333325464"
|
|
||||||
.to_string(),
|
|
||||||
3,
|
|
||||||
);
|
|
||||||
|
|
||||||
let model = Self {
|
let model = Self {
|
||||||
about_dialog,
|
about_dialog,
|
||||||
|
|||||||
@@ -10,17 +10,23 @@ use relm4_icons::icon_names;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Container {
|
pub struct Container {
|
||||||
hash: String,
|
hash: String,
|
||||||
value: u8,
|
value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ContainerMsg {
|
pub enum ContainerMsg {
|
||||||
Start,
|
Start,
|
||||||
|
Stop,
|
||||||
|
OpenTerminal,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ContainerInit {
|
||||||
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[relm4::factory(pub)]
|
#[relm4::factory(pub)]
|
||||||
impl FactoryComponent for Container {
|
impl FactoryComponent for Container {
|
||||||
type Init = u8;
|
type Init = ContainerInit;
|
||||||
type Input = ContainerMsg;
|
type Input = ContainerMsg;
|
||||||
type Output = ();
|
type Output = ();
|
||||||
type CommandOutput = ();
|
type CommandOutput = ();
|
||||||
@@ -31,7 +37,8 @@ impl FactoryComponent for Container {
|
|||||||
view! {
|
view! {
|
||||||
root = adw::ActionRow {
|
root = adw::ActionRow {
|
||||||
#[watch]
|
#[watch]
|
||||||
set_title: format!{"{}: {}", self.hash, self.value.to_string()}.as_str(),
|
set_title: &self.value,
|
||||||
|
set_subtitle: &self.hash,
|
||||||
|
|
||||||
add_prefix = >k::Box{
|
add_prefix = >k::Box{
|
||||||
gtk::AspectFrame{
|
gtk::AspectFrame{
|
||||||
@@ -58,7 +65,7 @@ impl FactoryComponent for Container {
|
|||||||
set_margin_top: 10,
|
set_margin_top: 10,
|
||||||
set_margin_bottom: 10,
|
set_margin_bottom: 10,
|
||||||
set_css_classes: &["flat"],
|
set_css_classes: &["flat"],
|
||||||
connect_clicked => ContainerMsg::Start,
|
connect_clicked => ContainerMsg::OpenTerminal,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -68,15 +75,15 @@ impl FactoryComponent for Container {
|
|||||||
fn init_model(value: Self::Init, index: &Self::Index, _sender: FactorySender<Self>) -> Self {
|
fn init_model(value: Self::Init, index: &Self::Index, _sender: FactorySender<Self>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
hash: index.clone(),
|
hash: index.clone(),
|
||||||
value,
|
value: value.name.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, msg: Self::Input, _sender: FactorySender<Self>) {
|
fn update(&mut self, msg: Self::Input, _sender: FactorySender<Self>) {
|
||||||
match msg {
|
match msg {
|
||||||
ContainerMsg::Start => {
|
ContainerMsg::Start => {}
|
||||||
self.value = self.value.wrapping_add(1);
|
ContainerMsg::Stop => {}
|
||||||
}
|
ContainerMsg::OpenTerminal => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ mod app;
|
|||||||
mod config;
|
mod config;
|
||||||
mod factories;
|
mod factories;
|
||||||
mod modals;
|
mod modals;
|
||||||
|
mod util;
|
||||||
|
|
||||||
use crate::config::{APP_ID, GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};
|
use crate::config::{APP_ID, GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};
|
||||||
use gettextrs::{gettext, LocaleCategory};
|
use gettextrs::{gettext, LocaleCategory};
|
||||||
|
|||||||
1
src/util.rs
Normal file
1
src/util.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod toolbox;
|
||||||
427
src/util/toolbox.rs
Normal file
427
src/util/toolbox.rs
Normal file
@@ -0,0 +1,427 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{fmt::Display, iter::zip, process::Command, str::FromStr, string::ParseError, sync::Arc};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum ToolbxError {
|
||||||
|
ParseStatusError(String),
|
||||||
|
JSONSerializationError(String),
|
||||||
|
CommandExecutionError(String),
|
||||||
|
CommandUnsuccessfulError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for ToolbxError {}
|
||||||
|
|
||||||
|
impl Display for ToolbxError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
ToolbxError::ParseStatusError(parse_error) => write!(f, "{}", parse_error),
|
||||||
|
ToolbxError::CommandExecutionError(command_exec_error) => {
|
||||||
|
write!(f, "{}", command_exec_error)
|
||||||
|
}
|
||||||
|
ToolbxError::JSONSerializationError(msg) => {
|
||||||
|
write!(f, "{}", msg)
|
||||||
|
}
|
||||||
|
ToolbxError::CommandUnsuccessfulError(command_unsuc_error) => {
|
||||||
|
write!(f, "{}", command_unsuc_error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum ToolbxStatus {
|
||||||
|
Running,
|
||||||
|
Configured,
|
||||||
|
Created,
|
||||||
|
Exited,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ToolbxStatus {
|
||||||
|
fn default() -> Self {
|
||||||
|
ToolbxStatus::Configured
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for ToolbxStatus {
|
||||||
|
type Err = ToolbxError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<ToolbxStatus, ToolbxError> {
|
||||||
|
match s {
|
||||||
|
"running" => Ok(ToolbxStatus::Running),
|
||||||
|
"configured" => Ok(ToolbxStatus::Configured),
|
||||||
|
"created" => Ok(ToolbxStatus::Created),
|
||||||
|
"exited" => Ok(ToolbxStatus::Exited),
|
||||||
|
s => Err(ToolbxError::ParseStatusError(format!(
|
||||||
|
"'{}' is not a valid toolbx status.",
|
||||||
|
s
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Default, Clone)]
|
||||||
|
pub struct ToolbxContainer {
|
||||||
|
pub id: String,
|
||||||
|
pub name: String,
|
||||||
|
pub created: String,
|
||||||
|
pub status: ToolbxStatus,
|
||||||
|
pub image: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type PodmanInspectArray = Vec<PodmanInspectInfo>;
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct PodmanInspectInfo {
|
||||||
|
#[serde(rename = "Id")]
|
||||||
|
pub id: String,
|
||||||
|
#[serde(rename = "Created")]
|
||||||
|
pub created: String,
|
||||||
|
#[serde(rename = "State")]
|
||||||
|
pub state: PodManInspectState,
|
||||||
|
#[serde(rename = "Image")]
|
||||||
|
pub image: String,
|
||||||
|
#[serde(rename = "ImageName")]
|
||||||
|
pub image_name: String,
|
||||||
|
#[serde(rename = "Name")]
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct PodManInspectState {
|
||||||
|
#[serde(rename = "Status")]
|
||||||
|
pub status: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum ToolboxCreateParameter {
|
||||||
|
None,
|
||||||
|
Distro(String),
|
||||||
|
Image(String),
|
||||||
|
Release(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToolbxContainer {
|
||||||
|
pub fn new(name: String) -> ToolbxContainer {
|
||||||
|
ToolbxContainer {
|
||||||
|
name: name,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(name: String, parameter: ToolboxCreateParameter) {
|
||||||
|
todo!("Implement actual functionality to create toolbox via commandline")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_toolboxes() -> Vec<ToolbxContainer> {
|
||||||
|
let output = run_cmd_toolbx_list_containers();
|
||||||
|
println!("{}", output);
|
||||||
|
parse_cmd_list_containers(output.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_status(output: &str) -> Result<PodmanInspectInfo, ToolbxError> {
|
||||||
|
let result: Result<PodmanInspectArray, _> = serde_json::from_str(output);
|
||||||
|
match result {
|
||||||
|
Ok(inspect_vec) => match inspect_vec.first() {
|
||||||
|
Some(info) => Ok(info.clone()),
|
||||||
|
None => Err(ToolbxError::JSONSerializationError(
|
||||||
|
"Inspect command returned empty vector.".to_string(),
|
||||||
|
)),
|
||||||
|
},
|
||||||
|
Err(e) => Err(ToolbxError::JSONSerializationError(e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_status(&mut self) -> Result<(), ToolbxError> {
|
||||||
|
let output = Command::new("flatpak-spawn")
|
||||||
|
.arg("--host")
|
||||||
|
.arg("podman")
|
||||||
|
.arg("container")
|
||||||
|
.arg("inspect")
|
||||||
|
.arg(self.name.clone())
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
let output = String::from_utf8_lossy(&output.stdout).to_string();
|
||||||
|
let inspect_result = ToolbxContainer::parse_status(output.as_str())?;
|
||||||
|
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();
|
||||||
|
|
||||||
|
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(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_start_1non_existing_container() {
|
||||||
|
// TODO: create container that exists based on simple image
|
||||||
|
// run command
|
||||||
|
// delete container
|
||||||
|
//let tbx = ToolbxContainer{created: "".to_string(), id: "".to_string(), name: "latex".to_string(), image: "".to_string(), status: ToolbxStatus::Exited};
|
||||||
|
|
||||||
|
//tbx.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_inspect_parsing() {
|
||||||
|
let podman_inspect = concat!(
|
||||||
|
"[{",
|
||||||
|
"\"Id\": \"ae05203091ab4cdf047a9aeba6af8a7bed8105f7f59d09a35d2b64c837ecac0d\",",
|
||||||
|
"\"Created\": \"2021-12-10T20:51:43.140418098+01:00\",",
|
||||||
|
"\"State\": {",
|
||||||
|
"\"Status\": \"running\"",
|
||||||
|
"},",
|
||||||
|
"\"Image\": \"ab8bc106d4a710a7a27c538762864610467b3559f80b413d30e0a1bfcfe272a5\",",
|
||||||
|
"\"ImageName\": \"registry.fedoraproject.org/fedora-toolbox:35\",",
|
||||||
|
"\"Name\": \"rust\"",
|
||||||
|
"}]"
|
||||||
|
);
|
||||||
|
let inspect_info = ToolbxContainer::parse_status(podman_inspect).unwrap();
|
||||||
|
assert_eq!("running", inspect_info.state.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_start_non_existing_container() {
|
||||||
|
let name = "zy2lM6BdZoTnKHaVPkUJ".to_string();
|
||||||
|
let mut tbx = ToolbxContainer {
|
||||||
|
created: "".to_string(),
|
||||||
|
id: "".to_string(),
|
||||||
|
name: name.clone(),
|
||||||
|
image: "".to_string(),
|
||||||
|
status: ToolbxStatus::Exited,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Err(ToolbxError::CommandUnsuccessfulError(format!(
|
||||||
|
"Error: no container with name or ID \"{}\" found: no such container\n",
|
||||||
|
name
|
||||||
|
))),
|
||||||
|
tbx.start()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_cmd_toolbx_list_containers() -> String {
|
||||||
|
let output = Command::new("flatpak-spawn")
|
||||||
|
.arg("--host")
|
||||||
|
.arg("toolbox")
|
||||||
|
.arg("list")
|
||||||
|
.arg("--containers")
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
println!("{:?}", String::from_utf8_lossy(&output.stdout).to_string());
|
||||||
|
|
||||||
|
String::from_utf8_lossy(&output.stdout).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn test_cmd_list_containers() {
|
||||||
|
// This requires toolbx to be installed
|
||||||
|
let toolbox_cmd_container_header =
|
||||||
|
"CONTAINER ID CONTAINER NAME CREATED STATUS IMAGE NAME";
|
||||||
|
assert!(run_cmd_toolbx_list_containers().starts_with(toolbox_cmd_container_header));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize_line_list_containers(line: &str) -> Vec<String> {
|
||||||
|
let mut tokens = Vec::<String>::new();
|
||||||
|
let mut current_token = Vec::<char>::new();
|
||||||
|
|
||||||
|
let mut whitespace_section = false;
|
||||||
|
|
||||||
|
let mut iter = line.chars().peekable();
|
||||||
|
while let Some(&c) = iter.peek() {
|
||||||
|
match (whitespace_section, c) {
|
||||||
|
(false, ' ') => {
|
||||||
|
iter.next();
|
||||||
|
if Some(' ') == iter.peek().map(|x| x.clone()) {
|
||||||
|
whitespace_section = true;
|
||||||
|
} else {
|
||||||
|
current_token.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(true, ' ') => {
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
(true, c) => {
|
||||||
|
whitespace_section = false;
|
||||||
|
tokens.push(current_token.into_iter().collect());
|
||||||
|
current_token = Vec::new();
|
||||||
|
current_token.push(c);
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
(false, c) => {
|
||||||
|
current_token.push(c);
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokens.push(current_token.into_iter().collect());
|
||||||
|
|
||||||
|
tokens
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tokenize_line_list_containers() {
|
||||||
|
let toolbox_cmd_container_header = "ae05203091ab rust 4 months ago \
|
||||||
|
running registry.fedoraproject.org/fedora-toolbox:35";
|
||||||
|
|
||||||
|
let target = vec![
|
||||||
|
"ae05203091ab",
|
||||||
|
"rust",
|
||||||
|
"4 months ago",
|
||||||
|
"running",
|
||||||
|
"registry.fedoraproject.org/fedora-toolbox:35",
|
||||||
|
];
|
||||||
|
let result = tokenize_line_list_containers(toolbox_cmd_container_header);
|
||||||
|
assert_eq!(target, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_line_list_containers(line: &str) -> Result<ToolbxContainer, ToolbxError> {
|
||||||
|
let tokens = tokenize_line_list_containers(line);
|
||||||
|
if tokens.len() != 5 {
|
||||||
|
panic! {"Expected 5 tokens found {} in {:?}", tokens.len(), tokens};
|
||||||
|
}
|
||||||
|
Ok(ToolbxContainer {
|
||||||
|
id: tokens[0].clone(),
|
||||||
|
name: tokens[1].clone(),
|
||||||
|
created: tokens[2].clone(),
|
||||||
|
status: ToolbxStatus::from_str(&tokens[3])?,
|
||||||
|
image: tokens[4].clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_line_list_containers() {
|
||||||
|
let toolbox_cmd_container_header = "ae05203091ab rust 4 months ago \
|
||||||
|
running registry.fedoraproject.org/fedora-toolbox:35";
|
||||||
|
|
||||||
|
let target = ToolbxContainer {
|
||||||
|
id: "ae05203091ab".to_string(),
|
||||||
|
name: "rust".to_string(),
|
||||||
|
created: "4 months ago".to_string(),
|
||||||
|
status: ToolbxStatus::Running,
|
||||||
|
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
|
||||||
|
};
|
||||||
|
let result = parse_line_list_containers(toolbox_cmd_container_header);
|
||||||
|
assert_eq!(target, result.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_cmd_list_containers(output: &str) -> Vec<ToolbxContainer> {
|
||||||
|
let lines = output.trim().split("\n").skip(1);
|
||||||
|
println!("{:?}", lines);
|
||||||
|
lines.map(parse_line_list_containers).flatten().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_cmd_list_containers() {
|
||||||
|
// This requires toolbx to be installed
|
||||||
|
let toolbox_cmd_container_header = concat!(
|
||||||
|
"CONTAINER ID CONTAINER NAME CREATED STATUS IMAGE NAME\n",
|
||||||
|
"cee1002b5f0b fedora-toolbox-35 2 months ago exited registry.fedoraproject.org/fedora-toolbox:35\n",
|
||||||
|
"9b611313bf65 latex 4 months ago configured registry.fedoraproject.org/fedora-toolbox:35\n",
|
||||||
|
"1235203091ab website 4 months ago created registry.fedoraproject.org/fedora-toolbox:35\n",
|
||||||
|
"ae05203091ab rust 4 months ago running registry.fedoraproject.org/fedora-toolbox:35\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
let desired_result = vec![
|
||||||
|
ToolbxContainer {
|
||||||
|
id: "cee1002b5f0b".to_string(),
|
||||||
|
name: "fedora-toolbox-35".to_string(),
|
||||||
|
created: "2 months ago".to_string(),
|
||||||
|
status: ToolbxStatus::Exited,
|
||||||
|
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
|
||||||
|
},
|
||||||
|
ToolbxContainer {
|
||||||
|
id: "9b611313bf65".to_string(),
|
||||||
|
name: "latex".to_string(),
|
||||||
|
created: "4 months ago".to_string(),
|
||||||
|
status: ToolbxStatus::Configured,
|
||||||
|
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
|
||||||
|
},
|
||||||
|
ToolbxContainer {
|
||||||
|
id: "1235203091ab".to_string(),
|
||||||
|
name: "website".to_string(),
|
||||||
|
created: "4 months ago".to_string(),
|
||||||
|
status: ToolbxStatus::Created,
|
||||||
|
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
|
||||||
|
},
|
||||||
|
ToolbxContainer {
|
||||||
|
id: "ae05203091ab".to_string(),
|
||||||
|
name: "rust".to_string(),
|
||||||
|
created: "4 months ago".to_string(),
|
||||||
|
status: ToolbxStatus::Running,
|
||||||
|
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (result, desired) in zip(
|
||||||
|
parse_cmd_list_containers(toolbox_cmd_container_header).iter(),
|
||||||
|
desired_result.iter(),
|
||||||
|
) {
|
||||||
|
assert_eq!(result, desired)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user