-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enclave_build: Extract stream output handling
Extract stream output handling code into handle_stream_output function and move to utils.rs Signed-off-by: Erdem Meydanli <[email protected]>
- Loading branch information
Showing
3 changed files
with
62 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use bollard::errors::Error; | ||
use bollard::secret::{BuildInfo, CreateImageInfo}; | ||
use futures::stream::StreamExt; | ||
use futures::Stream; | ||
use log::{error, info}; | ||
pub trait StreamItem { | ||
fn error(&self) -> Option<String>; | ||
} | ||
|
||
// Implement StreamItem for CreateImageInfo | ||
impl StreamItem for CreateImageInfo { | ||
fn error(&self) -> Option<String> { | ||
self.error.clone() | ||
} | ||
} | ||
|
||
// Implement StreamItem for BuildInfo | ||
impl StreamItem for BuildInfo { | ||
fn error(&self) -> Option<String> { | ||
self.error.clone() | ||
} | ||
} | ||
|
||
pub async fn handle_stream_output<T, U>( | ||
mut stream: impl Stream<Item = Result<T, Error>> + Unpin, | ||
error_type: U, | ||
) -> Result<(), U> | ||
where | ||
T: StreamItem + std::fmt::Debug, | ||
{ | ||
while let Some(item) = stream.next().await { | ||
match item { | ||
Ok(output) => { | ||
if let Some(err_msg) = output.error() { | ||
error!("{:?}", err_msg); | ||
return Err(error_type); | ||
} else { | ||
info!("{:?}", output); | ||
} | ||
} | ||
Err(e) => { | ||
error!("{:?}", e); | ||
return Err(error_type); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |