use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use serde_json::{Value, json};
use super::copy::holds_store;
use super::journal::{Left, Unit};
use super::report::{self, Offered};
use super::snapshot::{self, Snapshot};
use super::units::{NO_TURNS, UNFIT, inventory};
use crate::login::Login;
use crate::login::host::{Discovery, Result};
use crate::login::local::{self, State};
use crate::login::output::{Mark, View, fill, host as shown, lost, say};
pub(crate) struct Offer {
pub home: PathBuf,
pub host: String,
pub endpoint: String,
pub account: Option<String>,
pub snapshot: Snapshot,
pub units: Vec<Unit>,
pub local: Vec<Left>,
pub armed: bool,
pub held: Option<std::fs::File>,
pub credential: Option<String>,
}
impl Offer {
pub(crate) fn intent(&self) -> Value {
let offered = Offered::of(&self.units);
json!({"scopes": offered.scopes, "conversations": offered.conversations, "bytes": offered.bytes})
}
}
impl Drop for Offer {
fn drop(&mut self) {
if !self.armed {
let _ = snapshot::dispose(&self.home);
}
}
}
fn told(ui: &mut View<impl Write>, units: &[Unit], local: &[Left]) -> io::Result<()> {
ui.say(&report::filled("memory_offer", &Offered::of(units).words()))?;
for reason in [NO_TURNS, UNFIT] {
let n = local.iter().filter(|l| l.reason == reason).count();
if n > 0 {
let words = [("count", report::count(n, "conversation"))];
ui.say(&report::filled(&format!("memory_left_{reason}"), &words))?;
}
}
ui.say(&say("memory_secrets"))?;
ui.say(&say("memory_separate"))
}
pub(crate) fn offer(
args: &Login,
prior: Option<&State>,
discovery: &Discovery,
url: &str,
home: &Path,
ui: &mut View<impl Write>,
) -> Result<Option<Offer>> {
if args.refresh || args.non_interactive || local::environment().is_some() {
return Ok(None);
}
let Some(endpoint) = discovery.import_endpoint.clone() else {
if args.import {
let host = shown(&discovery.name);
return Err(fill("import_unoffered", &[("host", &host)]));
}
return Ok(None);
};
let ask = !args.import && !args.json && io::stdin().is_terminal();
if !(args.import || ask) || !holds_store(home) {
if args.import {
ui.step(&say("step_memory")).map_err(lost)?;
ui.mark(Mark::Ok, &say("memory_empty")).map_err(lost)?;
}
return Ok(None);
}
ui.step(&say("step_memory"))
.and_then(|()| ui.say(&say("memory_measuring")))
.and_then(|()| ui.flush())
.map_err(lost)?;
if super::journal::read(home)?.is_some() {
return Err(fill("import_waiting", &[("host", &shown(url))]));
}
snapshot::dispose(home).map_err(|f| f.message())?;
let taken = snapshot::take(home).map_err(|f| f.message())?;
let mut offer = Offer {
home: home.to_path_buf(),
host: url.to_string(),
endpoint,
account: prior.map(|s| s.account.id.clone()),
snapshot: taken,
units: Vec::new(),
local: Vec::new(),
armed: false,
held: None,
credential: None,
};
let made = inventory(&offer.snapshot, &mut |_, _, _| Ok(())).map_err(|e| e.text())?;
offer.units = made
.units
.iter()
.map(|u| Unit {
kind: u.kind.into(),
name: u.name.clone(),
sha256: u.sha256.clone(),
bytes: u.bytes,
})
.collect();
offer.local = made
.omitted
.iter()
.map(|o| Left {
kind: o.kind.into(),
name: o.name.clone(),
reason: o.reason.into(),
})
.collect();
if offer.units.is_empty() {
report::named(ui, &offer.local)
.and_then(|()| ui.mark(Mark::Ok, &say("memory_empty")))
.map_err(lost)?;
return Ok(None);
}
told(ui, &offer.units, &offer.local).map_err(lost)?;
if !ask {
ui.mark(Mark::Ok, &say("memory_chosen")).map_err(lost)?;
return Ok(Some(offer));
}
let question = fill("memory_question", &[("host", &shown(&discovery.name))]);
ui.prompt(&question).map_err(lost)?;
let mut answer = String::new();
io::stdin()
.read_line(&mut answer)
.map_err(|_| "cannot read the import answer")?;
if matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes") {
return Ok(Some(offer));
}
ui.mark(Mark::Ok, &say("memory_declined")).map_err(lost)?;
Ok(None)
}