Skip to content

Commit

Permalink
Add support for config
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Aug 18, 2023
1 parent 33d78ea commit 84ce179
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ jobs:
run: git clone https://github.com/coredump-ch/gitcash-demo-repo/
- name: Build
run: cargo build
- name: Create config
run: echo -e "repo_path = 'gitcash-demo-repo'\naccount = 'pos:fridge'\ngit_name = 'CI'\ngit_email = '[email protected]'" > config.toml
- name: Calculate balances
run: target/debug/gitcash --repo-path gitcash-demo-repo balances
run: target/debug/gitcash balances

rustfmt:
name: Check code formatting
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
/config.toml
2 changes: 2 additions & 0 deletions gitcash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
inquire = "0.6.2"
libgitcash = { path = "../libgitcash/" }
serde = { version = "1", features = ["derive"] }
toml = "0.7"
tracing = "0.1"
tracing-subscriber = "0.3"
38 changes: 38 additions & 0 deletions gitcash/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::path::{Path, PathBuf};

use anyhow::{bail, Context};
use libgitcash::{Account, AccountType};
use serde::{Deserialize, Serialize};

/// GitCash configuration
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
/// Path to the GitCash repository to use
pub repo_path: PathBuf,

/// Account corresponding to this PoS
pub account: Account,

/// Name to use for git commits
pub git_name: String,
/// E-mail to use for git commits
pub git_email: String,
}

impl Config {
/// Load config from the specified config path
pub fn load(config_path: &Path) -> anyhow::Result<Self> {
let config_string: String = std::fs::read_to_string(config_path)
.context(format!("Could not read config from {:?}", config_path))?;
let config: Self = toml::from_str(&config_string)
.context(format!("Could not parse config at {:?}", config_path))?;
if config.account.account_type != AccountType::PointOfSale {
bail!(
"Account type must be {:?}, not {:?}",
AccountType::PointOfSale,
config.account.account_type
);
}
Ok(config)
}
}
19 changes: 13 additions & 6 deletions gitcash/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};

use clap::{Parser, Subcommand};
use config::Config;
use inquire::validator::{ErrorMessage, Validation};
use libgitcash::{Account, AccountType, Repo, Transaction};
use tracing::metadata::LevelFilter;

mod config;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short, long)]
repo_path: std::path::PathBuf,
#[arg(short, long, default_value = "config.toml")]
config: PathBuf,

#[command(subcommand)]
command: Command,
Expand Down Expand Up @@ -38,9 +41,13 @@ pub fn main() -> anyhow::Result<()> {
// Parse args
let args = Args::parse();

// Parse config
let config = Config::load(&args.config)?;

// Open repo
let repo = Repo::open(&args.repo_path)?;
let repo = Repo::open(&config.repo_path)?;

// Run command
match args.command {
Command::Accounts => {
println!("Accounts:");
Expand Down Expand Up @@ -81,7 +88,7 @@ pub fn main() -> anyhow::Result<()> {
}
}
Command::Cli => {
println!("Welcome to the GitCash CLI!");
println!("Welcome to the GitCash CLI for {}!", config.git_name);

// Get list of valid user account names
let usernames = Arc::new(
Expand Down Expand Up @@ -142,7 +149,7 @@ pub fn main() -> anyhow::Result<()> {

repo.create_transaction(&Transaction {
from: Account::user(name),
to: Account::point_of_sale("TODO"),
to: config.account.clone(),
amount: repo.convert_amount(amount),
description: None,
meta: None,
Expand Down
15 changes: 15 additions & 0 deletions libgitcash/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::error::Error;

#[derive(Debug, Deserialize, Serialize)]
pub struct RepoConfig {
pub name: String,
pub currency: Currency,
}

impl RepoConfig {
/// Load repo config in the specified repo path
pub fn load(repo_path: &Path) -> Result<Self, Error> {
let config_string = std::fs::read_to_string(repo_path.join("gitcash.toml"))
.map_err(|e| Error::RepoError(format!("Could not read gitcash.toml: {}", e)))?;
let config: RepoConfig = toml::from_str(&config_string)
.map_err(|e| Error::RepoError(format!("Could not parse gitcash.toml: {}", e)))?;
Ok(config)
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Currency {
pub code: String,
Expand Down
5 changes: 1 addition & 4 deletions libgitcash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ impl Repo {
};

// Read config
let config_string = std::fs::read_to_string(repo_path.join("gitcash.toml"))
.map_err(|e| Error::RepoError(format!("Could not read gitcash.toml: {}", e)))?;
let config: RepoConfig = toml::from_str(&config_string)
.map_err(|e| Error::RepoError(format!("Could not parse gitcash.toml: {}", e)))?;
let config = RepoConfig::load(repo_path)?;

// Traverse commits from oldest to newest, extract transactions
let mut revwalk = repo.revwalk()?;
Expand Down

0 comments on commit 84ce179

Please sign in to comment.