Skip to content

Commit

Permalink
feat: create and start vm via quickemu
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardotglobal committed Jul 27, 2023
1 parent dde2255 commit da55fcc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
11 changes: 11 additions & 0 deletions winapps-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Command;
use winapps::quickemu::{create_vm, run_vm};

fn cli() -> Command {
Command::new("winapps-cli")
Expand All @@ -8,6 +9,8 @@ fn cli() -> Command {
.allow_external_subcommands(true)
.subcommand(Command::new("check").about("Checks remote connection"))
.subcommand(Command::new("connect").about("Connects to remote"))
.subcommand(Command::new("create-vm").about("Create a windows 10 vm using quickemu"))
.subcommand(Command::new("run-vm").about("Start the vm using quickemu"))
}

fn main() {
Expand All @@ -23,6 +26,14 @@ fn main() {
Some(("connect", _)) => {
println!("Connecting to remote");
}
Some(("create-vm", _)) => {
println!("Creating windows 10 vm..");
create_vm();
}
Some(("run-vm", _)) => {
println!("Starting vm..");
run_vm();
}
Some((_, _)) => {
cli.about("Command not found, try existing ones!")
.print_help()
Expand Down
2 changes: 2 additions & 0 deletions winapps/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod quickemu;

use derive_new::new;
use home::home_dir;
use serde::{Deserialize, Serialize};
Expand Down
66 changes: 66 additions & 0 deletions winapps/src/quickemu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use home::home_dir;
use std::path::PathBuf;
use std::process::exit;
use std::process::Command;

pub(crate) fn get_data_dir() -> PathBuf {
let home = home_dir().expect("Could not find the home path!");
let data_dir = home.join(".local/share/winapps");

if !data_dir.exists() {
let dir = data_dir.clone();
println!(
"Data directory {:?} does not exist! Creating...",
dir.to_str()
);
std::fs::create_dir_all(dir).expect("Failed to create directory");
}

if !data_dir.is_dir() {
panic!("Data directory {:?} is not a directory!", data_dir.to_str());
}

data_dir
}

pub fn create_vm() {
let data_dir = get_data_dir();

let output = match Command::new("quickget")
.current_dir(data_dir)
.arg("windows")
.arg("10")
.output()
{
Ok(o) => o,
Err(e) => {
println!("Failed to execute quickget: {}", e);
println!("Please make sure quickget is installed and in your PATH");
exit(1);
}
};

println!("{}", String::from_utf8_lossy(&output.stdout));
}

pub fn run_vm() {
let data_dir = get_data_dir();

let output = match Command::new("quickemu")
.current_dir(data_dir)
.arg("--vm")
.arg("windows-10-22H2.conf")
.spawn()
.unwrap()
.wait_with_output()
{
Ok(o) => o,
Err(e) => {
println!("Failed to execute quickemu: {}", e);
println!("Please make sure quickemu is installed and in your PATH");
exit(1);
}
};

println!("{}", String::from_utf8_lossy(&output.stdout));
}

0 comments on commit da55fcc

Please sign in to comment.