Browse Source

feat: add skeleton cli

rust
Giovanni Torres 1 year ago
parent
commit
81e6877bb2
  1. 1850
      Cargo.lock
  2. 19
      Cargo.toml
  3. 26
      src/cli.rs
  4. 4
      src/lib.rs
  5. 31
      src/main.rs
  6. 24
      src/vm.rs
  7. 52
      tests/cli_tests.rs

1850
Cargo.lock generated

File diff suppressed because it is too large Load Diff

19
Cargo.toml

@ -1,6 +1,23 @@
[package]
name = "kvm-install-vm"
version = "0.1.0"
version = "1.0.0-alpha"
edition = "2024"
description = "A tool for creating KVM virtual machines"
authors = ["Giovanni Torres <giovanni.torres@users.noreply.github.com"]
license = "MIT"
repository = "https://github.com/giovtorres/kvm-install-vm"
[dependencies]
anyhow = "1.0"
clap = { version = "4.4", features = ["derive"] }
env_logger = "0.10"
futures-util = "0.3"
indicatif = "0.17"
ipnetwork = "0.20"
log = "0.4"
reqwest = { version = "0.11", features = ["json", "stream"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
tempfile = "3.8"
tokio = { version = "1", features = ["full"] }
virt = "0.3.1"

26
src/cli.rs

@ -0,0 +1,26 @@
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Cli {
#[arg(short = 'n', long)]
pub name: String,
#[arg(short = 't', long, default_value = "centos8")]
pub distro: String,
#[arg(short = 'c', long, default_value_t = 1)]
pub vcpus: u32,
#[arg(short = 'm', long, default_value_t = 1024)]
pub memory: u32,
#[arg(short = 'd', long, default_value_t = 10)]
pub disk: u32,
#[arg(long)]
pub graphics: bool,
#[arg(long)]
pub dry_run: bool,
}

4
src/lib.rs

@ -0,0 +1,4 @@
pub mod cli;
pub mod vm;
pub use cli::Cli;

31
src/main.rs

@ -1,3 +1,30 @@
fn main() {
println!("Hello, world!");
use anyhow::Result;
use clap::Parser;
use kvm_install_vm::Cli;
#[tokio::main]
async fn main() -> Result<()> {
// Initialize logger
env_logger::init();
// Parse command line arguments
let cli = Cli::parse();
println!("Starting kvm-install-vm Rust implementation...");
println!("VM Name: {}", cli.name);
println!("Distribution: {}", cli.distro);
println!("Configuration:");
println!(" vCPUs: {}", cli.vcpus);
println!(" Memory: {} MB", cli.memory);
println!(" Disk Size: {} GB", cli.disk);
if cli.dry_run {
println!("Dry run mode - no VM will be created");
return Ok(());
}
println!("VM creation would start here...");
Ok(())
}

24
src/vm.rs

@ -0,0 +1,24 @@
use anyhow::Result;
pub struct VirtualMachine {
pub name: String,
pub vcpus: u32,
pub memory: u32,
pub disk_size: u32,
}
impl VirtualMachine {
pub fn new(name: String, vcpus: u32, memory: u32, disk_size: u32) -> Self {
Self {
name,
vcpus,
memory,
disk_size,
}
}
pub fn create(&self) -> Result<()> {
println!("Creating VM: {}", self.name);
Ok(())
}
}

52
tests/cli_tests.rs

@ -0,0 +1,52 @@
use clap::Parser;
use kvm_install_vm::Cli;
use std::ffi::OsString;
fn get_args(args: &[&str]) -> Vec<OsString> {
vec![OsString::from("kvm-install-vm")]
.into_iter()
.chain(args.iter().map(|s| OsString::from(s)))
.collect()
}
#[test]
fn test_cli_defaults() {
let args = get_args(&["--name", "test-vm"]);
let cli = Cli::parse_from(args);
assert_eq!(cli.name, "test-vm");
assert_eq!(cli.distro, "centos8");
assert_eq!(cli.vcpus, 1);
assert_eq!(cli.disk, 10);
assert_eq!(cli.memory, 1024);
assert_eq!(cli.graphics, false);
assert_eq!(cli.dry_run, false);
}
#[test]
fn test_cli_custom_values() {
let args = get_args(&[
"--name",
"custom-vm",
"--distro",
"ubuntu2004",
"--vcpus",
"4",
"--memory",
"4096",
"--disk",
"50",
"--graphics",
"--dry-run",
]);
let cli = Cli::parse_from(args);
assert_eq!(cli.name, "custom-vm");
assert_eq!(cli.distro, "ubuntu2004");
assert_eq!(cli.vcpus, 4);
assert_eq!(cli.disk, 50);
assert_eq!(cli.memory, 4096);
assert_eq!(cli.graphics, true);
assert_eq!(cli.dry_run, true);
}
Loading…
Cancel
Save