feat: Added command execution

Also renamed Command to UMUCommand, and CommandBuilder to UMUCommandBuilder

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-12-30 08:45:15 +11:00
parent 5599d44f9f
commit 02cd7a6e7a
3 changed files with 23 additions and 17 deletions

View File

@@ -1,14 +1,19 @@
use std::{io, process::{Command, Output}};
use tempfile::NamedTempFile;
pub struct Command {
inner: String,
pub struct UMUCommand {
inner: Command,
file: NamedTempFile
}
impl Command {
pub fn new(inner: String, file: NamedTempFile) -> Self {
impl UMUCommand {
pub fn new(inner: Command, file: NamedTempFile) -> Self {
Self {
inner,
file
}
}
pub fn run(&mut self) -> io::Result<Output> {
self.inner.output()
}
}

View File

@@ -1,11 +1,11 @@
use std::{io::Write, path::PathBuf};
use std::{io::Write, path::PathBuf, process::Command};
use serde::Serialize;
use crate::command::Command;
use crate::command::UMUCommand;
#[derive(Debug, Default, Clone, Serialize)]
pub struct CommandBuilder {
pub struct UMUCommandBuilder {
#[serde(skip)]
executable: PathBuf,
umu: UMU
@@ -21,25 +21,25 @@ struct UMU {
}
impl CommandBuilder {
impl UMUCommandBuilder {
pub fn new<T: Into<PathBuf>>(executable: T) -> Self {
Self {
executable: executable.into(),
..Default::default()
}
}
pub fn build(self) -> Command {
pub fn build(self) -> UMUCommand {
let mut file = tempfile::NamedTempFile::new().unwrap();
let serialized = toml::to_string(&self).unwrap();
println!("Serialized: \n{}", serialized);
file.write_all(serialized.as_bytes()).unwrap();
let mut command = Vec::new();
command.push(self.executable.as_os_str().to_str().unwrap());
command.push("--config");
command.push(file.path().to_str().unwrap());
let mut command = Command::new(self.executable);
command
.arg("--config")
.arg(file.path());
Command::new(command.join(" "), file)
UMUCommand::new(command, file)
}
pub fn game_id(mut self, game_id: String) -> Self {
self.umu.game_id = Some(game_id);

View File

@@ -1,9 +1,10 @@
use crate::command_builder::CommandBuilder;
use crate::command_builder::UMUCommandBuilder;
#[test]
pub fn print_sample() {
CommandBuilder::new("/usr/bin/umu-run")
UMUCommandBuilder::new("/usr/bin/umu-run")
.game_id("0".to_owned())
.wineprefix("~/.wine".into())
.build();
}
}