kratactl/cli/image/
pull.rs

1use anyhow::Result;
2use clap::{Parser, ValueEnum};
3use krata::v1::{
4    common::OciImageFormat,
5    control::{control_service_client::ControlServiceClient, PullImageRequest},
6};
7
8use tonic::transport::Channel;
9
10use crate::pull::pull_interactive_progress;
11
12#[derive(ValueEnum, Clone, Debug, PartialEq, Eq)]
13pub enum ImagePullImageFormat {
14    Squashfs,
15    Erofs,
16    Tar,
17}
18
19#[derive(Parser)]
20#[command(about = "Pull an image into the cache")]
21pub struct ImagePullCommand {
22    #[arg(help = "Image name")]
23    image: String,
24    #[arg(short = 's', long, default_value = "squashfs", help = "Image format")]
25    image_format: ImagePullImageFormat,
26    #[arg(short = 'o', long, help = "Overwrite image cache")]
27    overwrite_cache: bool,
28}
29
30impl ImagePullCommand {
31    pub async fn run(self, mut client: ControlServiceClient<Channel>) -> Result<()> {
32        let response = client
33            .pull_image(PullImageRequest {
34                image: self.image.clone(),
35                format: match self.image_format {
36                    ImagePullImageFormat::Squashfs => OciImageFormat::Squashfs.into(),
37                    ImagePullImageFormat::Erofs => OciImageFormat::Erofs.into(),
38                    ImagePullImageFormat::Tar => OciImageFormat::Tar.into(),
39                },
40                overwrite_cache: self.overwrite_cache,
41                update: true,
42            })
43            .await?;
44        let reply = pull_interactive_progress(response.into_inner()).await?;
45        println!("{}", reply.digest);
46        Ok(())
47    }
48}