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() -> PathBuf {
33 dirs_next().join(".linear_api_token")
34}
35
36fn dirs_next() -> PathBuf {
37 std::env::var("HOME")
38 .map(PathBuf::from)
39 .unwrap_or_else(|_| PathBuf::from("~"))
40}