#[macro_use]
extern crate log;
use std::path::Path;
use clap::{ Args, Parser, Subcommand};
use petbox::container;
use petbox::config::Config;
#[cfg(debug_assertions)]
const DEBUG_ENV: bool = true;
#[derive(Parser)]
#[command(author, version, about, long_about = None, disable_help_flag(true))]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
#[command(disable_help_flag(true))]
Create(Create),
#[command(disable_help_flag(true))]
Attach(Attach),
}
#[derive(Args)]
struct Create {
#[arg()]
name: String,
#[arg()]
source: String,
#[arg(short, long)]
home: bool,
#[arg(long,action = clap::ArgAction::Help)]
help: (),
#[arg(long)]
enter_ns: bool,
}
#[derive(Args)]
struct Attach {
#[arg(short, long)]
name: Option<String>,
#[arg(long)]
help: bool,
}
fn main() {
let mut logger: env_logger::Builder;
if DEBUG_ENV {
logger = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug"));
warn!("You are using dev build of petbox compiled without optimization.")
} else {
logger= env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"));
}
logger.format_timestamp(None).init();
let cli = Cli::parse();
match &cli.command {
Commands::Create(opt) => {
let config = Config::build();
let root_path = config.get_container_rootfs(&opt.name);
let root = container::Rootfs::new(&root_path);
info!("Creating conatiner...");
trace!("path:{:?},tar_file:{:?}", root_path, opt.source);
match &opt.enter_ns {
true => {root.install_rootfs_enter_ns("/bin/bash");},
false => {root.install_rootfs_from_tar(Path::new(&opt.source));},
}
}
Commands::Attach(opt) => {
println!("run")
}
}
}