Skip to content

Commit

Permalink
use exec method for direnv
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2023
1 parent cfa2ca6 commit 37e5a99
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 8 deletions.
1 change: 0 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
shellcheck 0.9.0
shfmt 3.6.0 # test comment
#nodejs 18.13.0
nodejs system
jq latest
18 changes: 18 additions & 0 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 @@ -55,6 +55,7 @@ toml_edit = "0.18.0"
url = "2.3.1"
versions = "4.1.0"
build-time = "0.1.2"
serde_json = "1.0.91"

[target.'cfg(unix)'.dependencies]
exec = "0.3.1"
Expand Down
2 changes: 0 additions & 2 deletions e2e/test_local
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ assert "rtx local -p" "#python 3.11.1 3.10.9 # foo
shellcheck 0.9.0
shfmt 3.6.0 # test comment
#nodejs 18.13.0
nodejs system
jq latest
"

assert "rtx local -p [email protected]" "#python 3.11.1 3.10.9 # foo
shellcheck 0.9.0
shfmt 3.5.0 # test comment
#nodejs 18.13.0
nodejs system
jq latest
"

Expand Down
3 changes: 2 additions & 1 deletion src/cli/direnv/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ impl Command for DirenvActivate {
fn run(self, _config: Config, out: &mut Output) -> Result<()> {
rtxprintln!(
out,
// source_env "$(rtx direnv envrc "$@")"
indoc! {r#"
### Do not edit. This was autogenerated by 'rtx direnv' ###
use_rtx() {{
source_env "$(rtx direnv envrc "$@")"
direnv_load rtx direnv exec
}}
"#}
);
Expand Down
8 changes: 4 additions & 4 deletions src/cli/direnv/envrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::ops::Deref;
use color_eyre::eyre::Result;

use crate::cli::command::Command;
use crate::config::settings::MissingRuntimeBehavior::{AutoInstall, Warn};
use crate::config::settings::MissingRuntimeBehavior::{Prompt, Warn};
use crate::config::Config;
use crate::dirs;
use crate::hash::hash_to_str;
use crate::output::Output;
use crate::{dirs, env};

/// [internal] This is an internal command that writes an envrc file
/// for direnv to consume.
Expand All @@ -19,11 +19,11 @@ pub struct Envrc {}

impl Command for Envrc {
fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
if config.settings.missing_runtime_behavior == AutoInstall {
if config.settings.missing_runtime_behavior == Prompt {
config.settings.missing_runtime_behavior = Warn;
}
config.ensure_installed()?;
let envrc_path = dirs::ROOT
let envrc_path = env::RTX_TMP_DIR
.join("direnv")
.join(hash_to_str(dirs::CURRENT.deref()) + ".envrc");

Expand Down
56 changes: 56 additions & 0 deletions src/cli/direnv/exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use color_eyre::eyre::Result;
use serde_derive::Deserialize;

use crate::cli::command::Command;
use crate::cmd;
use crate::config::settings::MissingRuntimeBehavior::{Prompt, Warn};
use crate::config::Config;
use crate::output::Output;

/// [internal] This is an internal command that writes an envrc file
/// for direnv to consume.
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, hide = true)]
pub struct DirenvExec {}

#[derive(Debug, Default, Deserialize)]
struct DirenvWatches {
#[serde(rename(deserialize = "DIRENV_WATCHES"))]
watches: String,
}

impl Command for DirenvExec {
fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
if config.settings.missing_runtime_behavior == Prompt {
config.settings.missing_runtime_behavior = Warn;
}
config.ensure_installed()?;
let mut cmd = cmd!("direnv", "dump");

for (k, v) in config.env()? {
cmd = cmd.env(k, v);
}
cmd = cmd.env("PATH", config.path_env()?);

let json = cmd!("direnv", "watch", "json", ".tool-versions").read()?;
let w: DirenvWatches = serde_json::from_str(&json)?;
cmd = cmd.env("DIRENV_WATCHES", w.watches);

cmd.run()?;
Ok(())
}
}

#[cfg(test)]
mod test {

use crate::assert_cli;

use super::*;

#[test]
#[ignore]
fn test_direnv_exec() {
let Output { .. } = assert_cli!("direnv", "exec");
}
}
3 changes: 3 additions & 0 deletions src/cli/direnv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::output::Output;

mod activate;
mod envrc;
mod exec;

/// Output direnv function to use rtx inside direnv
///
Expand All @@ -25,6 +26,7 @@ pub struct Direnv {
#[derive(Debug, Subcommand)]
enum Commands {
Envrc(envrc::Envrc),
Exec(exec::DirenvExec),
Activate(activate::DirenvActivate),
}

Expand All @@ -33,6 +35,7 @@ impl Commands {
match self {
Self::Activate(cmd) => cmd.run(config, out),
Self::Envrc(cmd) => cmd.run(config, out),
Self::Exec(cmd) => cmd.run(config, out),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ lazy_static! {
.map(PathBuf::from)
.unwrap_or_else(|| XDG_DATA_HOME.join("rtx"))
};
pub static ref RTX_TMP_DIR: PathBuf = temp_dir().join("rtx");
pub static ref PATH: OsString = var_os("PATH").unwrap_or_default();
pub static ref SHELL: String = var("SHELL").unwrap_or_else(|_| "sh".into());
pub static ref RTX_EXE: PathBuf = current_exe().unwrap_or_else(|_| "rtx".into());
Expand Down

0 comments on commit 37e5a99

Please sign in to comment.