Skip to content

Commit

Permalink
feat: initial network page
Browse files Browse the repository at this point in the history
- note  the modal situation needs tidying up as it is currently messy
- failed deletion results in a slightly dodgy y/n modal where neither can do anything...
  • Loading branch information
robertpsoane committed Jul 13, 2024
1 parent 6acddc5 commit 8b9ab10
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The following commands are supported:
| `images` | `image` | Open the `Images` top level page |
| `containers` | `container` | Open the `Containers` top level page |
| `volumes` | `volume` | Open the `Volumes` top level page |
| `networks` | `network` | Open the `Networks` top level page |
| `quit` | `q` | Close the application |


Expand Down Expand Up @@ -126,6 +127,15 @@ The following actions are available on the Volumes page:
| `Ctrl+d` | Delete the currently selected volume |
| `d` | Describe the currently selected volume |

#### Networks

The following actions are available on the Volumes page:

| Hotkey | Action |
| -------- | -------------------------------------- |
| `Ctrl+d` | Delete the currently selected volume |
| `d` | Describe the currently selected volume |

#### Logs

The following actions are available on the Logs page:
Expand Down
23 changes: 23 additions & 0 deletions src/callbacks/delete_network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{docker::network::DockerNetwork, traits::Callback};
use async_trait::async_trait;
use color_eyre::eyre::Result;

#[derive(Debug)]
pub struct DeleteNetwork {
docker: bollard::Docker,
network: DockerNetwork,
}

impl DeleteNetwork {
pub fn new(docker: bollard::Docker, network: DockerNetwork) -> Self {
Self { docker, network }
}

Check warning on line 14 in src/callbacks/delete_network.rs

View check run for this annotation

Codecov / codecov/patch

src/callbacks/delete_network.rs#L12-L14

Added lines #L12 - L14 were not covered by tests
}

#[async_trait]
impl Callback for DeleteNetwork {
async fn call(&self) -> Result<()> {
let _ = self.network.delete(&self.docker).await?;
Ok(())
}

Check warning on line 22 in src/callbacks/delete_network.rs

View check run for this annotation

Codecov / codecov/patch

src/callbacks/delete_network.rs#L19-L22

Added lines #L19 - L22 were not covered by tests
}
25 changes: 25 additions & 0 deletions src/callbacks/empty_callable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::traits::Callback;
use async_trait::async_trait;
use color_eyre::eyre::Result;

#[derive(Debug)]
pub struct EmptyCallable {}

impl Default for EmptyCallable {
fn default() -> Self {
Self::new()
}

Check warning on line 11 in src/callbacks/empty_callable.rs

View check run for this annotation

Codecov / codecov/patch

src/callbacks/empty_callable.rs#L9-L11

Added lines #L9 - L11 were not covered by tests
}

impl EmptyCallable {
pub fn new() -> Self {
Self {}
}

Check warning on line 17 in src/callbacks/empty_callable.rs

View check run for this annotation

Codecov / codecov/patch

src/callbacks/empty_callable.rs#L15-L17

Added lines #L15 - L17 were not covered by tests
}

#[async_trait]
impl Callback for EmptyCallable {
async fn call(&self) -> Result<()> {
Ok(())
}

Check warning on line 24 in src/callbacks/empty_callable.rs

View check run for this annotation

Codecov / codecov/patch

src/callbacks/empty_callable.rs#L22-L24

Added lines #L22 - L24 were not covered by tests
}
2 changes: 2 additions & 0 deletions src/callbacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod delete_container;
pub mod delete_image;
pub mod delete_network;
pub mod delete_volume;
pub mod empty_callable;
pub use delete_container::DeleteContainer;
5 changes: 4 additions & 1 deletion src/components/command_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const CONTAINER: &str = "container";
const CONTAINERS: &str = "containers";
const VOLUME: &str = "volume";
const VOLUMES: &str = "volumes";
const NETWORK: &str = "network";
const NETWORKS: &str = "networks";

#[derive(Debug)]
pub struct CommandInput {
Expand All @@ -33,7 +35,7 @@ pub struct CommandInput {
impl CommandInput {
pub fn new(tx: Sender<Message<Key, Transition>>, prompt: String) -> Self {
let ac: Autocomplete = Autocomplete::from(vec![
QUIT, Q, IMAGE, IMAGES, CONTAINER, CONTAINERS, VOLUME, VOLUMES,
QUIT, Q, IMAGE, IMAGES, CONTAINER, CONTAINERS, VOLUME, VOLUMES, NETWORK, NETWORKS,

Check warning on line 38 in src/components/command_input.rs

View check run for this annotation

Codecov / codecov/patch

src/components/command_input.rs#L38

Added line #L38 was not covered by tests
]);
Self {
tx,
Expand Down Expand Up @@ -93,6 +95,7 @@ impl CommandInput {
IMAGE | IMAGES => Some(Transition::ToImagePage(AppContext::default())),
CONTAINER | CONTAINERS => Some(Transition::ToContainerPage(AppContext::default())),
VOLUME | VOLUMES => Some(Transition::ToVolumePage(AppContext::default())),
NETWORK | NETWORKS => Some(Transition::ToNetworkPage(AppContext::default())),

Check warning on line 98 in src/components/command_input.rs

View check run for this annotation

Codecov / codecov/patch

src/components/command_input.rs#L98

Added line #L98 was not covered by tests
_ => None,
};

Expand Down
4 changes: 3 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
docker::{
container::DockerContainer, image::DockerImage, traits::Describe, volume::DockerVolume,
container::DockerContainer, image::DockerImage, network::DockerNetwork, traits::Describe,
volume::DockerVolume,
},
events::Transition,
};
Expand All @@ -17,6 +18,7 @@ pub struct AppContext {
pub docker_container: Option<DockerContainer>,
pub docker_image: Option<DockerImage>,
pub docker_volume: Option<DockerVolume>,
pub docker_network: Option<DockerNetwork>,
pub describable: Option<Box<dyn Describe>>,
}

Expand Down
1 change: 1 addition & 0 deletions src/docker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod container;
pub mod image;
pub mod logs;
pub mod network;
pub mod traits;
pub mod util;
pub mod volume;
68 changes: 68 additions & 0 deletions src/docker/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use bollard::{
secret::{Network, NetworkContainer},
};
use color_eyre::eyre::{bail, Result};
use serde::Serialize;
use std::collections::HashMap;

use super::traits::Describe;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DockerNetwork {
pub id: String,
pub name: String,
pub driver: String,
pub created_at: String,
pub scope: String,
pub internal: Option<bool>,
pub attachable: Option<bool>,
pub containers: Option<HashMap<String, NetworkContainer>>,
}

impl DockerNetwork {
pub fn from(v: Network) -> Self {
Self {
id: v.id.unwrap_or_default(),
name: v.name.unwrap_or_default(),
driver: v.driver.unwrap_or_default(),
created_at: v.created.unwrap_or_default(),
scope: v.scope.unwrap_or_default(),
internal: v.internal,
attachable: v.attachable,
containers: v.containers,
}
}

Check warning on line 34 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L23-L34

Added lines #L23 - L34 were not covered by tests

pub async fn list(docker: &bollard::Docker) -> Result<Vec<Self>> {
let networks = docker.list_networks::<String>(None).await?;
let mut network: Vec<Self> = networks.into_iter().map(Self::from).collect();

network.sort_by_key(|v| v.name.clone());

Ok(network)
}

Check warning on line 43 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L36-L43

Added lines #L36 - L43 were not covered by tests

pub async fn delete(&self, docker: &bollard::Docker) -> Result<()> {
docker.remove_network(&self.get_name()).await?;
Ok(())
}

Check warning on line 48 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L45-L48

Added lines #L45 - L48 were not covered by tests
}

impl Describe for DockerNetwork {
fn get_id(&self) -> String {
self.get_name()
}
fn get_name(&self) -> String {
self.name.clone()
}

Check warning on line 57 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L52-L57

Added lines #L52 - L57 were not covered by tests

fn describe(&self) -> Result<Vec<String>> {
let summary = match serde_yml::to_string(&self) {
Ok(s) => s,

Check warning on line 61 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L59-L61

Added lines #L59 - L61 were not covered by tests
Err(_) => {
bail!("failed to parse container summary")

Check warning on line 63 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L63

Added line #L63 was not covered by tests
}
};
Ok(summary.lines().map(String::from).collect())
}

Check warning on line 67 in src/docker/network.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/network.rs#L66-L67

Added lines #L66 - L67 were not covered by tests
}
1 change: 1 addition & 0 deletions src/events/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum Transition {
ToDescribeContainerPage(AppContext),
ToAttach(AppContext),
ToVolumePage(AppContext),
ToNetworkPage(AppContext),
}

pub async fn send_transition(
Expand Down
3 changes: 2 additions & 1 deletion src/pages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod containers;
pub mod describe;
pub mod images;
pub mod logs;
pub mod volume;
pub mod networks;
pub mod volumes;
Loading

0 comments on commit 8b9ab10

Please sign in to comment.