Skip to content

Commit dff53ee

Browse files
committed
feat!: move locales to config dir + add test-config command + new -c
argument to specify config path
1 parent 8142c12 commit dff53ee

File tree

7 files changed

+89
-35
lines changed

7 files changed

+89
-35
lines changed
File renamed without changes.
File renamed without changes.

src/cli.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub struct Cli {
2525
// #[arg(short = 'j', long = "json")]
2626
// pub output_json: bool,
2727

28+
#[arg(short = 'c', long = "config-dir")]
29+
pub config_directory_path: Option<PathBuf>,
30+
2831
#[command(flatten)]
2932
pub verbose: clap_verbosity_flag::Verbosity,
3033
}
@@ -52,6 +55,9 @@ pub enum Commands {
5255
/// Launch into interactive mode
5356
#[command(name = "inter", alias = "interactive", alias = "int")]
5457
Interactive,
58+
/// Test what configuration files are found
59+
#[command(name = "test-config", alias = "test-conf")]
60+
TestConfig,
5561
}
5662

5763
#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]

src/interactive.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
fs,
33
io::{self, Write},
4-
path::PathBuf,
4+
path::{Path, PathBuf},
55
time::Instant,
66
};
77

@@ -12,7 +12,7 @@ use yansi::Paint;
1212

1313
use crate::{cli::LanguageCode, localizer::Localizer, scraper::OpTcgScraper, storage::DataStore};
1414

15-
pub fn show_interactive() -> Result<()> {
15+
pub fn show_interactive(config_dir: &Path) -> Result<()> {
1616
println!("{}", "+---------------------------+".yellow());
1717
println!(
1818
"{} {} {}",
@@ -83,7 +83,7 @@ pub fn show_interactive() -> Result<()> {
8383

8484
let download_images = is_yes(value);
8585

86-
let localizer = Localizer::load(language)?;
86+
let localizer = Localizer::load(config_dir, language)?;
8787
let scraper = OpTcgScraper::new(&localizer);
8888
let store = DataStore::new(&data_dir, language);
8989

src/localizer.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::{collections::HashMap, fs, path::PathBuf};
2-
3-
use anyhow::{Context, Result};
1+
use anyhow::{ensure, Context, Result};
42
use log::{debug, info};
53
use serde::{Deserialize, Serialize};
4+
use std::{collections::HashMap, fs, path::Path};
65

76
use crate::cli::LanguageCode;
87

@@ -43,25 +42,52 @@ impl Localizer {
4342
Self::reverse_search(&self.rarities, value)
4443
}
4544

46-
pub fn load(language: LanguageCode) -> Result<Localizer> {
45+
pub fn find_locales(config_dir: &Path) -> Result<()> {
46+
ensure!(
47+
config_dir.exists(),
48+
format!("config directory not found: {}", config_dir.display())
49+
);
50+
51+
let entries = fs::read_dir(config_dir)?;
52+
println!("config directory: {}", config_dir.display());
53+
54+
for entry in entries {
55+
let entry = entry?;
56+
let file_name = entry.file_name();
57+
println!("- {}", file_name.to_string_lossy());
58+
}
59+
60+
Ok(())
61+
}
62+
63+
pub fn load(config_dir: &Path, language: LanguageCode) -> Result<Localizer> {
4764
match language {
48-
LanguageCode::ChineseHongKong => Self::load_from_file("zh_hk"),
49-
LanguageCode::ChineseSimplified => Self::load_from_file("zh_cn"),
50-
LanguageCode::ChineseTaiwan => Self::load_from_file("zh_tw"),
51-
LanguageCode::English => Self::load_from_file("en"),
52-
LanguageCode::EnglishAsia => Self::load_from_file("en_asia"),
53-
LanguageCode::Japanese => Self::load_from_file("jp"),
54-
LanguageCode::Thai => Self::load_from_file("th"),
65+
LanguageCode::ChineseHongKong => Self::load_from_file(config_dir, "zh_hk"),
66+
LanguageCode::ChineseSimplified => Self::load_from_file(config_dir, "zh_cn"),
67+
LanguageCode::ChineseTaiwan => Self::load_from_file(config_dir, "zh_tw"),
68+
LanguageCode::English => Self::load_from_file(config_dir, "en"),
69+
LanguageCode::EnglishAsia => Self::load_from_file(config_dir, "en_asia"),
70+
LanguageCode::Japanese => Self::load_from_file(config_dir, "jp"),
71+
LanguageCode::Thai => Self::load_from_file(config_dir, "th"),
5572
}
5673
}
5774

58-
pub fn load_from_file(locale: &str) -> Result<Localizer> {
59-
let path = format!("./locales/{}.toml", locale);
60-
let path = PathBuf::from(path);
61-
info!("load {} locale from: {}", locale, path.to_string_lossy());
75+
pub fn load_from_file(config_dir: &Path, locale: &str) -> Result<Localizer> {
76+
ensure!(
77+
config_dir.exists(),
78+
format!("config directory not found: {}", config_dir.display())
79+
);
80+
81+
let locale_path = config_dir.join(format!("{}.toml", locale));
82+
ensure!(
83+
locale_path.exists(),
84+
format!("locale file not found: {}", locale_path.display())
85+
);
86+
87+
info!("load {} locale from: {}", locale, locale_path.display());
6288

63-
let locale_data = fs::read_to_string(&path)
64-
.with_context(|| format!("Failed to open file: {}", path.display()))?;
89+
let locale_data = fs::read_to_string(&locale_path)
90+
.with_context(|| format!("Failed to open file: {}", locale_path.display()))?;
6591
debug!("loaded {}", locale_data);
6692

6793
let localizer: Localizer = toml::from_str(&locale_data)?;

src/main.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs, path::PathBuf, time::Instant};
1+
use std::{env, fs, path::Path, process::ExitCode, time::Instant};
22

33
use anyhow::{bail, Result};
44
use clap::Parser;
@@ -17,30 +17,52 @@ mod pack;
1717
mod scraper;
1818
mod storage;
1919

20-
fn main() -> Result<()> {
20+
fn main() -> ExitCode {
2121
let args = Cli::parse();
2222
env_logger::Builder::new()
2323
.filter_level(args.verbose.log_level_filter())
2424
.init();
2525

26-
process_args(args)?;
27-
Ok(())
26+
match process_args(args) {
27+
Ok(_) => ExitCode::SUCCESS,
28+
Err(e) => {
29+
error!("{}", e);
30+
ExitCode::FAILURE
31+
}
32+
}
2833
}
2934

3035
fn process_args(args: Cli) -> Result<()> {
36+
let default_config_dir = env::current_dir()?.join("config");
37+
let config_dir = args.config_directory_path.unwrap_or(default_config_dir);
38+
info!("using configuration from: {}", config_dir.display());
39+
3140
match args.command {
32-
cli::Commands::Packs => list_packs(args.language),
33-
cli::Commands::Cards { pack_id } => list_cards(args.language, &pack_id.to_string_lossy()),
34-
cli::Commands::Interactive => interactive::show_interactive(),
41+
cli::Commands::Packs => list_packs(&config_dir, args.language),
42+
cli::Commands::Cards { pack_id } => {
43+
list_cards(&config_dir, args.language, &pack_id.to_string_lossy())
44+
}
45+
cli::Commands::Interactive => interactive::show_interactive(&config_dir),
3546
cli::Commands::Images {
3647
pack_id,
3748
output_dir,
38-
} => download_images(args.language, &pack_id.to_string_lossy(), output_dir),
49+
} => download_images(
50+
&config_dir,
51+
args.language,
52+
&pack_id.to_string_lossy(),
53+
&output_dir,
54+
),
55+
cli::Commands::TestConfig => Localizer::find_locales(&config_dir),
3956
}
4057
}
4158

42-
fn download_images(language: LanguageCode, pack_id: &str, output_dir: PathBuf) -> Result<()> {
43-
let localizer = Localizer::load(language)?;
59+
fn download_images(
60+
config_dir: &Path,
61+
language: LanguageCode,
62+
pack_id: &str,
63+
output_dir: &Path,
64+
) -> Result<()> {
65+
let localizer = Localizer::load(config_dir, language)?;
4466
let scraper = OpTcgScraper::new(&localizer);
4567

4668
if output_dir.exists() {
@@ -51,7 +73,7 @@ fn download_images(language: LanguageCode, pack_id: &str, output_dir: PathBuf) -
5173
);
5274
}
5375

54-
match fs::create_dir_all(&output_dir) {
76+
match fs::create_dir_all(output_dir) {
5577
Ok(_) => info!("successfully created `{}`", output_dir.display()),
5678
Err(e) => bail!("failed to create `{}`: {}", output_dir.display(), e),
5779
}
@@ -98,8 +120,8 @@ fn download_images(language: LanguageCode, pack_id: &str, output_dir: PathBuf) -
98120
Ok(())
99121
}
100122

101-
fn list_packs(language: LanguageCode) -> Result<()> {
102-
let localizer = Localizer::load(language)?;
123+
fn list_packs(config_dir: &Path, language: LanguageCode) -> Result<()> {
124+
let localizer = Localizer::load(config_dir, language)?;
103125
let scraper = OpTcgScraper::new(&localizer);
104126

105127
info!("fetching all pack ids...");
@@ -117,8 +139,8 @@ fn list_packs(language: LanguageCode) -> Result<()> {
117139
Ok(())
118140
}
119141

120-
fn list_cards(language: LanguageCode, pack_id: &str) -> Result<()> {
121-
let localizer = Localizer::load(language)?;
142+
fn list_cards(config_dir: &Path, language: LanguageCode, pack_id: &str) -> Result<()> {
143+
let localizer = Localizer::load(config_dir, language)?;
122144
let scraper = OpTcgScraper::new(&localizer);
123145

124146
info!("fetching all cards...");

0 commit comments

Comments
 (0)