This commit is contained in:
stubbfelnewpc
2020-06-20 20:31:24 +02:00
commit 28f9a40d68
6 changed files with 420 additions and 0 deletions

97
control/cli/src/main.rs Normal file
View File

@@ -0,0 +1,97 @@
extern crate clap;
use clap::{Arg, App, SubCommand};
use std::process::{Command, Stdio};
use std::io::{BufRead, BufReader, Error, ErrorKind};
const START_SUBCOMMAND_NAME: &str = "start";
const STOP_SUBCOMMAND_NAME: &str = "stop";
const START_FILE_ARGUMENT_NAME: &str = "start-ide-file";
const START_ARGS_ARGUMENT_NAME: &str = "start-ide-arguments";
const CLEAN_RUN_ARGUMENT_NAME: &str = "clean-run";
const VERBOSE_ARGUMENT_NAME: &str = "verbose";
const IDE_ID_ARGUMENT_NAME: &str = "ide-id";
fn main() {
let matches =
App::new("nixidectl")
.version("0.1.0")
.author("stubbfel")
.about("control nixide process")
.subcommand(SubCommand::with_name(START_SUBCOMMAND_NAME)
.arg(Arg::with_name(START_FILE_ARGUMENT_NAME)
.short("f")
.long("start-ide-file")
.value_name("START_IDE_FILE")
.help("path to start-ide.nix")
.required(true)
.takes_value(true))
.arg(Arg::with_name(START_ARGS_ARGUMENT_NAME)
.short("a")
.long("start-ide-arguments")
.value_name("START_IDE_ARGUMENTS")
.help("the start arguments of the start-ide.nix")
.default_value("{}")
.takes_value(true))
.arg(Arg::with_name(CLEAN_RUN_ARGUMENT_NAME)
.short("c")
.long("clean-run")
.help("clean app folder, before start runnnig"))
.arg(Arg::with_name(VERBOSE_ARGUMENT_NAME)
.short("v")
.long("verbose")
.help("verbose")))
.subcommand(SubCommand::with_name("stop")
.arg(Arg::with_name(IDE_ID_ARGUMENT_NAME)
.short("i")
.long("ide-id")
.value_name("IDE_ID")
.help("the ide id is the process group id")
.required(true)
.takes_value(true)))
.get_matches();
match matches.subcommand() {
(START_SUBCOMMAND_NAME, Some(start_cmd)) => {
println!("{}", std::process::id());
let file = start_cmd.value_of(START_FILE_ARGUMENT_NAME).unwrap();
let args = start_cmd.value_of(START_ARGS_ARGUMENT_NAME).unwrap();
let run_script = match start_cmd.is_present(CLEAN_RUN_ARGUMENT_NAME) {
true => "clean_run_ide.sh",
_ => "run_ide.sh"
};
let nix_expression = format!("with import <nixpkgs> {{}}; callPackage {} {}", file, args);
let cmd = Command::new("nix-shell")
.stderr(Stdio::null())
//.stdout(Stdio::null())
.stdout(Stdio::piped())
.arg("--expr")
.arg(nix_expression)
.arg("--pure")
.arg("--run")
.arg(run_script)
.spawn()
.expect("start ide command failed");
let out = cmd.stdout.ok_or_else(|| Error::new(ErrorKind::Other,"Could not capture standard output.")).unwrap();
let reader = BufReader::new(out);
let start_up_log = reader.lines()
.filter_map(|line| line.ok())
.take_while(|line| !line.find("theia start").is_some());
match start_cmd.is_present(VERBOSE_ARGUMENT_NAME) {
true => start_up_log.for_each(|line| println!("{}", line)),
_ => start_up_log.for_each(|_| {})
};
},
(STOP_SUBCOMMAND_NAME, Some(stop_cmd)) => {
let ide_id = stop_cmd.value_of(IDE_ID_ARGUMENT_NAME).unwrap();
Command::new("pkill")
.arg("-TERM")
.arg("-g")
.arg(ide_id)
.output().unwrap();
}
_ => println!("-1")
};
}