mirror of https://github.com/sgoudham/git-view.git
refactor: Perform massive refactor
parent
d37764c5a5
commit
95953b87f3
@ -1,34 +1,37 @@
|
|||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum AppError {
|
pub enum ErrorType {
|
||||||
CommandFailedToExecute(String),
|
CommandFailed,
|
||||||
CommandError(String),
|
CommandError,
|
||||||
MissingGitRepository(String),
|
MissingGitRepository,
|
||||||
MissingGitRemote(String),
|
MissingGitRemote,
|
||||||
InvalidGitUrl(String),
|
InvalidGitUrl,
|
||||||
InvalidUtf8(String),
|
InvalidUtf8,
|
||||||
|
IOError,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct AppError {
|
||||||
|
pub error_type: ErrorType,
|
||||||
|
pub error_str: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<std::io::Error> for AppError {
|
impl From<std::io::Error> for AppError {
|
||||||
fn from(error: std::io::Error) -> Self {
|
fn from(error: std::io::Error) -> Self {
|
||||||
AppError::CommandFailedToExecute(error.to_string())
|
AppError::new(ErrorType::IOError, error.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<std::str::Utf8Error> for AppError {
|
impl From<std::str::Utf8Error> for AppError {
|
||||||
fn from(error: std::str::Utf8Error) -> Self {
|
fn from(error: std::str::Utf8Error) -> Self {
|
||||||
AppError::InvalidUtf8(error.to_string())
|
AppError::new(ErrorType::InvalidUtf8, error.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppError {
|
impl AppError {
|
||||||
pub fn print(&self) -> &String {
|
pub fn new(error_type: ErrorType, error_str: String) -> Self {
|
||||||
match self {
|
Self {
|
||||||
AppError::CommandFailedToExecute(str)
|
error_type,
|
||||||
| AppError::MissingGitRepository(str)
|
error_str,
|
||||||
| AppError::MissingGitRemote(str)
|
|
||||||
| AppError::CommandError(str)
|
|
||||||
| AppError::InvalidGitUrl(str)
|
|
||||||
| AppError::InvalidUtf8(str) => str,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,81 @@
|
|||||||
|
use mockall::automock;
|
||||||
|
use std::process::{Command, Output};
|
||||||
|
|
||||||
|
use crate::error::AppError;
|
||||||
|
|
||||||
|
pub(crate) enum Git<'a> {
|
||||||
|
IsValidRepository,
|
||||||
|
LocalBranch,
|
||||||
|
DefaultRemote,
|
||||||
|
TrackedRemote(&'a str),
|
||||||
|
UpstreamBranch(&'a str),
|
||||||
|
IsValidRemote(&'a str),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) enum GitOutput {
|
||||||
|
Ok(String),
|
||||||
|
Err(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
pub(crate) trait GitCommand {
|
||||||
|
fn execute(&self) -> Result<GitOutput, AppError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Git<'a> {
|
||||||
|
fn command(&self) -> Result<Output, std::io::Error> {
|
||||||
|
match *self {
|
||||||
|
Git::IsValidRepository => Command::new("git")
|
||||||
|
.arg("rev-parse")
|
||||||
|
.arg("--is-inside-work-tree")
|
||||||
|
.output(),
|
||||||
|
Git::LocalBranch => Command::new("git")
|
||||||
|
.arg("symbolic-ref")
|
||||||
|
.arg("-q")
|
||||||
|
.arg("--short")
|
||||||
|
.arg("HEAD")
|
||||||
|
.output(),
|
||||||
|
Git::DefaultRemote => Command::new("git")
|
||||||
|
.arg("config")
|
||||||
|
.arg("open.default.remote")
|
||||||
|
.output(),
|
||||||
|
Git::TrackedRemote(branch) => Command::new("git")
|
||||||
|
.arg("config")
|
||||||
|
.arg(format!("branch.{}.remote", branch))
|
||||||
|
.output(),
|
||||||
|
Git::UpstreamBranch(branch) => Command::new("git")
|
||||||
|
.arg("config")
|
||||||
|
.arg(format!("branch.{}.merge", branch))
|
||||||
|
.output(),
|
||||||
|
Git::IsValidRemote(remote) => Command::new("git")
|
||||||
|
.arg("ls-remote")
|
||||||
|
.arg("--get-url")
|
||||||
|
.arg(remote)
|
||||||
|
.output(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trim(&self, bytes: &[u8]) -> Result<String, AppError> {
|
||||||
|
let mut utf_8_string = String::from(std::str::from_utf8(bytes)?.trim());
|
||||||
|
|
||||||
|
if utf_8_string.ends_with('\n') {
|
||||||
|
utf_8_string.pop();
|
||||||
|
if utf_8_string.ends_with('\r') {
|
||||||
|
utf_8_string.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(utf_8_string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> GitCommand for Git<'a> {
|
||||||
|
fn execute(&self) -> Result<GitOutput, AppError> {
|
||||||
|
let command = self.command()?;
|
||||||
|
if command.status.success() {
|
||||||
|
Ok(GitOutput::Ok(self.trim(&command.stdout)?))
|
||||||
|
} else {
|
||||||
|
Ok(GitOutput::Err(self.trim(&command.stderr)?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue