From 35e5c29a60210406eafe62f8eaf0fb04f0175370 Mon Sep 17 00:00:00 2001 From: stubbfelnewpc Date: Tue, 23 Jun 2020 00:15:57 +0200 Subject: [PATCH] add systemd unstall command --- .gitignore | 1 + Cargo.lock | 141 ++++++++++++++++++++++++++++++++++++++++ control/cli/Cargo.toml | 3 +- control/cli/src/main.rs | 100 ++++++++++++++++++++++------ 4 files changed, 225 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 4a4e253..f331813 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # will have compiled files and executables target/ .theia +.nixide # These are backup files generated by rustfmt **/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock index 23b0846..c64b1cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" + [[package]] name = "atty" version = "0.2.14" @@ -20,12 +32,41 @@ dependencies = [ "winapi", ] +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "clap" version = "2.33.1" @@ -41,6 +82,55 @@ dependencies = [ "vec_map", ] +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "dirs" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" +dependencies = [ + "cfg-if", + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.14" @@ -50,6 +140,12 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.71" @@ -61,6 +157,45 @@ name = "nixidectl_cli" version = "0.1.0" dependencies = [ "clap", + "shellexpand", +] + +[[package]] +name = "redox_syscall" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" + +[[package]] +name = "redox_users" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + +[[package]] +name = "rust-argon2" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + +[[package]] +name = "shellexpand" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2b22262a9aaf9464d356f656fea420634f78c881c5eebd5ef5e66d8b9bc603" +dependencies = [ + "dirs", ] [[package]] @@ -90,6 +225,12 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi" version = "0.3.8" diff --git a/control/cli/Cargo.toml b/control/cli/Cargo.toml index b98fda8..0eff925 100644 --- a/control/cli/Cargo.toml +++ b/control/cli/Cargo.toml @@ -7,4 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = "2.33.1" \ No newline at end of file +clap = "2.33.1" +shellexpand = "2.0.0" \ No newline at end of file diff --git a/control/cli/src/main.rs b/control/cli/src/main.rs index 1dd1ea1..3498db6 100644 --- a/control/cli/src/main.rs +++ b/control/cli/src/main.rs @@ -1,10 +1,32 @@ extern crate clap; -use clap::{Arg, App, SubCommand}; -use std::process::{Command, Stdio}; -use std::io::{BufRead, BufReader, Error, ErrorKind}; +extern crate shellexpand; +use clap::{ + Arg, + App, + SubCommand +}; +use std::{ + fs, + io::{ + BufRead, + BufReader, + Error, + ErrorKind + }, + process::{ + Command, + Stdio + } +}; const START_SUBCOMMAND_NAME: &str = "start"; +const SYSTEMD_SUBCOMMAND_NAME: &str = "systemd"; +const INSTALL_SUBCOMMAND_NAME: &str = "install"; +const UNINSTALL_SUBCOMMAND_NAME: &str = "uninstall"; const STOP_SUBCOMMAND_NAME: &str = "stop"; +const FORCE_ARGUMENT_NAME: &str = "force"; +const IDE_SERVICE_NAME_ARGUMENT_NAME: &str = "ide-service-name"; +const SYSTEMD_UNIT_FOLDER_ARGUMENT_NAME: &str = "unit-folder"; 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"; @@ -12,26 +34,30 @@ const VERBOSE_ARGUMENT_NAME: &str = "verbose"; const IDE_ID_ARGUMENT_NAME: &str = "ide-id"; fn main() { + + let start_ide_file_arg =Arg::with_name(START_FILE_ARGUMENT_NAME) + .short("s") + .long("start-ide-file") + .value_name("PATH_TO_START_IDE_FILE") + .help("path to start-ide.nix") + .required(true) + .takes_value(true); + let start_ide_arguments_args = 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); + 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(&start_ide_file_arg) + .arg(&start_ide_arguments_args) .arg(Arg::with_name(CLEAN_RUN_ARGUMENT_NAME) .short("c") .long("clean-run") @@ -40,7 +66,7 @@ fn main() { .short("v") .long("verbose") .help("verbose"))) - .subcommand(SubCommand::with_name("stop") + .subcommand(SubCommand::with_name(STOP_SUBCOMMAND_NAME) .arg(Arg::with_name(IDE_ID_ARGUMENT_NAME) .short("i") .long("ide-id") @@ -48,6 +74,31 @@ fn main() { .help("the ide id is the process group id") .required(true) .takes_value(true))) + .subcommand(SubCommand::with_name(SYSTEMD_SUBCOMMAND_NAME) + .arg(Arg::with_name(SYSTEMD_UNIT_FOLDER_ARGUMENT_NAME) + .short("u") + .long("unit-folder") + .value_name("PATH_TO_SYSTEMD_UNIT_FOLDER") + .help("path to folder, where systemd expect unit files") + .default_value("$HOME/.config/systemd/user") + .takes_value(true) + .global(true)) + .arg(Arg::with_name(IDE_SERVICE_NAME_ARGUMENT_NAME) + .short("n") + .long("ide-service-name") + .value_name("IDE_SERVICE_NAME") + .help("the name of the ide serive") + .default_value("nixide") + .takes_value(true) + .global(true)) + .subcommand(SubCommand::with_name(INSTALL_SUBCOMMAND_NAME) + .arg(&start_ide_file_arg) + .arg(&start_ide_arguments_args) + .arg(Arg::with_name(FORCE_ARGUMENT_NAME) + .short("f") + .long("force") + .help("uninstall a service, when a service with the given name already exits."))) + .subcommand(SubCommand::with_name(UNINSTALL_SUBCOMMAND_NAME))) .get_matches(); match matches.subcommand() { @@ -91,7 +142,18 @@ fn main() { .arg("-g") .arg(ide_id) .output().unwrap(); - } + }, + (SYSTEMD_SUBCOMMAND_NAME, Some(system_cmd)) => { + let ide_service_name = system_cmd.value_of(IDE_SERVICE_NAME_ARGUMENT_NAME) + .and_then(|v| match v.ends_with(".service"){ + true => Some(v.to_owned()), + _ => Some(format!("{}.service",v)) + }).unwrap(); + let unit_folder = system_cmd.value_of(SYSTEMD_UNIT_FOLDER_ARGUMENT_NAME).unwrap(); + let service_file= shellexpand::full(&format!("{}/{}", unit_folder, ide_service_name)) + .and_then(|v| Ok(String::from(v))).unwrap(); + fs::remove_file(&service_file).expect(format!("could remove service file {}", service_file).as_ref()); + } _ => println!("-1") }; }