You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
amethyst/src/builder/git.rs

100 lines
2.1 KiB
Rust

use std::path::{Path, PathBuf};
use crate::{
error::{AppError, AppResult},
internal::commands::ShellCommand,
};
#[derive(Debug, Default)]
pub struct GitCloneBuilder {
url: String,
directory: PathBuf,
}
impl GitCloneBuilder {
pub fn url<S: ToString>(mut self, url: S) -> Self {
self.url = url.to_string();
self
}
pub fn directory<P: AsRef<Path>>(mut self, path: P) -> Self {
self.directory = path.as_ref().into();
self
}
pub async fn clone(self) -> AppResult<()> {
let result = ShellCommand::git()
.arg("clone")
.arg(self.url)
.arg(self.directory)
.wait_with_output()
.await?;
if result.status.success() {
Ok(())
} else {
Err(AppError::Other(result.stderr))
}
}
}
#[derive(Debug, Default)]
pub struct GitPullBuilder {
directory: PathBuf,
}
impl GitPullBuilder {
pub fn directory<P: AsRef<Path>>(mut self, path: P) -> Self {
self.directory = path.as_ref().into();
self
}
pub async fn pull(self) -> AppResult<()> {
let result = ShellCommand::git()
.arg("-C")
.arg(self.directory)
.arg("pull")
.wait_with_output()
.await?;
if result.status.success() {
Ok(())
} else {
Err(AppError::Other(result.stderr))
}
}
}
#[derive(Debug, Default)]
pub struct GitResetBuilder {
directory: PathBuf,
}
impl GitResetBuilder {
pub fn directory<P: AsRef<Path>>(mut self, path: P) -> Self {
self.directory = path.as_ref().into();
self
}
pub async fn reset(self) -> AppResult<()> {
let result = ShellCommand::git()
.arg("-C")
.arg(self.directory)
.arg("reset")
.arg("HEAD")
.arg("--hard")
.wait_with_output()
.await?;
if result.status.success() {
Ok(())
} else {
Err(AppError::Other(result.stderr))
}
}
}