-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathtokens.rs
57 lines (53 loc) · 1.68 KB
/
tokens.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{env, fs};
use thiserror::Error;
use anyhow::Result;
use tracing::info;
use crate::pipeline::TokenSource;
#[derive(Error, Debug)]
enum TokenRetrievalError {
#[error("No home directory.")]
HomeDirectoryMissing,
}
/// This reads a token from a specified source. If the token cannot be read, a warning is logged with `tracing`
/// and *no token is used*.
pub(crate) fn get_token(source: &TokenSource) -> Result<String> {
Ok(match source {
TokenSource::Literal(data) => data.clone(),
TokenSource::EnvVar(envvar) => {
let tok = env::var(envvar);
if let Ok(tok) = tok {
tok
} else {
info!("Could not load token at {envvar:?}, using no HF token.");
"".to_string()
}
}
TokenSource::Path(path) => {
let tok = fs::read_to_string(path);
if let Ok(tok) = tok {
tok
} else {
info!("Could not load token at {path:?}, using no HF token.");
"".to_string()
}
}
TokenSource::CacheToken => {
let home = format!(
"{}/.cache/huggingface/token",
dirs::home_dir()
.ok_or(TokenRetrievalError::HomeDirectoryMissing)?
.display()
);
let tok = fs::read_to_string(home.clone());
if let Ok(tok) = tok {
tok
} else {
info!("Could not load token at {home:?}, using no HF token.");
"".to_string()
}
}
TokenSource::None => "".to_string(),
}
.trim()
.to_string())
}