assume_rolers/command/
shell.rs

1use async_trait::async_trait;
2use std::env;
3use std::ffi::CString;
4
5use tracing::debug;
6
7use assume_rolers_schema::credentials::ProfileCredentials;
8
9use crate::command::{into_variables, Command, Variable};
10
11pub struct ShellCommand;
12
13#[async_trait]
14impl Command for ShellCommand {
15    async fn run(self, credentials: ProfileCredentials) -> anyhow::Result<()> {
16        set_credentials(credentials);
17        start_shell_session()?;
18        Ok(())
19    }
20}
21
22fn set_credentials(credentials: ProfileCredentials) {
23    let variables = into_variables(&credentials);
24    for Variable { name, value } in variables {
25        if let Some(value) = value {
26            env::set_var(name, value);
27        } else {
28            env::remove_var(name);
29        }
30    }
31}
32
33fn start_shell_session() -> anyhow::Result<()> {
34    let shell = env::var("SHELL")?;
35    debug!("shell: {}, ", &shell);
36
37    let shell = CString::new(shell.bytes().collect::<Vec<_>>())?;
38    let args: Vec<CString> = Vec::new();
39    nix::unistd::execv(&shell, &args)?;
40
41    unreachable!("execv will replace the current process, so never reach this instruction.")
42}