From 2866a1d1b235ee160595f5899fa1b655786c6bea Mon Sep 17 00:00:00 2001 From: LDprg Date: Thu, 13 Jul 2023 20:02:57 +0200 Subject: [PATCH] Add basic cli and config file parsing --- winapps-cli/Cargo.toml | 1 + winapps-cli/src/main.rs | 34 +++++++++++++++++-- winapps/Cargo.toml | 4 +++ winapps/src/lib.rs | 72 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) diff --git a/winapps-cli/Cargo.toml b/winapps-cli/Cargo.toml index abc0b8b..6837cf7 100644 --- a/winapps-cli/Cargo.toml +++ b/winapps-cli/Cargo.toml @@ -7,4 +7,5 @@ default-run = "winapps-cli" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = "4.3.11" winapps = { path = "../winapps" } diff --git a/winapps-cli/src/main.rs b/winapps-cli/src/main.rs index acf379e..df3aa82 100644 --- a/winapps-cli/src/main.rs +++ b/winapps-cli/src/main.rs @@ -1,3 +1,33 @@ -fn main() { - println!("Test lib: {}", winapps::add(1, 2)); +use clap::Command; + +fn cli() -> Command { + Command::new("winapps-cli") + .about("The winapps-cli is a command line interface for winapps") + .subcommand_required(true) + .arg_required_else_help(true) + .allow_external_subcommands(true) + .subcommand(Command::new("check").about("Checks remote connection")) + .subcommand(Command::new("connect").about("Connects to remote")) +} + +fn main() { + let cli = cli(); + let matches = cli.clone().get_matches(); + + match matches.subcommand() { + Some(("check", _)) => { + println!("Checking remote connection"); + + let _config = winapps::load_config(None); + } + Some(("connect", _)) => { + println!("Connecting to remote"); + } + Some((_, _)) => { + cli.about("Command not found try existing ones!") + .print_help() + .unwrap(); + } + _ => unreachable!(), + } } diff --git a/winapps/Cargo.toml b/winapps/Cargo.toml index 95f2d32..abd213a 100644 --- a/winapps/Cargo.toml +++ b/winapps/Cargo.toml @@ -6,3 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +derive-new = "0.5.9" +home = "0.5.5" +serde = { version = "1.0.171", features = ["derive"] } +toml = "0.7.6" diff --git a/winapps/src/lib.rs b/winapps/src/lib.rs index 211bfff..e8e589e 100644 --- a/winapps/src/lib.rs +++ b/winapps/src/lib.rs @@ -1,3 +1,75 @@ +use derive_new::new; +use home::home_dir; +use serde::{Deserialize, Serialize}; +use std::io::Write; +use std::{ + fs::{self, File}, + path::Path, +}; + +#[derive(new, Debug, Deserialize, Serialize)] +pub struct Config { + #[new(value = "HostConfig::new()")] + host: HostConfig, + #[new(value = "RemoteConfig::new()")] + rdp: RemoteConfig, +} + +#[derive(new, Debug, Deserialize, Serialize)] +pub struct HostConfig { + #[new(value = "\"X11\".to_string()")] + display: String, +} + +#[derive(new, Debug, Deserialize, Serialize)] +pub struct RemoteConfig { + #[new(value = "\"RDPWindows\".to_string()")] + host: String, + #[new(value = "\"WORKGROUP\".to_string()")] + domain: String, + #[new(value = "\"RDPUser\".to_string()")] + username: String, + #[new(value = "\"RDPPass\".to_string()")] + password: String, +} + +pub trait RemoteClient { + fn check_depends(&self) -> bool { + panic!("Dependency check not implemented!"); + } + + fn load_config(&self, path: &str); + + fn run_app(&self, app: &str); +} + +pub fn load_config(path: Option<&str>) -> Config { + let home = home_dir().expect("Could not find the home path!"); + let default = &format!("{}{}", home.to_str().unwrap(), "/.config/winapps/"); + let path = Path::new(path.unwrap_or(default)); + let config = Config::new(); + + if !path.exists() { + println!("{:?} does not exist! Creating...", path.to_str()); + fs::create_dir_all(path).expect("Failed to create directory"); + } + + let config_file = path.join("config.toml"); + + if !config_file.exists() { + let mut config_file = + File::create(&config_file).expect("Failed to create configuration file"); + + let gen_config = toml::to_string(&config).unwrap(); + write!(config_file, "{}", gen_config).expect("Failed to write configuration file"); + } + + let config_file = fs::read_to_string(config_file).expect("Failed to read configuration file"); + let config: Config = toml::from_str(config_file.as_str()).unwrap(); + + config +} + pub fn add(left: usize, right: usize) -> usize { left + right }