use super::network::{NetworkFact, network_fact};
use super::{Check, Facts, Level, fail, ok, warn, words};
use crate::context::Context;
use crate::host::Os;
use crate::host::task::Task;
use crate::schedule::Fault;
use std::path::PathBuf;
pub(crate) struct ScheduleFact {
pub(crate) location: crate::schedule::Location,
pub(crate) task: Option<Task>,
pub(crate) program: Option<PathBuf>,
pub(crate) gone: Option<PathBuf>,
pub(crate) network: NetworkFact,
pub(crate) network_differs: bool,
pub(crate) outdated: bool,
pub(crate) refused: Option<crate::schedule::Refused>,
}
pub(crate) struct WatchServiceFact {
pub(crate) location: crate::schedule::Location,
pub(crate) program: Option<PathBuf>,
pub(crate) gone: Option<PathBuf>,
pub(crate) switching: Option<crate::watch_service::Switching>,
pub(crate) task: Option<Task>,
pub(crate) outdated: bool,
pub(crate) refused: Option<crate::schedule::Refused>,
}
pub(super) fn schedule_fact(ctx: &Context) -> Option<ScheduleFact> {
if !crate::schedule::serves(ctx) {
return None;
}
let crate::schedule::Installed::Yes { location, .. } = crate::schedule::status(ctx) else {
return None;
};
let program = crate::schedule::installed_program(ctx);
let proxies = crate::schedule::installed_proxies(ctx).unwrap_or_default();
Some(ScheduleFact {
gone: gone(ctx, crate::schedule::Job::Renew, program.as_deref()),
program,
location,
task: crate::schedule::task(ctx, crate::schedule::Job::Renew),
network: network_fact(&proxies),
network_differs: !proxies.same_way(&ctx.proxy),
outdated: crate::schedule::outdated(ctx, crate::schedule::Job::Renew),
refused: crate::schedule::refused(ctx, crate::schedule::Job::Renew),
})
}
pub(super) fn watch_service_fact(ctx: &Context) -> Option<WatchServiceFact> {
if !crate::schedule::serves(ctx) {
return None;
}
let crate::watch_service::Installed::Yes {
location,
switching,
} = crate::watch_service::status(ctx)
else {
return None;
};
let program = crate::watch_service::installed_program(ctx);
Some(WatchServiceFact {
gone: gone(ctx, crate::schedule::Job::Watch, program.as_deref()),
program,
location,
switching,
task: crate::watch_service::task(ctx),
outdated: crate::schedule::outdated(ctx, crate::schedule::Job::Watch),
refused: crate::schedule::refused(ctx, crate::schedule::Job::Watch),
})
}
fn gone(
ctx: &Context,
job: crate::schedule::Job,
program: Option<&std::path::Path>,
) -> Option<PathBuf> {
crate::schedule::missing(ctx, job, program?)
}
pub(super) fn judge_schedule(facts: &Facts) -> Option<Check> {
let again = again(facts.os);
let schedule = facts.schedule.as_ref()?;
let Some(program) = &schedule.program else {
return Some(warn(
"schedule",
"renewal schedule",
words::does_not_say_which_pitboard(&schedule.location),
again,
));
};
match crate::schedule::fault(facts.os, program, schedule.gone.is_none()) {
Some(Fault::Gone) => {
let gone = schedule.gone.as_deref().unwrap_or(program);
return Some(fail(
"schedule",
"renewal schedule",
words::runs_what_is_gone(gone.display()),
again,
));
}
Some(Fault::TheApp) => {
return Some(fail(
"schedule",
"renewal schedule",
words::runs_the_app(program.display()),
again,
));
}
None => {}
}
let runs = words::runs_daily(program.display());
if schedule.outdated {
return Some(warn(
"schedule",
"renewal schedule",
words::renews_by_time_awake(&runs),
again,
));
}
if let Some(refused) = &schedule.refused {
return Some(warn(
"schedule",
"renewal schedule",
words::could_not_open(
&runs,
facts.now - refused.at,
refused.vault,
refused.locked(),
super::parks::keyring(facts),
),
words::runs_again(facts.os),
));
}
let Some(task) = &schedule.task else {
return Some(ok("schedule", "renewal schedule", runs));
};
if !task.drifted.is_empty() {
let drifted: Vec<String> = task.drifted.iter().map(words::drift).collect();
return Some(warn(
"schedule",
"renewal schedule",
words::drifted_in_task_scheduler(&runs, &drifted),
again,
));
}
let ago = |at: i64| facts.now - at;
Some(match (task.failed(), task.last_run) {
(Some(result), at) => warn(
"schedule",
"renewal schedule",
words::last_run_ended(&runs, at.map(ago), result),
words::RENEW_TO_SEE_WHY,
),
(None, Some(at)) => ok(
"schedule",
"renewal schedule",
words::last_ran(&runs, ago(at)),
),
(None, None) => ok("schedule", "renewal schedule", words::not_run_yet(&runs)),
})
}
pub(super) fn judge_watch_service(facts: &Facts) -> Option<Check> {
let service = facts.watch_service.as_ref()?;
let again = words::INSTALL_THE_SERVICE_AGAIN;
let Some(program) = &service.program else {
return Some(warn(
"watch_service",
"watch service",
words::does_not_say_which_pitboard(&service.location),
again,
));
};
if let Some(gone) = &service.gone {
return Some(fail(
"watch_service",
"watch service",
words::runs_what_is_gone(gone.display()),
again,
));
}
let runs = words::runs_from_sign_in(service.switching, program.display());
if service.outdated {
return Some(warn(
"watch_service",
"watch service",
words::fails_once_its_pitboard_is_gone(&runs),
again,
));
}
if let Some(refused) = &service.refused {
return Some(warn(
"watch_service",
"watch service",
words::switch_could_not_open(
&runs,
facts.now - refused.at,
refused.vault,
refused.locked(),
super::parks::keyring(facts),
),
words::TRIES_AGAIN_AT_ITS_NEXT_SWITCH,
));
}
Some(match service.task.as_ref().and_then(Task::failed) {
Some(result) => warn(
"watch_service",
"watch service",
words::last_run_ended(&runs, None, result),
words::WATCH_TO_SEE_WHY,
),
None => ok("watch_service", "watch service", runs),
})
}
pub(super) fn judge_schedule_proxy(facts: &Facts) -> Option<Check> {
let schedule = facts.schedule.as_ref()?;
if !schedule.network_differs {
return None;
}
let here = words::this_side(facts.in_the_app);
let installed = match &schedule.network.proxy {
None => words::INSTALLED_WITH_NO_PROXY.to_string(),
Some(proxy) => words::installed_with(&words::proxy_named(proxy)),
};
let this_run = match &facts.network.proxy {
None => words::goes_directly(here),
Some(proxy) if proxy.socks => words::is_given(here, &words::proxy_named(proxy)),
Some(proxy) => words::goes_through(here, &words::proxy_named(proxy)),
};
let detail = match (&schedule.network.proxy, &facts.network.proxy) {
(Some(its), Some(mine)) if words::proxy_named(its) == words::proxy_named(mine) => {
words::another_user_or_password(here, &words::proxy_named(its))
}
_ => words::installed_and_here(&installed, &this_run),
};
if facts.in_the_app {
return Some(Check {
code: "schedule_proxy",
name: "renewal schedule's proxy".into(),
level: Level::Ok,
detail,
advice: words::THE_APPS_OWN_ENVIRONMENT.into(),
account: None,
});
}
let advice = match &facts.network.proxy {
Some(proxy) if proxy.fails => words::socks_would_fail(proxy.variable),
_ => words::INSTALL_AGAIN_FROM_HERE.to_string(),
};
Some(warn(
"schedule_proxy",
"renewal schedule's proxy",
detail,
advice,
))
}
pub(super) fn again(os: Os) -> &'static str {
if has_app(os) {
words::AGAIN_IN_THE_APP_OR_A_TERMINAL
} else {
words::AGAIN_IN_A_TERMINAL
}
}
pub(super) fn has_app(os: Os) -> bool {
match os {
Os::MacOs | Os::Windows => true,
Os::Linux => false,
}
}