Skip to content

Commit

Permalink
All tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
tesujimath committed Aug 22, 2024
1 parent 40c2256 commit 133d805
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/tesujimath/nu_plugin_bash_env"
readme = "README.md"

[dependencies]
itertools = "0.13.0"
nu-plugin = "0.97.1"
nu-protocol = "0.97.1"
once_cell = "1.19.0"
Expand Down
54 changes: 49 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ use once_cell::sync::OnceCell;
use rust_embed::Embed;
use serde::{Deserialize, Serialize};
use shellexpand::tilde;
use std::{env, fs, io::Write, os::unix::fs::PermissionsExt, path::PathBuf};
use std::{
env, fs,
io::Write,
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
};
use subprocess::{Popen, PopenConfig};
use tempfile::TempDir;
use tracing::debug;
Expand Down Expand Up @@ -69,7 +74,8 @@ impl PluginCommand for BashEnv {
let path = match call.positional.first() {
Some(value @ Value::String { val: path, .. }) => {
let path = PathBuf::from(tilde(path).into_owned());
if path.exists() {
let abs_path = Path::new(&cwd).join(&path);
if abs_path.exists() {
Some(path.into_os_string().into_string().unwrap())
} else {
Err(create_error(
Expand All @@ -84,6 +90,29 @@ impl PluginCommand for BashEnv {
call.head,
))?,
};

let export = call
.named
.iter()
.filter(|&(name, _value)| (name.item == "export"))
.map(|(_name, value)| {
if let Some(Value::List { vals, .. }) = value {
vals.iter()
.filter_map(|value| {
if let Value::String { val, .. } = value {
Some(val.clone())
} else {
None
}
})
.collect::<Vec<String>>()
} else {
Vec::default()
}
})
.next()
.unwrap_or_default();

let stdin = match input {
// TODO: pipe the stream into the subprocess rather than via a string
PipelineData::ByteStream(bytes, _metadata) => Some(bytes.into_string()?),
Expand All @@ -94,10 +123,20 @@ impl PluginCommand for BashEnv {
}
};

debug!("run path={:?} stdin={:?}", &path, stdin);
debug!(
"run path={:?} stdin={:?} export={:?} cwd={:?}",
&path, &stdin, &export, &cwd
);

bash_env(span.unwrap_or(Span::unknown()), call.head, stdin, path, cwd)
.map(|value| value.into_pipeline_data())
bash_env(
span.unwrap_or(Span::unknown()),
call.head,
stdin,
path,
export,
cwd,
)
.map(|value| value.into_pipeline_data())
}
}

Expand All @@ -106,6 +145,7 @@ fn bash_env(
creation_site_span: Span,
stdin: Option<String>,
path: Option<String>,
export: Vec<String>,
cwd: String,
) -> Result<Value, LabeledError> {
let script_path = bash_env_script_path();
Expand All @@ -116,6 +156,10 @@ fn bash_env(
if let Some(ref path) = path {
argv.push(path.as_str());
}
let exports =
itertools::Itertools::intersperse(export.into_iter(), ",".to_string()).collect::<String>();
argv.push("--export");
argv.push(exports.as_str());

debug!("popen({:?})", &argv);

Expand Down

0 comments on commit 133d805

Please sign in to comment.