support appending when saving file (#3992)

This patch implements `>>` operation in bash.

Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
Tw
2021-09-05 02:12:08 +08:00
committed by GitHub
parent 4e2d3ceaaf
commit ae9f4135c0
5 changed files with 21 additions and 4 deletions

View File

@ -14,7 +14,8 @@ use nu_protocol::{CommandAction, ConfigPath, TaggedDictBuilder, Value};
use nu_source::{Span, Tag};
use nu_stream::{ActionStream, Interruptible, IntoActionStream, OutputStream};
use std::collections::VecDeque;
use std::io::ErrorKind;
use std::fs::OpenOptions;
use std::io::{ErrorKind, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
@ -873,8 +874,19 @@ impl Shell for FilesystemShell {
full_path: &Path,
save_data: &[u8],
name: Span,
append: bool,
) -> Result<OutputStream, ShellError> {
match std::fs::write(full_path, save_data) {
let mut options = OpenOptions::new();
if append {
options.append(true)
} else {
options.write(true).create(true).truncate(true)
};
match options
.open(full_path)
.and_then(|ref mut file| file.write_all(save_data))
{
Ok(_) => Ok(OutputStream::empty()),
Err(e) => Err(ShellError::labeled_error(
e.to_string(),

View File

@ -49,5 +49,6 @@ pub trait Shell: std::fmt::Debug {
path: &Path,
contents: &[u8],
name: Span,
append: bool,
) -> Result<OutputStream, ShellError>;
}

View File

@ -105,8 +105,9 @@ impl ShellManager {
full_path: &Path,
save_data: &[u8],
name: Span,
append: bool,
) -> Result<OutputStream, ShellError> {
self.shells.lock()[self.current_shell()].save(full_path, save_data, name)
self.shells.lock()[self.current_shell()].save(full_path, save_data, name, append)
}
pub fn next(&self) {

View File

@ -248,6 +248,7 @@ impl Shell for ValueShell {
_path: &Path,
_contents: &[u8],
_name: Span,
_append: bool,
) -> Result<OutputStream, ShellError> {
Err(ShellError::unimplemented(
"save on help shell is not supported",