Skip to content

Commit 1cf6d68

Browse files
committed
Centralized path.absolute + added reports_dir to config.json
1 parent 82610b6 commit 1cf6d68

File tree

11 files changed

+342
-250
lines changed

11 files changed

+342
-250
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/target
2-
/reports
3-
.stm_config
2+
/.reports
3+
*stm_config*
44
Cargo.lock
5-
stm-report.json
65
trace*.txt
76
*report*.json
87
!samples/*report*.json

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Example:
180180
#### Debug settings
181181
182182
* `--log error|warn|info|debug|trace`: the log is written to _stdout_. Defaults to `error` for least verbose output. Redirect the output to a file or _null device_ to completely silence it. E.g. `stackmuncher --log debug >> ~/stm_trace.log`
183-
* `--reports "path to reports folder"`: a path to an alternative location for saving stack reports. The path can be relative or absolute. Defaults to a platform-specific user-data location.
183+
* `--reports "path to reports folder"`: a path to an alternative location for saving stack reports. The path can be relative or absolute. Defaults to a platform-specific user-data location. Set once.
184184
* `--config "path to config folder"`: a path to an alternative location of the config folder. The path can be relative or absolute. Defaults to a platform-specific user-data location.
185185
186186
#### Additional info

stackmuncher/src/app_args.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use regex::Regex;
44
use std::env::consts::EXE_SUFFIX;
55
use std::str::FromStr;
66
use std::{path::PathBuf, process::exit};
7+
use tracing::debug;
78

89
pub(crate) const GIST_ID_REGEX: &str = "[a-f0-9]{32}";
910

@@ -169,8 +170,9 @@ impl AppArgs {
169170
exit(1);
170171
}
171172

173+
// try to covert to path and expand ~/somepath on Linux to /home/user/...
172174
match PathBuf::from_str(&project) {
173-
Ok(v) => app_args.project = Some(v),
175+
Ok(v) => app_args.project = Some(tilde_expand(v)),
174176
Err(_) => {
175177
eprintln!(
176178
"STACKMUNCHER CONFIG ERROR: `{}` is not a valid path for `--project`. Omit that param to use the current folder or provide a valid path to where the project is located (absolute or relative).",
@@ -193,8 +195,10 @@ impl AppArgs {
193195
exit(1);
194196
}
195197

196-
match PathBuf::from_str(&reports) {
197-
Ok(v) => app_args.reports = Some(v),
198+
// try to convert to path
199+
let reports_dir = match PathBuf::from_str(&reports) {
200+
// expand ~/somepath on Linux to /home/user/...
201+
Ok(v) => tilde_expand(v),
198202
Err(_) => {
199203
eprintln!(
200204
"STACKMUNCHER CONFIG ERROR: `{}` is not a valid path for `--reports`. Omit it to use the default location or provide a valid path to where report files should be placed (absolute or relative).",
@@ -203,7 +207,10 @@ impl AppArgs {
203207
help::emit_report_dir_msg();
204208
exit(1);
205209
}
206-
}
210+
};
211+
212+
// this should be an OK value that will be validated further downstream
213+
app_args.reports = Some(reports_dir);
207214
};
208215

209216
// config folder
@@ -218,7 +225,7 @@ impl AppArgs {
218225
}
219226

220227
match PathBuf::from_str(&config_folder) {
221-
Ok(v) => app_args.config = Some(v),
228+
Ok(v) => app_args.config = Some(tilde_expand(v)),
222229
Err(_) => {
223230
eprintln!(
224231
"STACKMUNCHER CONFIG ERROR: `{}` is not a valid path for `--config`. Omit it to use the default location or provide a valid path to where your encryption keys and config details should be stored (absolute or relative)",
@@ -292,3 +299,53 @@ fn string_to_log_level(s: String) -> tracing::Level {
292299
}
293300
}
294301
}
302+
303+
/// Replaces `~` in Linux paths with the full path to the home directory.
304+
/// E.g. `~/rust/stm_app` -> `/home/ubuntu/rust/stm_app`
305+
fn tilde_expand(path: PathBuf) -> PathBuf {
306+
// check if there is a ~ at all
307+
if !path.starts_with("~") {
308+
return path;
309+
}
310+
311+
// is there a home directory?
312+
let home_dir = match std::env::var("HOME") {
313+
Ok(v) => v,
314+
Err(e) => {
315+
eprintln!("STACKMUNCHER CONFIG ERROR: Cannot get the name of HOME directory due to {}", e);
316+
eprintln!();
317+
eprintln!(" Try replacing ~ with an absolute path.");
318+
eprintln!();
319+
help::emit_usage_msg();
320+
exit(1);
321+
}
322+
};
323+
324+
debug!("Home dir: {}", home_dir);
325+
326+
let home_dir = match PathBuf::from_str(&home_dir) {
327+
Err(_) => {
328+
eprintln!("STACKMUNCHER CONFIG ERROR: $HOME has invalid home directory path: {}", home_dir);
329+
eprintln!();
330+
eprintln!(" Try replacing ~ with an absolute path.");
331+
eprintln!();
332+
help::emit_usage_msg();
333+
exit(1);
334+
}
335+
Ok(v) => {
336+
if path.starts_with("~/") {
337+
v.join(&path.to_string_lossy()[2..])
338+
} else {
339+
eprintln!("STACKMUNCHER CONFIG ERROR: cannot expand ~ shortcut");
340+
eprintln!();
341+
eprintln!(" Try replacing ~ with an absolute path.");
342+
eprintln!();
343+
help::emit_usage_msg();
344+
exit(1);
345+
}
346+
}
347+
};
348+
349+
debug!("Expanded {} -> {}", path.to_string_lossy(), home_dir.to_string_lossy());
350+
home_dir
351+
}

stackmuncher/src/cmd_config.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::help;
33
use crate::signing::ReportSignature;
44
use hyper::{Client, Request};
55
use hyper_rustls::HttpsConnector;
6-
use path_absolutize::{self, Absolutize};
76
use ring::signature::{self, Ed25519KeyPair, KeyPair};
87
use serde::Deserialize;
98
use serde_json::Value;
@@ -12,6 +11,11 @@ use tracing::{debug, error, info, warn};
1211
/// A "well-known" string used as the content to be signed for GH verification. The signature is uploaded to a Gist.
1312
const GH_VERIFICATION_STRING_TO_SIGN: &str = "stackmuncher";
1413

14+
#[cfg(not(target_os = "windows"))]
15+
const PATH_SEPARATOR: &str = "/";
16+
#[cfg(target_os = "windows")]
17+
const PATH_SEPARATOR: &str = "\\";
18+
1519
/// A stripped-down representation of GH GetGist API response: Owner details.
1620
#[derive(Deserialize)]
1721
pub(crate) struct GistOwner {
@@ -55,18 +59,15 @@ pub(crate) async fn view_config(config: AppConfig) {
5559
// prepare values needed in println!() macros to prevent line wrapping in the code
5660
let pub_key = ReportSignature::get_public_key(&config.user_key_pair);
5761
let reports = config
58-
.lib_config
59-
.report_dir
62+
.reports_dir
6063
.as_ref()
61-
.expect("config.report_dir is not set. It's a bug.")
62-
.absolutize()
63-
.expect("Cannot convert config.report_dir to absolute path. It's a bug.")
64+
.expect("config.reports_dir is not set. It's a bug.")
6465
.to_string_lossy()
6566
.to_string();
6667
let config_file = config
6768
.config_file_path
68-
.absolutize()
69-
.expect("Cannot convert config.config_file_path to absolute path. It's a bug.")
69+
.parent()
70+
.expect("config_file_path has no parent. It's a bug.")
7071
.to_string_lossy()
7172
.to_string();
7273
let exe_file = match std::env::current_exe() {
@@ -95,8 +96,8 @@ pub(crate) async fn view_config(config: AppConfig) {
9596
println!(" Public profile: {}", public_profile);
9697
println!(" GitHub validation: {}", github_validation);
9798
println!();
98-
println!(" Stack reports: {}", reports);
99-
println!(" Config folder: {}", config_file);
99+
println!(" Stack reports: {}{}", reports, PATH_SEPARATOR);
100+
println!(" Config folder: {}{}", config_file, PATH_SEPARATOR);
100101
println!(" Executable: {}", exe_file);
101102
println!();
102103
}

stackmuncher/src/cmd_munch.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::help;
33
use crate::signing::ReportSignature;
44
use crate::submission::submit_report;
55
use futures::stream::{FuturesUnordered, StreamExt};
6-
use path_absolutize::{self, Absolutize};
76
use stackmuncher_lib::{code_rules::CodeRules, config::Config, git, report::Report, utils::hash_str_sha1};
87
use std::path::Path;
98
use tracing::{debug, info, warn};
@@ -18,7 +17,7 @@ pub(crate) async fn run(config: AppConfig) -> Result<(), ()> {
1817
let report_dir = Path::new(
1918
config
2019
.lib_config
21-
.report_dir
20+
.project_report_dir
2221
.as_ref()
2322
.expect("Cannot unwrap config.report_dir. It's a bug."),
2423
);
@@ -43,10 +42,7 @@ pub(crate) async fn run(config: AppConfig) -> Result<(), ()> {
4342
None => {
4443
// there were no changes since the previous report - it can be reused as-is
4544
info!("Done in {}ms", instant.elapsed().as_millis());
46-
// do not print the end user msg if the logging is enabled
47-
if config.lib_config.log_level == tracing::Level::ERROR {
48-
println!("No new commits since the last run.");
49-
}
45+
println!(" No new commits since the last run.");
5046
cached_project_report.expect("Cannot unwrap cached report. It's a bug.")
5147
}
5248
Some(v) => {
@@ -56,17 +52,6 @@ pub(crate) async fn run(config: AppConfig) -> Result<(), ()> {
5652
}
5753
};
5854

59-
// print the location of the reports
60-
if config.lib_config.log_level == tracing::Level::ERROR {
61-
println!(
62-
" Stack report: {}",
63-
report_dir
64-
.absolutize()
65-
.expect("Cannot convert report_dir to absolute path. It's a bug.")
66-
.to_string_lossy()
67-
);
68-
}
69-
7055
info!("Contributor reports requested for: {:?}", config.lib_config.git_identities);
7156

7257
// check if there are multiple contributors and generate individual reports
@@ -189,10 +174,7 @@ pub(crate) async fn run(config: AppConfig) -> Result<(), ()> {
189174
// check if the submission to the directory should go ahead
190175
if config.dryrun {
191176
// a dry-run was requested by the user
192-
if config.lib_config.log_level == tracing::Level::ERROR {
193-
println!("Directory Profile update skipped: `--dryrun` flag.");
194-
}
195-
warn!("Skipping report submission: `--dryrun` flag.")
177+
println!(" Directory Profile update skipped: `--dryrun` flag.");
196178
} else {
197179
if first_run {
198180
info!("No report submission on the first run");
@@ -215,6 +197,8 @@ pub(crate) async fn run(config: AppConfig) -> Result<(), ()> {
215197
}
216198
}
217199

200+
// print the location of the reports
201+
println!(" Stack reports: {}", report_dir.to_string_lossy());
218202
info!("Repo processed in {}ms", instant.elapsed().as_millis());
219203

220204
Ok(())

0 commit comments

Comments
 (0)