Skip to content

Instantly share code, notes, and snippets.

@karno
Created March 2, 2020 01:14
Show Gist options
  • Save karno/47a6e961b6197be37e7c36c1471e9cdb to your computer and use it in GitHub Desktop.
Save karno/47a6e961b6197be37e7c36c1471e9cdb to your computer and use it in GitHub Desktop.
pub async fn load_or_init_api_key(token_file: &str) -> Result<ApiKey, ConfigError> {
let token_path = path::Path::new(token_file);
if token_path.exists() {
let token = ApiKey::load(token_path)?;
match ApiKey::load(token_path) {
Ok(token) => Ok(token),
Err(e) => {
eprintln!("failed to read application token file:");
eprintln!("{}", e);
Err(e)
}
}
} else {
// create
println!("file not found -> create token file.");
let token = create_api_key().await?;
token.save(token_file)?;
Ok(token)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment