Skip to content

Commit

Permalink
feat: use anyhow::Result throughout the codebase; comment unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
wzslr321 committed Oct 10, 2024
1 parent 93da68e commit f1929f7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
24 changes: 11 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use serde_json::to_string_pretty;

use crate::transform::init_registry;
use crate::utils;
use crate::{config::Config, git::clone_repo, git::extract_difference, git::open_repo};
use crate::git;
use crate::config;
use anyhow::Result;

#[derive(Parser, Debug, Clone)]
#[command(
Expand Down Expand Up @@ -75,7 +77,7 @@ struct Args {
origin: String,
}

// TODO add more credentials variants
// TODO: add more credentials variants
pub enum Credentials<'a> {
UsernamePassword {
username: &'a str,
Expand Down Expand Up @@ -133,14 +135,10 @@ pub fn run() -> Result<(), CliError> {
},
};

let mock_credentials = Credentials::UsernamePassword {
username: "wzslr321",
password: "TEST",
};

crate::git::fetch_remote(&repository, &args.origin, &mock_credentials).unwrap();
let credentials = utils::get_mock_credentials();
git::fetch_remote(&repository, &args.origin, &credentials).unwrap();

let diff = extract_difference(
let diff = git::extract_difference(
&repository,
&crate::git::DiffOptions::Branches {
from: &args.from_branch.unwrap(),
Expand Down Expand Up @@ -181,7 +179,7 @@ fn check_args_validity(args: Args) -> Result<(), CliError> {
}

fn try_retrieve_repo_from_path(path: Box<Path>) -> Result<Repository, CliError> {
match open_repo(&path) {
match git::open_repo(&path) {
Ok(repository) => {
info!("sucessfully retrieved repository from path");
Ok(repository)
Expand Down Expand Up @@ -220,7 +218,7 @@ fn try_retrieve_repo_from_url(
username,
password: &access_token.unwrap_or_else(|| "OnlyForTesting".to_string()), // ehttps://www.twitch.tv/directory/followingxpect("access_token must be specified, as it is the only supported authentication method for now"),
};
let cloned_repo = match clone_repo(&credentials, url, &clone_into_path) {
let cloned_repo = match git::clone_repo(&credentials, url, &clone_into_path) {
Ok(repo) => repo,
Err(e) => {
error!("Failed to retreive repository from url.\nError: {}", e);
Expand Down Expand Up @@ -261,9 +259,9 @@ fn setup_logging(tracing_level: u8) {
.init();
}

fn load_config(path: &Path) -> anyhow::Result<Config, CliError> {
fn load_config(path: &Path) -> anyhow::Result<config::Config, CliError> {
trace!("Starting loading config from {:?}", path);
match Config::load_from_file(path) {
match config::Config::load_from_file(path) {
Ok(config) => {
info!("Config loaded successfully");
Ok(config)
Expand Down
47 changes: 23 additions & 24 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::{Context, Result};
use anyhow::Result;
use serde::{Deserialize, Deserializer};
use tracing::error;
use std::cmp;
use std::fmt;
use std::path::Path;
use std::path::PathBuf;
use thiserror::Error;
use tracing::debug;
use url::Url;
Expand Down Expand Up @@ -35,16 +34,16 @@ pub struct OptionsConfig {
pub clone_into: Option<Box<Path>>,
}

#[derive(Debug, Deserialize)]
pub struct Trigger {
pub path: Box<Path>,

#[serde(default)]
pub pattern: Option<String>,

#[serde(default)]
pub analyze_dependencies: bool,
}
// #[derive(Debug, Deserialize)]
// pub struct Trigger {
// pub path: Box<Path>,
//
// #[serde(default)]
// pub pattern: Option<String>,
//
// #[serde(default)]
// pub analyze_dependencies: bool,
// }

#[derive(Debug, Deserialize)]
pub struct TransformStep {
Expand All @@ -55,17 +54,17 @@ pub struct TransformStep {

#[derive(Debug, Deserialize)]
pub struct Transform {
pub name: String,
// pub name: String,

#[serde(default)]
pub steps: Vec<TransformStep>,
}

#[derive(Debug, Deserialize)]
pub struct Matcher {
pub path: PathBuf,
pub pattern: String,
}
// #[derive(Debug, Deserialize)]
// pub struct Matcher {
// pub path: PathBuf,
// pub pattern: String,
// }

#[derive(Debug, Deserialize)]
pub enum AlertLevel {
Expand All @@ -76,17 +75,17 @@ pub enum AlertLevel {

#[derive(Debug, Deserialize)]
pub struct Action {
pub alert_level: AlertLevel,
pub message: String,
// pub alert_level: AlertLevel,
// pub message: String,
}

#[derive(Debug, Deserialize)]
pub struct Rule {
pub name: String,
pub trigger: Trigger,
// pub name: String,
// pub trigger: Trigger,
pub transform: Transform,
pub matcher: Matcher,
pub action: Action,
// pub matcher: Matcher,
// pub action: Action,
}

pub struct CustomStep {
Expand Down
1 change: 1 addition & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;
use serde::Serialize;
use thiserror::Error;
use anyhow::Result;

use git2::{Cred, RemoteCallbacks, Repository};
use std::str;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod utils;
mod transform;

use cli::CliError;
use anyhow::Result;

fn main() -> Result<(), CliError> {
cli::run()
Expand Down
1 change: 1 addition & 0 deletions src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::CustomStep;
use anyhow::Result;
use rhai::{Dynamic, Engine, Map, Scope};
use tracing::trace;
use std::collections::HashMap;
Expand Down
12 changes: 11 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{fs, path::Path};
use tracing::{info, trace};
use anyhow::Result;

pub fn prepare_directory(path: &Path) -> Result<(), anyhow::Error> {
use crate::cli::Credentials;

pub fn prepare_directory(path: &Path) -> Result<()> {
if path.exists() {
if path.read_dir()?.next().is_some() {
info!("Directory is not empty, removing existing files...");
Expand All @@ -15,3 +18,10 @@ pub fn prepare_directory(path: &Path) -> Result<(), anyhow::Error> {
trace!("Successfully prepared directory for cloning");
Ok(())
}

pub fn get_mock_credentials<'a>() -> &'a Credentials<'a> {
&Credentials::UsernamePassword {
username: "wzslr321",
password: "TEST",
}
}

0 comments on commit f1929f7

Please sign in to comment.