Skip to content

Instantly share code, notes, and snippets.

@karno
Created March 2, 2020 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karno/77603bc7c863a9d7e4a09766a648a465 to your computer and use it in GitHub Desktop.
Save karno/77603bc7c863a9d7e4a09766a648a465 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