Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
apply clap for run subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
guni1192 committed Dec 25, 2018
1 parent 6514bea commit 42b6ce2
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 9 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2018"
nix = "0.11.0"
getopts = "0.2"
dirs = "1.0"
clap = "2.32.0"
22 changes: 13 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use nix::unistd::{chdir, chroot, getpgid, getuid, Pid};

use dirs::home_dir;

use clap::ArgMatches;

use super::bootstrap::pacstrap;
use super::container;
use super::help::print_help;
Expand All @@ -17,29 +19,31 @@ use super::network::{Bridge, Network};
use super::options;

// TODO: deamonize option
pub fn run(args: &[String]) {
let args = args.to_vec();
// pub fn run(args: &[String]) {
pub fn run(sub_m: ArgMatches) {
// let args = args.to_vec();

let ace_container_path_env = "ACE_CONTAINER_PATH";
// TODO: settting.rsからの読み込みに変更
let home_dir = home_dir().expect("Cannot get $HOME");
let ace_path = format!("{}/{}", home_dir.display(), "ace-containers");
env::set_var(ace_container_path_env, ace_path);

let matches = options::get_runner_options(args).expect("Invalid arguments");
// let matches = options::get_runner_options(args).expect("Invalid arguments");
let matches = &sub_m;

if matches.opt_present("help") {
if matches.is_present("help") {
print_help();
exit(0);
}

let command = match matches.opt_str("exec") {
Some(c) => c,
let command = match matches.value_of("exec") {
Some(c) => c.to_string(),
None => "/bin/bash".to_string(),
};

let container_name = matches
.opt_str("name")
.value_of("name")
.expect("invalied arguments about container name");

let pid = process::id();
Expand All @@ -51,9 +55,9 @@ pub fn run(args: &[String]) {

let uid = getuid();

let container = container::Container::new(container_name.clone(), command, uid, pgid);
let container = container::Container::new(container_name.to_string(), command, uid, pgid);

if matches.opt_present("del") {
if matches.is_present("del") {
container.delete().expect("Faild to remove container: ");
}

Expand Down
27 changes: 27 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env::args;
use std::process::exit;

use clap::{App, Arg, SubCommand};

mod bootstrap;
mod cli;
mod commands;
Expand All @@ -13,12 +15,36 @@ mod options;
use self::help::print_help;

fn main() {
let app_matches = App::new("Cromwell")
.version("v1.0.0")
.author("Takashi IIGUNI <ad2314ce71926@gmail.com>")
.about("Ownership Managed Container Runntime")
.subcommand(
SubCommand::with_name("run")
.version("v1.0.0")
.about("run cromwell container")
.arg(Arg::with_name("debug").help("print debug information verbosely")),
)
.get_matches();

let args: Vec<String> = args().collect();

if args.len() < 2 {
print_help();
exit(1);
}

match &app_matches.subcommand() {
("run", Some(sub_m)) => cli::run(sub_m.clone().clone()),
// Some("network", sub_m) => cli::network(sub_m),
_ => {
eprintln!("Unexpected Arguments");
print_help();
exit(1);
}
}

/*
match &args[1][..] {
"run" => cli::run(&args[1..]),
"network" => cli::network(&args[1..]),
Expand All @@ -32,4 +58,5 @@ fn main() {
exit(1);
}
}
*/
}

0 comments on commit 42b6ce2

Please sign in to comment.