1use crate::error::LinearError;
2use std::path::PathBuf;
3
4pub fn token_from_file() -> Result<String, LinearError> {
7 let path = token_file_path()?;
8 std::fs::read_to_string(&path)
9 .map(|s| s.trim().to_string())
10 .map_err(|e| {
11 LinearError::AuthConfig(format!(
12 "Could not read token file {}: {}",
13 path.display(),
14 e
15 ))
16 })
17}
18
19pub fn token_from_env() -> Result<String, LinearError> {
21 std::env::var("LINEAR_API_TOKEN").map_err(|_| {
22 LinearError::AuthConfig("LINEAR_API_TOKEN environment variable not set".to_string())
23 })
24}
25
26pub fn auto_token() -> Result<String, LinearError> {
29 token_from_env().or_else(|_| token_from_file())
30}
31
32fn token_file_path() -> Result<PathBuf, LinearError> {
33 let home = home::home_dir()
34 .ok_or_else(|| LinearError::AuthConfig("Could not determine home directory".to_string()))?;
35 Ok(home.join(".linear_api_token"))
36}