Skip to content

Commit

Permalink
feat(main): add cmd group for build image
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Jun 23, 2024
1 parent 6adccc7 commit b817540
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ license-file = "LICENSE"
[dependencies]
rust-embed="8.4.0"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
clap = { version = "4.5.7", features = ["derive"] }
serde = { version = "1.0.203", features = ["derive"] }

[build-dependencies]
reqwest = "0.12.5"
Expand Down
8 changes: 8 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ changelog:

validate:
file files/sealos

line:
tokei .

check:
cargo check
cargo fmt -- --check
cargo deny check -d
17 changes: 17 additions & 0 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use clap::Parser;

use crate::BuildRuntime;

#[derive(Parser, Debug)]
#[command(name = "rbuild-runtime", about, version, author, long_about = None)]
pub struct BuildRuntimeOpts {
#[clap(subcommand)]
pub cmd: BuildRuntime,
#[arg(
short,
long,
default_value = "false",
long_help = "Print debug information"
)]
pub debug: bool,
}
89 changes: 89 additions & 0 deletions src/cli/cli_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::fmt;
use std::str::FromStr;

use clap::Parser;

#[derive(Parser, Debug)]
pub enum BuildRuntime {
#[clap(name = "build", about = "build the runtime for sealos runtime images")]
Build(BuildOpts),
}

#[derive(Parser, Debug)]
pub struct BuildOpts {
#[clap(subcommand)]
pub build: BuildCommand,
#[arg(long, default_value = "v1.25.0")]
pub kube_version: String,
#[arg(long, default_value = "v4.3.7")]
pub sealos_version: String,
#[arg(long, default_value = "2.8.2")]
pub registry_version: String,
}

#[derive(Parser, Debug)]
pub enum BuildCommand {
#[clap(name = "docker", about = "build the runtime for docker runtime images")]
Docker(DockerOpts),
#[clap(
name = "containerd",
about = "build the runtime for containerd runtime images"
)]
Containerd(ContainerdOpts),
}

#[derive(Parser, Debug)]
pub struct DockerOpts {
#[arg(default_value = "20.10.9")]
pub version: String,
#[arg(default_value = "v0.3.14")]
pub cri_docker_version: String,
}

#[derive(Parser, Debug)]
pub struct ContainerdOpts {
#[arg(long, default_value = "v1.6.23")]
pub version: String,
#[arg(short, long, default_value = "runc", value_parser = parse_container_runtime)]
pub runtime: ContainerRuntime,
}

#[derive(Debug, Copy, Clone)]
pub enum ContainerRuntime {
RunC,
CRun,
Youki,
}

fn parse_container_runtime(s: &str) -> Result<ContainerRuntime, &'static str> {
s.parse()
}

impl From<ContainerRuntime> for &'static str {
fn from(f: ContainerRuntime) -> Self {
match f {
ContainerRuntime::RunC => "runc",
ContainerRuntime::CRun => "crun",
ContainerRuntime::Youki => "youki",
}
}
}

impl FromStr for ContainerRuntime {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"runc" => Ok(ContainerRuntime::RunC),
"crun" => Ok(ContainerRuntime::CRun),
"youki" => Ok(ContainerRuntime::Youki),
_ => Err("Invalid file format"),
}
}
}

impl fmt::Display for ContainerRuntime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Into::<&str>::into(*self))
}
}
5 changes: 5 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod cli;
pub mod cli_build;

pub use cli::*;
pub use cli_build::*;

0 comments on commit b817540

Please sign in to comment.