diff --git a/src/commands/env.rs b/src/commands/env.rs index 1cd73e9..b9d8ea0 100644 --- a/src/commands/env.rs +++ b/src/commands/env.rs @@ -1,6 +1,6 @@ use super::Command; use crate::{ - env::{self, get_env_vars}, + env::{self, get_env_vars, get_setenv_commands}, shell::{self, ShellEnv}, success, warning, }; @@ -47,7 +47,7 @@ impl Command for Env { return apply_profile(&shell); } - let vars = get_env_vars(&shell)?; + let vars = get_setenv_commands(&shell)?; println!("{}", vars); Ok(()) diff --git a/src/commands/exec.rs b/src/commands/exec.rs new file mode 100644 index 0000000..7775a47 --- /dev/null +++ b/src/commands/exec.rs @@ -0,0 +1,32 @@ +use std::{ + io::{self, Write}, + process::{self, Stdio}, +}; + +use clap::Args; + +use crate::{env::get_env_vars, shell, Command}; + +/// Display the currently selected version of Go. +#[derive(Args)] +#[command(visible_aliases = ["e", "run"])] +pub struct Exec { + args: Vec, +} + +impl Command for Exec { + fn run(&self) -> anyhow::Result<()> { + let shell = shell::get_shell(); + + let env_vars = get_env_vars(&shell)?; + + let out = process::Command::new("go") + .envs(env_vars) + .args(&self.args) + .output()?; + + io::stdout().write_all(&out.stdout)?; + + Ok(()) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b90d9f5..d4f1a68 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,4 @@ -crate::prelude!(current, env, ls, lsr, r#use, drop, clean, check); +crate::prelude!(current, env, ls, lsr, r#use, drop, clean, check, exec); use anyhow::Result; diff --git a/src/env/shared.rs b/src/env/shared.rs index 5fd993f..a36ff0a 100644 --- a/src/env/shared.rs +++ b/src/env/shared.rs @@ -13,16 +13,22 @@ use whattheshell::Shell; const CURRENT_VERSION_FILE: &str = ".current_version"; /// Returns all required environment variables. -pub fn get_env_vars(shell: &Shell) -> Result { +pub fn get_env_vars(shell: &Shell) -> Result> { let path = std::env::var("PATH")?; - let vars = [ + Ok(vec![ ( "PATH", shell.append_to_path(&path, &shell.path_to_string(get_current_bin_dir()?)?)?, ), ("GOROOT", shell.path_to_string(get_current_install_dir()?)?), - ]; + ]) +} + +pub fn get_setenv_commands(shell: &Shell) -> Result { + let path = std::env::var("PATH")?; + + let vars = get_env_vars(shell)?; let lines: Result, _> = vars .iter() diff --git a/src/main.rs b/src/main.rs index 51fdb2d..fe97ad2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ struct App { command: Commands, } -register_commands!(Check, Clean, Current, Drop, Env, Ls, Lsr, Use); +register_commands!(Check, Clean, Current, Drop, Env, Ls, Lsr, Use, Exec); fn main() { let app = App::parse();