Skip to content

Commit 73dbd67

Browse files
committed
Ok
1 parent c4ef8d2 commit 73dbd67

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

crates/app/src/configuration/global_config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub struct GlobalConfig {
5757
pub show_shortcuts: bool,
5858
#[serde(default = "default_export_directory")]
5959
pub export_directory: PathBuf,
60+
pub log_file: Option<PathBuf>
6061
}
6162

6263
fn default_url_template() -> String {
@@ -99,6 +100,7 @@ impl TryFrom<&PathBuf> for GlobalConfig {
99100
show_shortcuts: true,
100101
export_directory: default_export_directory(),
101102
consumer: ConsumerConfig::default(),
103+
log_file: None,
102104
})
103105
}
104106
}

crates/app/src/configuration/workspace.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct Workspace {
1919
pub path: PathBuf,
2020
/// Specific config file of Yozefu
2121
config_file: PathBuf,
22+
/// Specific log file of Yozefu
23+
log_file: PathBuf,
2224
}
2325

2426
impl Default for Workspace {
@@ -28,6 +30,9 @@ impl Default for Workspace {
2830
config_file: Self::yozefu_directory()
2931
.unwrap()
3032
.join(Self::CONFIG_FILENAME),
33+
log_file: Self::yozefu_directory()
34+
.unwrap()
35+
.join(Self::LOGS_FILENAME),
3136
}
3237
}
3338
}
@@ -38,10 +43,11 @@ impl Workspace {
3843
pub const THEMES_FILENAME: &str = "themes.json";
3944
pub const FILTERS_DIRECTORY: &str = "filters";
4045

41-
pub fn new(directory: &Path, config_file: &Path) -> Self {
46+
pub fn new(directory: &Path, config_file: &Path, log_file: PathBuf) -> Self {
4247
Self {
4348
path: directory.to_path_buf(),
4449
config_file: config_file.to_path_buf(),
50+
log_file,
4551
}
4652
}
4753

@@ -61,7 +67,7 @@ impl Workspace {
6167

6268
/// Returns the name of the logs file
6369
pub fn logs_file(&self) -> PathBuf {
64-
self.path.join(Self::LOGS_FILENAME)
70+
self.log_file.clone()
6571
}
6672

6773
/// Returns the name of the logs file

crates/app/tests/global_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn check_backwards_compatibility() {
1919
export_directory: PathBuf::from("./yozefu-exports"),
2020
consumer: ConsumerConfig::default(),
2121
highlighter_theme: None,
22+
log_file: None,
2223
};
2324

2425
let json = serde_json::to_string_pretty(&config).unwrap();

crates/command/src/global_args.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use app::configuration::Workspace;
1+
use app::configuration::{GlobalConfig, Workspace};
2+
use chrono::naive::serde;
23
use clap::Args;
3-
use std::path::PathBuf;
4+
use std::{fs, path::PathBuf};
45
use tracing::debug;
56

67
#[derive(Args, Clone, Debug, Default)]
@@ -11,18 +12,35 @@ pub struct GlobalArgs {
1112
#[arg(long, env = "YOZEFU_CONFIG_DIR", global = true)]
1213
/// Use a specific config directory to store the configuration, logs, search filters.
1314
pub config_dir: Option<PathBuf>,
15+
#[arg(long, env = "YOZEFU_LOG_FILE", global = true)]
16+
/// Append logs to a specific log file
17+
pub log_file: Option<PathBuf>,
1418
}
1519

1620
impl GlobalArgs {
1721
pub fn workspace(&self) -> Workspace {
1822
let default_workspace = Workspace::default();
23+
1924
let workspace = match (&self.config_dir, &self.config_file) {
2025
(Some(dir), Some(file)) => Workspace::new(dir, file),
2126
(Some(dir), None) => Workspace::new(dir, &dir.join(Workspace::CONFIG_FILENAME)),
2227
(None, Some(file)) => Workspace::new(&default_workspace.path, file),
2328
(None, None) => default_workspace,
2429
};
2530

31+
32+
33+
let lo = match self.log_file {
34+
Some(log_file) => log_file,
35+
None => {
36+
let contents = fs::read_to_string(workspace.config_file()).unwrap_or("{}".to_string());
37+
let config: GlobalConfig = serde_json::from_str(&contents)
38+
.unwrap_or_default();
39+
config.log_file.unwrap_or_else(|| workspace.logs_file())
40+
}
41+
}
42+
43+
2644
debug!("Using config directory: {}", workspace.path.display());
2745
workspace
2846
}
@@ -42,6 +60,7 @@ mod tests {
4260
let args = GlobalArgs {
4361
config_dir: Some(PathBuf::from("/tmp/config_dir")),
4462
config_file: Some(PathBuf::from("/tmp/config_dir/config.json")),
63+
log_file: None,
4564
};
4665
let ws = args.workspace();
4766
assert_eq!(ws.path, PathBuf::from("/tmp/config_dir"));
@@ -56,6 +75,7 @@ mod tests {
5675
let args = GlobalArgs {
5776
config_dir: Some(PathBuf::from("/tmp/config_dir")),
5877
config_file: None,
78+
log_file: None,
5979
};
6080
let ws = args.workspace();
6181
assert_eq!(ws.path, PathBuf::from("/tmp/config_dir"));
@@ -80,6 +100,7 @@ mod tests {
80100
let args = GlobalArgs {
81101
config_dir: None,
82102
config_file: Some(PathBuf::from("/tmp/config_dir/config.json")),
103+
log_file: None,
83104
};
84105
let ws = args.workspace();
85106
assert_eq!(ws.path, default_ws.path);
@@ -95,6 +116,7 @@ mod tests {
95116
let args = GlobalArgs {
96117
config_dir: None,
97118
config_file: None,
119+
log_file: None,
98120
};
99121
let ws = args.workspace();
100122
assert_eq!(ws.path, default_ws.path);
@@ -106,6 +128,7 @@ mod tests {
106128
let args = GlobalArgs {
107129
config_dir: Some(PathBuf::from("/tmp/config_dir")),
108130
config_file: Some(PathBuf::from("/tmp/config_dir/config.json")),
131+
log_file: None,
109132
};
110133
assert_eq!(
111134
args.workspace().config_file(),

0 commit comments

Comments
 (0)