Skip to content

Commit

Permalink
Add support for seeing previous messages when attach()ing and fix cli…
Browse files Browse the repository at this point in the history
…ppy lints and tests
  • Loading branch information
not-jan committed May 24, 2024
1 parent e2f6969 commit 2373c39
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
match opts.subcmd {
Cmd::Attach { id } => {
let container = docker.containers().get(&id);
let tty_multiplexer = container.attach().await?;
let tty_multiplexer = container.attach(false).await?;

let (mut reader, _writer) = tty_multiplexer.split();

Expand Down
9 changes: 7 additions & 2 deletions src/api/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ impl Container {
/// The [`TtyMultiplexer`](TtyMultiplexer) implements Stream for returning Stdout and Stderr chunks. It also implements [`AsyncWrite`](futures_util::io::AsyncWrite) for writing to Stdin.
///
/// The multiplexer can be split into its read and write halves with the [`split`](TtyMultiplexer::split) method
pub async fn attach(&self) -> Result<tty::Multiplexer> {
pub async fn attach(&self, logs: bool) -> Result<tty::Multiplexer> {
let inspect = self.inspect().await?;
let logs = if logs {
1
} else {
0
};
let is_tty = inspect.config.and_then(|c| c.tty).unwrap_or_default();
stream::attach(
self.docker.clone(),
format!(
"/containers/{}/attach?stream=1&stdout=1&stderr=1&stdin=1",
"/containers/{}/attach?stream=1&stdout=1&stderr=1&stdin=1&logs={logs}",
self.id
),
Payload::empty(),
Expand Down
25 changes: 14 additions & 11 deletions src/opts/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
string::ToString,

Check failure on line 15 in src/opts/container.rs

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Diff in /home/runner/work/docker-api-rs/docker-api-rs/src/opts/container.rs

Check failure on line 15 in src/opts/container.rs

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

Diff in /Users/runner/work/docker-api-rs/docker-api-rs/src/opts/container.rs
time::Duration,
};
use std::fmt::{Display, Formatter};

use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
Expand Down Expand Up @@ -331,9 +332,9 @@ impl FromStr for PublishPort {
}
}

impl ToString for PublishPort {
fn to_string(&self) -> String {
format!("{}/{}", self.port, self.protocol.as_ref())
impl Display for PublishPort {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}", self.port, self.protocol.as_ref())
}
}

Expand Down Expand Up @@ -388,15 +389,16 @@ pub enum IpcMode {
Host,
}

impl ToString for IpcMode {
fn to_string(&self) -> String {
match &self {
impl Display for IpcMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match &self {
IpcMode::None => String::from("none"),
IpcMode::Private => String::from("private"),
IpcMode::Shareable => String::from("shareable"),
IpcMode::Container(id) => format!("container:{}", id),
IpcMode::Host => String::from("host"),
}
};
write!(f, "{}", str)
}
}

Expand All @@ -408,12 +410,13 @@ pub enum PidMode {
Host,
}

impl ToString for PidMode {
fn to_string(&self) -> String {
match &self {
impl Display for PidMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match &self {
PidMode::Container(id) => format!("container:{}", id),
PidMode::Host => String::from("host"),
}
};
write!(f, "{}", str)
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/opts/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
path::{Path, PathBuf},
string::ToString,

Check failure on line 4 in src/opts/image.rs

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Diff in /home/runner/work/docker-api-rs/docker-api-rs/src/opts/image.rs

Check failure on line 4 in src/opts/image.rs

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

Diff in /Users/runner/work/docker-api-rs/docker-api-rs/src/opts/image.rs
};
use std::fmt::Display;

use containers_api::opts::{Filter, FilterItem};
use containers_api::url::encoded_pairs;
Expand Down Expand Up @@ -370,16 +371,17 @@ pub enum ImageName {
Digest { image: String, digest: String },
}

impl ToString for ImageName {
fn to_string(&self) -> String {
match &self {
impl Display for ImageName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match &self {
ImageName::Tag { image, tag } => match tag {
Some(tag) => format!("{image}:{tag}"),
None => image.to_owned(),
},
ImageName::Id(id) => id.to_owned(),
ImageName::Digest { image, digest } => format!("{image}@{digest}"),
}
};
write!(f, "{}", str)
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/container_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ async fn container_attach() {

let _ = container.start().await;

let mut multiplexer = container.attach().await.unwrap();
let mut multiplexer = container.attach(false).await.unwrap();
if let Some(chunk) = multiplexer.next().await {
match chunk {
Ok(TtyChunk::StdOut(chunk)) => {
Expand Down Expand Up @@ -726,7 +726,7 @@ async fn container_attach() {

let _ = container.start().await;

let mut multiplexer = container.attach().await.unwrap();
let mut multiplexer = container.attach(false).await.unwrap();
if let Some(chunk) = multiplexer.next().await {
match chunk {
Ok(TtyChunk::StdOut(chunk)) => {
Expand Down
3 changes: 1 addition & 2 deletions tests/network_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ async fn network_connect_disconnect() {
.unwrap()
.networks
.unwrap()
.get(network_name)
.is_some());
.contains_key(network_name));

let _ = network.delete().await;
let _ = container.delete().await;
Expand Down

0 comments on commit 2373c39

Please sign in to comment.