Skip to content

Commit d6713ea

Browse files
committed
Print list of persons
1 parent a9b63b7 commit d6713ea

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

cli-client/src/configuration.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ pub struct Settings {
66

77
pub fn get_configuration(path: std::path::PathBuf) -> Result<Settings, config::ConfigError> {
88
let settings = config::Config::builder()
9-
.add_source(
10-
config::File::new(path.to_str().unwrap(), config::FileFormat::Yaml)
11-
).build()?;
9+
.add_source(config::File::new(
10+
path.to_str().unwrap(),
11+
config::FileFormat::Yaml,
12+
))
13+
.build()?;
1214

1315
settings.try_deserialize::<Settings>()
1416
}
@@ -18,10 +20,10 @@ mod tests {
1820
use super::*;
1921
use std::path::PathBuf;
2022

21-
2223
#[test]
2324
fn parse_valid_config() {
24-
let configuration = get_configuration(PathBuf::from("tests/fixtures/valid_config.yaml")).unwrap();
25+
let configuration =
26+
get_configuration(PathBuf::from("tests/fixtures/valid_config.yaml")).unwrap();
2527
assert_eq!(configuration.api_key, "abc");
2628
assert_eq!(configuration.base_url, "https://example.com");
2729
}

cli-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub mod configuration;
2-
pub mod remote_client;
32
pub mod local_client;
3+
pub mod remote_client;

cli-client/src/local_client.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
use serde_json::Value;
22

33
pub fn add_strike(name: &str, db_path: &std::path::PathBuf) -> Value {
4-
let db = std::fs::read_to_string(db_path).unwrap_or_else(|_| {
5-
"{}".to_string()
6-
});
4+
let db = std::fs::read_to_string(db_path).unwrap_or_else(|_| "{}".to_string());
75
let updated_db = update_strikes(name, serde_json::from_str(&db).unwrap());
86
std::fs::write(db_path, serde_json::to_string_pretty(&updated_db).unwrap()).unwrap();
9-
7+
108
updated_db
119
}
1210

13-
fn update_strikes(name: &str, db: Value)-> Value {
11+
fn update_strikes(name: &str, db: Value) -> Value {
1412
let mut db = db.as_object().unwrap().clone();
1513
let count = db.get(name).unwrap_or(&Value::Null).as_u64().unwrap_or(0);
1614
db.insert(name.to_string(), Value::from(count + 1));
17-
15+
1816
Value::Object(db)
1917
}
2018

@@ -45,15 +43,15 @@ mod unit_tests {
4543
#[cfg(test)]
4644
mod integration_tests {
4745
use super::*;
48-
use std::path::PathBuf;
4946
use serde_json::json;
47+
use std::path::PathBuf;
5048

5149
#[test]
5250
fn it_adds_a_strike() {
5351
let db_path = PathBuf::from("tests/fixtures/db.json");
54-
52+
5553
let db = add_strike("guenther", &db_path);
56-
54+
5755
std::fs::remove_file(db_path).unwrap();
5856

5957
assert_eq!(db, json!({"guenther": 1}));
@@ -64,9 +62,9 @@ mod integration_tests {
6462
let db_path = PathBuf::from("tests/fixtures/db_0.json");
6563
add_strike("guenther", &db_path);
6664
add_strike("heinz", &db_path);
67-
65+
6866
let db = add_strike("guenther", &db_path);
69-
67+
7068
std::fs::remove_file(db_path).unwrap();
7169

7270
assert_eq!(db, json!({"guenther": 2, "heinz": 1}));

cli-client/src/main.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
1-
use clap::Parser;
1+
use clap::{Parser, Subcommand};
22
use std::path::PathBuf;
33
use strikes::{
44
configuration::get_configuration, local_client::add_strike, remote_client::check_health,
55
};
66

7+
#[derive(Subcommand, Clone, Debug)]
8+
enum Command {
9+
#[command(about = "Adds a strike to the specified person.")]
10+
Strike { name: String },
11+
#[command(about = "Lists all the persons and the number of strikes they have")]
12+
Ls,
13+
}
14+
715
#[derive(Debug, Parser)]
16+
#[command(
17+
name = "Strikes CLI",
18+
version = "0.1",
19+
about = "Manage strikes for people",
20+
long_about = "This is a command-line tool for blaming people."
21+
)]
822
struct Cli {
9-
name: String,
23+
#[arg(
24+
short,
25+
long,
26+
help = "Specify the path to the configuration file where the strikes are stored."
27+
)]
1028
config_path: Option<std::path::PathBuf>,
29+
#[command(subcommand)]
30+
command: Option<Command>,
1131
}
1232

1333
#[tokio::main]
1434
async fn main() {
1535
let args = Cli::parse();
36+
1637
let home = &std::env::var("HOME").unwrap();
1738
let config_path = PathBuf::from(home).join(".strikes/configuration.yaml");
18-
let config = get_configuration(args.config_path.unwrap_or(config_path))
39+
let _config = get_configuration(args.config_path.unwrap_or(config_path))
1940
.expect("Faild to read configuration.");
2041

21-
check_health(config.base_url, config.api_key).await;
42+
// check_health(config.base_url, config.api_key).await;
2243
let db_path = PathBuf::from(home).join(".strikes/db.json");
23-
add_strike(&args.name, &db_path);
44+
45+
match args.command.unwrap() {
46+
Command::Strike { name } => {
47+
add_strike(&name, &db_path);
48+
}
49+
Command::Ls => {
50+
let db = std::fs::read_to_string(&db_path).unwrap_or_else(|_| "{}".to_string());
51+
println!("{}", db);
52+
}
53+
}
2454
}

cli-client/src/remote_client.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ pub async fn check_health(base_url: String, api_key: String) {
99
.await
1010
.expect("Failed to execute request");
1111

12-
println!("Try to reach remote location: {:?}", response.text().await.unwrap());
12+
println!(
13+
"Try to reach remote location: {:?}",
14+
response.text().await.unwrap()
15+
);
1316
}

0 commit comments

Comments
 (0)