Skip to content

Commit

Permalink
WIP: exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Feb 11, 2024
1 parent 6359bb1 commit 047a088
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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(())
Expand Down
32 changes: 32 additions & 0 deletions src/commands/exec.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

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(())
}
}
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
12 changes: 9 additions & 3 deletions src/env/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
pub fn get_env_vars(shell: &Shell) -> Result<Vec<(&'static str, String)>> {
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<String> {
let path = std::env::var("PATH")?;

let vars = get_env_vars(shell)?;

let lines: Result<Vec<_>, _> = vars
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 047a088

Please sign in to comment.