diff options
| author | Teddy Wing | 2021-05-22 22:05:12 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-05-22 22:05:12 +0200 |
| commit | cc90495f29cba19a8d3f4fa201d884ac543d1334 (patch) | |
| tree | 4fe1621035ced85d7c48252b5945fcca057417f7 /src | |
| parent | c65101ade6765afb805822c30994ececfacefc77 (diff) | |
| download | google-calendar-rsvp-cc90495f29cba19a8d3f4fa201d884ac543d1334.tar.bz2 | |
Fix "token.json" not found error
Because I was using `xdg_dirs.find_data_file()` to get the path to
"token.json", an error was triggered when the file didn't exist yet. It
doesn't need to exist because the program is supposed to create it, so
we can't use `find_data_file()`.
Remove the `local_data_file()` function, and instead call the XDG
functions directly where they're needed since in one case we need to
find an existing file (and error with a specific message if it doesn't
exist), and in another, we need a path to a file to create or read.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index ac730fb..a8d3e73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,6 @@ use std::env; use std::fmt; use std::fs; use std::io::{self, Read}; -use std::path::{Path, PathBuf}; use std::process; use std::str; @@ -159,7 +158,7 @@ async fn rsvp(event_id: &str, response: &EventResponseStatus) -> anyhow::Result< secret, oauth2::InstalledFlowReturnMethod::HTTPRedirect, ) - .persist_tokens_to_disk(local_data_file("token.json")?) + .persist_tokens_to_disk(xdg_dirs.get_data_home().join("token.json")) .build() .await .context("authentication failed")?; @@ -205,16 +204,19 @@ async fn rsvp(event_id: &str, response: &EventResponseStatus) -> anyhow::Result< } fn secret_from_file() -> anyhow::Result<oauth2::ApplicationSecret> { + let xdg_dirs = xdg::BaseDirectories::with_prefix("google-calendar-rsvp") + .context("can't get XDG base directory")?; + let f = fs::File::open( - local_data_file("oauth-secret.json") + &xdg_dirs.find_data_file("oauth-secret.json") .context(format!( "Missing OAuth2 secret file. Create an application on the Google Developer Console (https://console.developers.google.com/) and download the JSON secret file to '{}'.", - xdg::BaseDirectories::with_prefix("google-calendar-rsvp")? - .get_data_home() + xdg_dirs.get_data_home() .join("oauth-secret.json") .display() ))?, - )?; + ) + .context("unable to open OAuth secret file")?; let console_secret: oauth2::ConsoleApplicationSecret = serde_json::from_reader(f) @@ -224,15 +226,6 @@ fn secret_from_file() -> anyhow::Result<oauth2::ApplicationSecret> { .ok_or(anyhow::anyhow!("OAuth2 application secret not found")) } -fn local_data_file<P: AsRef<Path>>(file: P) -> anyhow::Result<PathBuf> { - let xdg_dirs = xdg::BaseDirectories::with_prefix("google-calendar-rsvp")?; - - Ok( - xdg_dirs.find_data_file(&file) - .ok_or(anyhow::anyhow!("unable to get XDG data path"))? - ) -} - fn event_id_from_base64(event_id: &str) -> anyhow::Result<String> { let decoded = match base64::decode(event_id) { Ok(d) => d, |
