add systemd unstall command

This commit is contained in:
stubbfelnewpc
2020-06-23 00:15:57 +02:00
parent 28f9a40d68
commit 35e5c29a60
4 changed files with 225 additions and 20 deletions

View File

@@ -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")
};
}