Skip to main content

gitv_tui/auth/
env.rs

1use crate::{auth::AuthProvider, errors::AppError};
2
3pub struct EnvAuth;
4impl EnvAuth {
5    const ENV_VAR: &'static str = "GH_TOKEN";
6}
7impl AuthProvider for EnvAuth {
8    fn get_token(&self) -> Result<String, AppError> {
9        std::env::var(Self::ENV_VAR).map_err(|_| {
10            AppError::Other(anyhow::anyhow!(
11                "{} environment variable not set",
12                Self::ENV_VAR
13            ))
14        })
15    }
16
17    fn set_token(&self, token: &str) -> Result<(), AppError> {
18        // Safety: This is safe because the env variable is only read once, so there is
19        // threat of a race condition.
20        unsafe {
21            std::env::set_var(Self::ENV_VAR, token);
22        }
23        Ok(())
24    }
25}