Skip to main content

org_gdocs/
doctor.rs

1//! Health-check provider for `org-gdocs`.
2//!
3//! Reports on the pieces auth and sync depend on: resolvable config/data
4//! directories, a readable configuration, the Google OAuth client secret, and a
5//! cached token. Token *expiry* is not checked here — `yup-oauth2` refreshes
6//! silently — so a present token is the meaningful signal.
7
8use tftio_cli_common::{DoctorCheck, DoctorChecks, RepoInfo};
9
10use crate::config::{Config, EnvInputs, Paths};
11
12/// Doctor checks provider for the `org-gdocs` tool.
13pub struct OrgGdocsDoctor;
14
15impl DoctorChecks for OrgGdocsDoctor {
16    fn repo_info() -> RepoInfo {
17        RepoInfo::new("tftio-stuff", "tools")
18    }
19
20    fn current_version() -> &'static str {
21        env!("CARGO_PKG_VERSION")
22    }
23
24    fn tool_checks(&self) -> Vec<DoctorCheck> {
25        let paths = match Paths::resolve(&EnvInputs::from_env()) {
26            Ok(paths) => paths,
27            Err(err) => {
28                return vec![DoctorCheck::fail("Config paths", err.to_string())];
29            }
30        };
31
32        let config = match Config::load(&paths) {
33            Ok(config) => config,
34            Err(err) => {
35                return vec![DoctorCheck::fail(
36                    "Configuration",
37                    format!("could not load {}: {err}", paths.config_file().display()),
38                )];
39            }
40        };
41
42        vec![
43            DoctorCheck::pass(format!("Config directory: {}", paths.config_dir.display())),
44            credential_check(&config),
45            token_check(&config),
46        ]
47    }
48}
49
50/// Report whether the Google OAuth client secret is present.
51fn credential_check(config: &Config) -> DoctorCheck {
52    if config.credentials_path.exists() {
53        DoctorCheck::pass(format!(
54            "Google OAuth client secret: {}",
55            config.credentials_path.display()
56        ))
57    } else {
58        DoctorCheck::fail(
59            "Google OAuth client secret",
60            format!(
61                "not found at {} (download a Desktop OAuth client from Google Cloud Console; see README)",
62                config.credentials_path.display()
63            ),
64        )
65    }
66}
67
68/// Report whether an OAuth token has been cached by a prior `auth` run.
69fn token_check(config: &Config) -> DoctorCheck {
70    if config.token_path.exists() {
71        DoctorCheck::pass(format!(
72            "OAuth token cached: {}",
73            config.token_path.display()
74        ))
75    } else {
76        DoctorCheck::fail(
77            "OAuth token",
78            format!(
79                "no cached token at {} (run `org-gdocs auth`)",
80                config.token_path.display()
81            ),
82        )
83    }
84}