Skip to content

Commit

Permalink
Fix broken parent_name by making output-path internally absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed Jun 5, 2024
1 parent cee6ce3 commit e504ed9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ cases, use the guaranteed `text` field instead.*
Assume a programming language ID from the specified filename. If `filename` contains an extension, that is checked - otherwise `filename` itself is
checked.

For a comprehensive list of possible language IDs, see the [`language_specifics.rs`](src/templater/language_specifics.rs) source file.
For a comprehensive list of possible language IDs, see the [`language_specifics.rs`](src/templater/functions/language_specifics.rs) source file.

One example use case could be with the `file_name` [builtin](#built-in-variables) - this would give you the assumed file format of the output file,
when using the `--path` option.
Expand Down
43 changes: 37 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use cli::{Cli, CommandVariant, OutputArgGroup};
use colored::Colorize;
use error::{DevinitError, DevinitResult};
use files::{ConfigYaml, ConfigYamlBuilder};
use path_clean::PathClean;
use std::{
collections::HashMap,
env,
ffi::OsStr,
fs,
io::{ErrorKind, Read, Write},
Expand Down Expand Up @@ -92,6 +94,33 @@ fn main() {
return Ok(());
}

// make output path absolute if it was specified
let output_path_absolute = if let Some(path) = &output_conf.path {
let path = PathBuf::from(&path);
Some(
if path.is_absolute() {
path
} else {
env::current_dir()
.map_err(|_| {
DevinitError::FileReadWriteError("Cwd not accessible".to_string())
})?
.join(path)
}
.clean()
.display()
.to_string(),
)
} else {
None
};

let output_conf = OutputArgGroup {
path: output_path_absolute, // use absolute path
dry_run: output_conf.dry_run,
list_vars: output_conf.list_vars,
};

render(&mut renderer, &cli_var_defs, &output_conf, assert_empty)?;

Ok(())
Expand Down Expand Up @@ -365,19 +394,21 @@ fn get_file_builtin_info<P: AsRef<Path>>(path: P) -> DevinitResult<(String, Stri
path.as_ref()
.file_name()
.and_then(OsStr::to_str)
.ok_or(DevinitError::FileReadWriteError(
"File path is not valid UTF-8".to_string(),
))?
.ok_or(DevinitError::FileReadWriteError(format!(
"Output file path {:?} is not valid UTF-8",
path.as_ref()
)))?
.to_owned(),
// path parent directory name
path.as_ref()
.parent()
.unwrap_or(PathBuf::from("").as_path())
.file_name()
.and_then(OsStr::to_str)
.ok_or(DevinitError::FileReadWriteError(
"File path is not valid UTF-8".to_string(),
))?
.ok_or(DevinitError::FileReadWriteError(format!(
"Path of parent directory of output file {:?} is not valid UTF-8",
path.as_ref()
)))?
.to_string(),
// file contents
match fs::read_to_string(&path) {
Expand Down

0 comments on commit e504ed9

Please sign in to comment.