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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 4 deletions

View File

@ -142,6 +142,7 @@ impl WholeStreamCommand for Save {
"treat values as-is rather than auto-converting based on file extension",
Some('r'),
)
.switch("append", "append values rather than overriding", Some('a'))
}
fn usage(&self) -> &str {
@ -165,6 +166,7 @@ fn save(args: CommandArgs) -> Result<OutputStream, ShellError> {
let path: Option<Tagged<PathBuf>> = args.opt(0)?;
let save_raw = args.has_flag("raw");
let append = args.has_flag("append");
let input: Vec<Value> = args.input.collect();
if path.is_none() {
@ -231,7 +233,7 @@ fn save(args: CommandArgs) -> Result<OutputStream, ShellError> {
};
};
shell_manager.save(&full_path, &content?, name.span)
shell_manager.save(&full_path, &content?, name.span, append)
}
fn string_from(input: &[Value]) -> String {

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",