Make --output respect --directory

I should really add a test for this too ... but goldens are not really
suitable for this. Let's leave it untested for now then?
This commit is contained in:
Ruud van Asseldonk 2024-02-19 21:42:42 +01:00
parent b76e8d7a4b
commit d88cbb2a83
4 changed files with 18 additions and 6 deletions

View file

@ -45,7 +45,10 @@ output format is `json`.
### `-o` `--output <outfile>`
Write the output to the given file instead of stdout.
Write the output to the given file instead of stdout. When [`--directory`][dir]
is set, the output path is relative to that directory.
[dir]: rcl.md#-c-directory-dir
### `--sandbox <mode>`

View file

@ -29,9 +29,12 @@ exactly one input file.
### `-o` `--output <outfile>`
Write the output to the given file instead of stdout. This option is
Write the output to the given file instead of stdout. When [`--directory`][dir]
is set, the output path is relative to that directory. This option is
incompatible with `--in-place`.
[dir]: rcl.md#-c-directory-dir
### `-w` `--width <width>`
Target width in columns. Must be an integer. Defaults to 80. Note that the

View file

@ -50,10 +50,10 @@ pub struct PathLookup {
/// A friendly name displayed to the user.
///
/// This is the path relative to the working directory if possible.
name: String,
pub name: String,
/// The absolute path on the file system to load the data from.
path: PathBuf,
pub path: PathBuf,
}
/// A filesystem resolves import paths to file contents.
@ -328,6 +328,11 @@ impl Loader {
Ok(())
}
/// Resolve a path specified on the CLI so it respects the workdir.
pub fn resolve_cli_path(&self, path: &str) -> Result<PathLookup> {
self.filesystem.resolve_entrypoint(path)
}
/// Borrow all documents.
pub fn as_inputs(&self) -> Vec<Doc> {
self.documents.iter().map(Document::as_doc).collect()

View file

@ -35,11 +35,12 @@ impl App {
/// Write a string to a file.
fn print_to_file(&self, out_path: &str, data: String) -> Result<()> {
std::fs::write(out_path, data).map_err(|err| {
let out_path = self.loader.resolve_cli_path(out_path)?;
std::fs::write(&out_path.path, data).map_err(|err| {
// The concat! macro is not exported, we'll make do with a vec here.
let parts = vec![
"Failed to write to file '".into(),
Doc::highlight(out_path).into_owned(),
Doc::highlight(&out_path.name).into_owned(),
"': ".into(),
err.to_string().into(),
];