Skip to content

Commit

Permalink
feat(main): add cache cmd for rust
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Dec 26, 2024
1 parent 25a3514 commit 0300b26
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "rbuild-runtime"
version = "0.1.0"
authors = ["cuisongliu <[email protected]>"]
edition = "2021"
description = "rbuild-runtime, a rusty-hermit application"
description = "rbuild-runtime, a rusty-hermit application.\n\nversion description: https://sealos.io/docs/5.0.0/developer-guide/lifecycle-management/quick-start/deploy-kubernetes#kubernetes-compatibility-with-containerd"
repository = "https://github.com/labring-actions/rbuild-runtime"
license-file = "LICENSE"
[dependencies]
Expand Down
28 changes: 28 additions & 0 deletions src/cli/cli_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::{BaseOpts, CmdExec};
use clap::Parser;

#[derive(Parser, Debug, Clone)]
pub struct CacheOpts {
#[arg(
long,
long_help = "registry host address",
default_value = "sealos.hub:5000"
)]
pub registry_host: String,
#[arg(long, long_help = "registry repo name", default_value = "5000")]
pub registry_repo: String,
#[arg(long, long_help = "registry username", default_value = "admin")]
pub registry_username: String,
#[arg(long, long_help = "registry password", default_value = "passw0rd")]
pub registry_password: String,
}

impl CmdExec for CacheOpts {
async fn execute(self, _base_opts: BaseOpts) -> anyhow::Result<()> {
println!(
"registry info host: {}, username: {}, repo: {}",
self.registry_host, self.registry_username, self.registry_repo
);
Ok(())
}
}
36 changes: 31 additions & 5 deletions src/cli/cli_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ use clap::Parser;

use crate::Runtime;

#[derive(Parser, Debug)]
#[command(name = "rbuild-runtime", about, version, author, long_about = None)]
#[derive(Parser, Debug, Clone)]
#[command(
name = "rbuild-runtime",
about,
version,
author,
long_about = "build the runtime images"
)]
pub struct BuildOpts {
#[clap(subcommand)]
pub cmd: Runtime,
#[arg(long, default_value = "v1.25.0")]
#[clap(flatten)]
pub base_opts: BaseOpts,
}

#[derive(Parser, Debug, Clone)]
pub struct BaseOpts {
#[arg(
long,
default_value = "v1.25.0",
long_help = "kubernetes version, >=1.26 using v1 cri version"
)]
pub kube_version: String,
#[arg(long, default_value = "v4.3.7")]
#[arg(long, default_value = "v4.3.7", long_help = "sealos version")]
pub sealos_version: String,
#[arg(long, default_value = "2.8.2")]
#[arg(long, default_value = "2.8.2", long_help = "registry version")]
pub registry_version: String,
#[arg(long,long_help="if using proxy address,use_proxy=true", default_value = "false",value_parser = parse_proxy)]
pub use_proxy: bool,
}

fn parse_proxy(s: &str) -> Result<bool, &'static str> {
match s {
"true" => Ok(true),
"false" => Ok(false),
_ => Err("invalid value, only true or false"),
}
}
11 changes: 7 additions & 4 deletions src/cli/cli_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::cli::cli_build_containerd::ContainerdOpts;
use crate::cli::cli_build_crio::CrioOpts;
use crate::cli::cli_build_docker::DockerOpts;
use crate::cli::cli_runtime_containerd::ContainerdOpts;
use crate::cli::cli_runtime_crio::CrioOpts;
use crate::cli::cli_runtime_docker::DockerOpts;
use crate::CacheOpts;
use clap::Parser;
use enum_dispatch::enum_dispatch;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
#[enum_dispatch(CmdExec)]
pub enum Runtime {
#[clap(
Expand All @@ -16,4 +17,6 @@ pub enum Runtime {
Docker(DockerOpts),
#[clap(name = "crio", about = "build the runtime for crio runtime images")]
Crio(CrioOpts),
#[clap(name = "cache", about = "cache the runtime images")]
Cache(CacheOpts),
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::parse_container_runtime;
use crate::{CmdExec, ContainerRuntime};
use crate::{BaseOpts, CmdExec, ContainerRuntime};
use clap::Parser;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
pub struct ContainerdOpts {
#[arg(long, default_value = "v1.6.23")]
pub version: String,
Expand All @@ -13,7 +13,7 @@ pub struct ContainerdOpts {
}

impl CmdExec for ContainerdOpts {
async fn execute(self) -> anyhow::Result<()> {
async fn execute(self, _base_opts: BaseOpts) -> anyhow::Result<()> {
println!(
"containerd version: {}, runtime: {:?}",
self.version, self.runtime
Expand Down
6 changes: 3 additions & 3 deletions src/cli/cli_build_crio.rs → src/cli/cli_runtime_crio.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::parse_container_runtime;
use crate::{CmdExec, ContainerRuntime};
use crate::{BaseOpts, CmdExec, ContainerRuntime};
use clap::Parser;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
pub struct CrioOpts {
#[arg(long, long_help = "cril version", default_value = "20.10.9")]
pub version: String,
Expand All @@ -13,7 +13,7 @@ pub struct CrioOpts {
}

impl CmdExec for CrioOpts {
async fn execute(self) -> anyhow::Result<()> {
async fn execute(self, _base_opts: BaseOpts) -> anyhow::Result<()> {
println!(
"crio version: {}, runtime: {:?}",
self.version, self.runtime
Expand Down
6 changes: 3 additions & 3 deletions src/cli/cli_build_docker.rs → src/cli/cli_runtime_docker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::CmdExec;
use crate::{BaseOpts, CmdExec};
use clap::Parser;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
pub struct DockerOpts {
#[arg(long, long_help = "docker cli version", default_value = "20.10.9")]
pub version: String,
Expand All @@ -10,7 +10,7 @@ pub struct DockerOpts {
}

impl CmdExec for DockerOpts {
async fn execute(self) -> anyhow::Result<()> {
async fn execute(self, _base_opts: BaseOpts) -> anyhow::Result<()> {
println!(
"docker version: {}, cri version: {:?}",
self.version, self.cri_docker_version
Expand Down
14 changes: 8 additions & 6 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
pub use cli_build_containerd::*;
pub use cli_build_crio::*;
pub use cli_build_docker::*;
pub use cli_cache::*;
pub use cli_root::*;
pub use cli_runtime::*;
pub use cli_runtime_containerd::*;
pub use cli_runtime_crio::*;
pub use cli_runtime_docker::*;
use std::fmt;
use std::str::FromStr;

mod cli_build_containerd;
mod cli_build_crio;
mod cli_build_docker;
mod cli_cache;
#[allow(clippy::module_inception)]
mod cli_root;
mod cli_runtime;
mod cli_runtime_containerd;
mod cli_runtime_crio;
mod cli_runtime_docker;

#[derive(Debug, Copy, Clone)]
pub enum ContainerRuntime {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ mod cli;
#[allow(async_fn_in_trait)]
#[enum_dispatch]
pub trait CmdExec {
async fn execute(self) -> anyhow::Result<()>;
async fn execute(self, base_opts: BaseOpts) -> anyhow::Result<()>;
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ async fn main() -> anyhow::Result<()> {
info!("log level set to: {:?}", LevelFilter::Info);
trace!("asset: {:?}", asset.data.len());
let opts: BuildOpts = BuildOpts::parse();
opts.cmd.execute().await?;
opts.cmd.execute(opts.base_opts.clone()).await?;
Ok(())
}

0 comments on commit 0300b26

Please sign in to comment.