Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: lib: cli: Expand all args, and canonize logpath #117

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,25 @@ fn endpoints_parser(entry: &str) -> Result<Arc<dyn drivers::Driver>, String> {
/// Constructs our manager, Should be done inside main
#[instrument(level = "debug")]
pub fn init() {
init_with(Args::parse());
let expanded_args = std::env::args()
.map(|arg| {
// Fallback to the original if it fails to expand
shellexpand::env(&arg.clone())
.inspect_err(|_| {
warn!("Failed expanding arg: {arg:?}, using the non-expanded instead.")
})
.unwrap_or_else(|_| arg.into())
.into_owned()
})
.collect::<Vec<String>>();

let reparsed_expanded_args = Args::parse_from(expanded_args);

init_with(reparsed_expanded_args);
}

/// Constructs our manager, Should be done inside main
/// Note: differently from init(), this doesn't expand env variables
Copy link
Member Author

@joaoantoniocardoso joaoantoniocardoso Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an additional comment here, this would be very hard to do: we don't have a simple and maintainable way to obtain a command-line string from Args and re-parse the string into Args after expanding.

#[instrument(level = "debug")]
pub fn init_with(args: Args) {
MANAGER.get_or_init(|| Manager { clap_matches: args });
Expand All @@ -174,11 +189,18 @@ pub fn log_path() -> String {
let log_path = args()
.log_path
.clone()
.expect("Clap arg \"log-path\" should always be \"Some(_)\" because of the default value.");
.expect("Clap arg \"log-path\" should always be \"Some(_)\" because of the default value.")
.parse::<std::path::PathBuf>()
.expect("Failed parsing the passed log-path");

shellexpand::full(&log_path)
.expect("Failed to expand path")
.to_string()
std::fs::canonicalize(&log_path)
.inspect_err(|_| {
warn!("Failed canonicalizing path: {log_path:?}, using the non-canonized instead.")
})
.unwrap_or(log_path)
.into_os_string()
.into_string()
.expect("Failed converting PathBuf into string")
}

pub fn endpoints() -> Vec<Arc<dyn drivers::Driver>> {
Expand Down