mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 16:15:20 +02:00
support appending when saving file (#3992)
This patch implements `>>` operation in bash. Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
@ -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(),
|
||||
|
@ -49,5 +49,6 @@ pub trait Shell: std::fmt::Debug {
|
||||
path: &Path,
|
||||
contents: &[u8],
|
||||
name: Span,
|
||||
append: bool,
|
||||
) -> Result<OutputStream, ShellError>;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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",
|
||||
|
Reference in New Issue
Block a user