Skip to content

Commit 1c1848e

Browse files
committed
feat: use --password-stdin flag
1 parent e23c4e5 commit 1c1848e

1 file changed

Lines changed: 27 additions & 36 deletions

File tree

src/transactions/docker.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
11
use anyhow::Result;
2+
use tokio::{io, task};
23

34
use crate::env::{DOCKER_PASSWORD, DOCKER_USERNAME};
45

56
pub async fn login() -> Result<()> {
6-
match tokio::process::Command::new("docker")
7+
let mut password = tokio::process::Command::new("echo")
8+
.arg(&*DOCKER_PASSWORD)
9+
.stdout(std::process::Stdio::piped())
10+
.spawn()?;
11+
12+
let mut login = tokio::process::Command::new("docker")
713
.arg("login")
814
.arg("-u")
915
.arg(&*DOCKER_USERNAME)
10-
.arg("-p")
11-
.arg(&*DOCKER_PASSWORD)
12-
.output()
13-
.await
14-
{
15-
Ok(output) => {
16-
if output.status.success() {
17-
tracing::info!(
18-
"successfully logged in to docker with username {}",
19-
&*DOCKER_USERNAME
20-
);
21-
Ok(())
22-
} else {
23-
tracing::error!(
24-
"failed to login to docker with username {}: {}",
25-
&*DOCKER_USERNAME,
26-
String::from_utf8_lossy(&output.stderr)
27-
);
28-
Err(anyhow::anyhow!(
29-
"failed to login to docker with username {}",
30-
&*DOCKER_USERNAME
31-
))
32-
}
33-
}
34-
Err(e) => {
35-
tracing::error!(
36-
"command failed to execute: docker login with username {}: {e:?}",
37-
&*DOCKER_USERNAME
38-
);
39-
Err(anyhow::anyhow!(
40-
"command failed to execute: docker login with username {}",
41-
&*DOCKER_USERNAME
42-
))
43-
}
44-
}
16+
.arg("--password-stdin")
17+
.stdin(std::process::Stdio::piped())
18+
.spawn()?;
19+
20+
let mut password_out = password.stdout.take().unwrap();
21+
let mut login_in = login.stdin.take().unwrap();
22+
23+
let pipe = task::spawn(async move { io::copy(&mut password_out, &mut login_in).await });
24+
25+
pipe.await??;
26+
27+
password.wait().await?;
28+
login.wait().await?;
29+
30+
tracing::info!(
31+
"successfully logged in to docker with username {}",
32+
&*DOCKER_USERNAME,
33+
);
34+
35+
Ok(())
4536
}
4637

4738
pub async fn logout() -> Result<()> {

0 commit comments

Comments
 (0)