diff --git a/docs/rcl_evaluate.md b/docs/rcl_evaluate.md index bb7ca20..121cda2 100644 --- a/docs/rcl_evaluate.md +++ b/docs/rcl_evaluate.md @@ -45,7 +45,10 @@ output format is `json`. ### `-o` `--output ` -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 ` diff --git a/docs/rcl_format.md b/docs/rcl_format.md index 3e6b040..aee47f7 100644 --- a/docs/rcl_format.md +++ b/docs/rcl_format.md @@ -29,9 +29,12 @@ exactly one input file. ### `-o` `--output ` -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 ` Target width in columns. Must be an integer. Defaults to 80. Note that the diff --git a/src/loader.rs b/src/loader.rs index 3c047ee..501d1fc 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -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 { + self.filesystem.resolve_entrypoint(path) + } + /// Borrow all documents. pub fn as_inputs(&self) -> Vec { self.documents.iter().map(Document::as_doc).collect() diff --git a/src/main.rs b/src/main.rs index 0917cf9..9952e86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(), ];