Add basic cli and config file parsing

This commit is contained in:
LDprg 2023-07-13 20:02:57 +02:00
parent c7033b1750
commit 2866a1d1b2
4 changed files with 109 additions and 2 deletions

View File

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

View File

@ -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!(),
}
}

View File

@ -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"

View File

@ -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
}