mirror of
https://github.com/nushell/nushell
synced 2025-01-28 04:45:18 +00: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:
parent
4e2d3ceaaf
commit
ae9f4135c0
5 changed files with 21 additions and 4 deletions
|
@ -142,6 +142,7 @@ impl WholeStreamCommand for Save {
|
||||||
"treat values as-is rather than auto-converting based on file extension",
|
"treat values as-is rather than auto-converting based on file extension",
|
||||||
Some('r'),
|
Some('r'),
|
||||||
)
|
)
|
||||||
|
.switch("append", "append values rather than overriding", Some('a'))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
|
@ -165,6 +166,7 @@ fn save(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
|
|
||||||
let path: Option<Tagged<PathBuf>> = args.opt(0)?;
|
let path: Option<Tagged<PathBuf>> = args.opt(0)?;
|
||||||
let save_raw = args.has_flag("raw");
|
let save_raw = args.has_flag("raw");
|
||||||
|
let append = args.has_flag("append");
|
||||||
|
|
||||||
let input: Vec<Value> = args.input.collect();
|
let input: Vec<Value> = args.input.collect();
|
||||||
if path.is_none() {
|
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 {
|
fn string_from(input: &[Value]) -> String {
|
||||||
|
|
|
@ -14,7 +14,8 @@ use nu_protocol::{CommandAction, ConfigPath, TaggedDictBuilder, Value};
|
||||||
use nu_source::{Span, Tag};
|
use nu_source::{Span, Tag};
|
||||||
use nu_stream::{ActionStream, Interruptible, IntoActionStream, OutputStream};
|
use nu_stream::{ActionStream, Interruptible, IntoActionStream, OutputStream};
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io::ErrorKind;
|
use std::fs::OpenOptions;
|
||||||
|
use std::io::{ErrorKind, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -873,8 +874,19 @@ impl Shell for FilesystemShell {
|
||||||
full_path: &Path,
|
full_path: &Path,
|
||||||
save_data: &[u8],
|
save_data: &[u8],
|
||||||
name: Span,
|
name: Span,
|
||||||
|
append: bool,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> 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()),
|
Ok(_) => Ok(OutputStream::empty()),
|
||||||
Err(e) => Err(ShellError::labeled_error(
|
Err(e) => Err(ShellError::labeled_error(
|
||||||
e.to_string(),
|
e.to_string(),
|
||||||
|
|
|
@ -49,5 +49,6 @@ pub trait Shell: std::fmt::Debug {
|
||||||
path: &Path,
|
path: &Path,
|
||||||
contents: &[u8],
|
contents: &[u8],
|
||||||
name: Span,
|
name: Span,
|
||||||
|
append: bool,
|
||||||
) -> Result<OutputStream, ShellError>;
|
) -> Result<OutputStream, ShellError>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,8 +105,9 @@ impl ShellManager {
|
||||||
full_path: &Path,
|
full_path: &Path,
|
||||||
save_data: &[u8],
|
save_data: &[u8],
|
||||||
name: Span,
|
name: Span,
|
||||||
|
append: bool,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> 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) {
|
pub fn next(&self) {
|
||||||
|
|
|
@ -248,6 +248,7 @@ impl Shell for ValueShell {
|
||||||
_path: &Path,
|
_path: &Path,
|
||||||
_contents: &[u8],
|
_contents: &[u8],
|
||||||
_name: Span,
|
_name: Span,
|
||||||
|
_append: bool,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
Err(ShellError::unimplemented(
|
Err(ShellError::unimplemented(
|
||||||
"save on help shell is not supported",
|
"save on help shell is not supported",
|
||||||
|
|
Loading…
Reference in a new issue