use crate::serve::verbs_client::identity::{self, Choice, Group};
use crate::snapshot::SandboxRow;
use crate::tui::data::Snapshot;
use crate::tui::prompt::PromptAction;
use crate::tui::remote::{Route, find_instance, instance_sandbox, machine_root, sandbox_key};
use super::view::{ConfirmModal, Confirmed};
use super::{App, Modal, VerbEnd};
#[derive(Clone, Debug, PartialEq)]
pub enum IdentityReq {
Offer { routes: Vec<Route>, asked: bool },
Answer { choice: Choice, on: IdentityOn, then: Option<PromptAction> },
}
#[derive(Clone, Debug, PartialEq)]
pub enum IdentityOn {
Groups(Vec<Group>),
Notes { route: Route, ids: Vec<String> },
}
fn target<'a>(s: &'a Snapshot, action: &PromptAction) -> Option<&'a SandboxRow> {
match action {
PromptAction::Run { sandbox, .. } => s.sandboxes.iter().find(|r| sandbox_key(r) == *sandbox),
PromptAction::Rebuild { instance, .. } | PromptAction::Start { instance } => {
let inst = find_instance(s, instance)?;
instance_sandbox(s, inst)
}
_ => None,
}
}
fn row_route(s: &Snapshot, row: &SandboxRow) -> Route {
match &row.machine {
None => Route::default(),
Some(m) => Route { machine: Some(m.clone()), root: row.root.clone().or_else(|| machine_root(s, m)) },
}
}
pub(super) fn offer_routes(s: &Snapshot) -> Vec<Route> {
let mut routes = vec![Route::default()];
for row in &s.sandboxes {
if row.machine.is_some() && row.identity.iter().any(|n| !n.acked) {
let route = row_route(s, row);
if !routes.contains(&route) {
routes.push(route);
}
}
}
routes
}
impl App {
pub(super) fn identity_gate(&mut self, action: PromptAction) -> Option<PromptAction> {
let Some(s) = &self.snapshot else { return Some(action) };
let Some(row) = target(s, &action) else { return Some(action) };
let notes: Vec<_> = row.identity.iter().filter(|n| !n.acked).collect();
if notes.is_empty() {
return Some(action);
}
let ids = notes.iter().map(|n| n.id.clone()).collect();
let body = notes.iter().map(|n| format!(" {}", n.text)).collect();
let question = format!("sandbox {} assumes root in the container, which breaks under identity = \"host\":", sandbox_key(row));
let route = row_route(s, row);
self.modal = Modal::Confirm(ConfirmModal::new(question, body, Confirmed::Identity { route, ids, action }));
None
}
pub(super) fn request_identity_offer(&mut self) {
let routes = self.snapshot.as_ref().map(offer_routes).unwrap_or_else(|| vec![Route::default()]);
self.status = Some("reading the config for /root…".into());
self.pending_identity = Some(IdentityReq::Offer { routes, asked: true });
}
pub(super) fn offer_identity_once(&mut self) {
if std::mem::replace(&mut self.identity_offered, true) {
return;
}
if let Some(s) = &self.snapshot {
self.pending_identity = Some(IdentityReq::Offer { routes: offer_routes(s), asked: false });
}
}
pub fn identity_offer(&mut self, groups: Vec<Group>, asked: bool) {
if groups.is_empty() {
if asked {
self.status = Some("no config uses /root".into());
}
return;
}
if !asked && !matches!(self.modal, Modal::None) {
return;
}
if asked {
self.status = None;
}
let question = "update these to work under identity = \"host\"?".to_string();
let body = identity::listing(&groups, crate::ask::Style::plain());
self.modal = Modal::Confirm(ConfirmModal::new(question, body, Confirmed::IdentityOffer(groups)));
}
pub(super) fn identity_chosen(&mut self, choice: Choice, on: IdentityOn, then: Option<PromptAction>) {
match choice {
Choice::Skip => self.cleared_action = then,
Choice::Update | Choice::Keep => {
self.status = Some(match choice {
Choice::Update => "updating the config…".into(),
_ => "recording that…".into(),
});
self.pending_identity = Some(IdentityReq::Answer { choice, on, then });
}
}
}
pub fn identity_answered(&mut self, outcome: Result<String, String>, then: Option<PromptAction>) {
match outcome {
Ok(line) => {
if !line.is_empty() {
self.status = Some(line.clone());
}
if then.is_some() {
self.identity_note = (!line.is_empty()).then_some(line);
self.cleared_action = then;
}
}
Err(why) => match then {
Some(action) => self.verb_answered(&action, VerbEnd::Done { status: why, failed: true }),
None => self.status = Some(why),
},
}
}
pub fn take_pending_identity(&mut self) -> Option<IdentityReq> {
self.pending_identity.take()
}
pub fn take_identity_note(&mut self) -> Option<String> {
self.identity_note.take()
}
}
#[cfg(test)]
mod tests {
use crate::snapshot::IdentityNote;
use crate::tui::app::test_support::*;
use crate::tui::app::*;
use crate::tui::remote::Route;
use crate::serve::verbs_client::identity::{Choice, Group};
use super::{IdentityOn, IdentityReq};
fn note(id: &str, acked: bool) -> IdentityNote {
IdentityNote { id: id.into(), text: format!("sandbox.s: mounts entry {id}"), acked }
}
fn app_with(notes: Vec<IdentityNote>) -> App {
let mut app = new_app();
let mut s = snapshot_with_status(2, crate::tui::data::ContainerStatus::Exited("Exited (0)".into()));
s.sandboxes[0].identity = notes;
app.set_snapshot(s);
app.take_pending_identity();
app
}
fn start() -> PromptAction {
PromptAction::Start { instance: "inst0".into() }
}
fn identity_modal(app: &App) -> Option<(&str, Vec<String>)> {
match &app.modal {
Modal::Confirm(m) if m.is_identity() => Some((m.question.as_str(), m.body.clone())),
_ => None,
}
}
#[test]
fn the_modal_holds_a_build_verb_only_with_unacked_notes() {
for notes in [vec![], vec![note("a", true)]] {
let mut app = app_with(notes);
app.pending_action = Some(start());
assert_eq!(app.take_pending_action(), Some(start()));
assert!(identity_modal(&app).is_none());
}
let mut app = app_with(vec![note("a", true), note("b", false)]);
app.pending_action = Some(PromptAction::Stop { instance: "inst0".into() });
assert_eq!(app.take_pending_action(), Some(PromptAction::Stop { instance: "inst0".into() }));
for action in [
start(),
PromptAction::Rebuild { instance: "inst1".into(), force: false },
PromptAction::Run { sandbox: "s".into(), name: None, branch: None, base: None, place: false },
] {
app.pending_action = Some(action.clone());
assert_eq!(app.take_pending_action(), None, "{action:?}");
let (question, body) = identity_modal(&app).expect("modal");
assert!(question.starts_with("sandbox s assumes root"), "{question}");
assert_eq!(body, vec![" sandbox.s: mounts entry b".to_string()], "the un-acked notes only");
app.modal = Modal::None;
}
}
#[test]
fn each_key_of_the_modal() {
for (code, choice) in [(KeyCode::Char('u'), Some(Choice::Update)), (KeyCode::Char('c'), Some(Choice::Keep))] {
let mut app = app_with(vec![note("b", false)]);
app.pending_action = Some(start());
assert_eq!(app.take_pending_action(), None);
app.on_key(key(code));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_action(), None, "held until answered");
assert_eq!(
app.take_pending_identity(),
Some(IdentityReq::Answer {
choice: choice.unwrap(),
on: IdentityOn::Notes { route: Route::default(), ids: vec!["b".into()] },
then: Some(start()),
})
);
app.identity_answered(Ok("updated devsandboxes.toml (1 edit)".into()), Some(start()));
assert_eq!(app.take_pending_action(), Some(start()), "not asked again");
assert_eq!(app.take_identity_note().as_deref(), Some("updated devsandboxes.toml (1 edit)"));
}
for code in [KeyCode::Char('s'), KeyCode::Esc] {
let mut app = app_with(vec![note("b", false)]);
app.pending_action = Some(start());
assert_eq!(app.take_pending_action(), None);
app.on_key(key(code));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_identity(), None);
assert_eq!(app.take_pending_action(), Some(start()), "{code:?}");
}
let mut app = app_with(vec![note("b", false)]);
app.pending_action = Some(start());
app.take_pending_action();
app.on_key(key(KeyCode::Char('y')));
assert!(identity_modal(&app).is_some());
}
#[test]
fn a_conflict_sends_nothing_and_clears_the_guard() {
let mut app = app_with(vec![note("b", false)]);
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('s')));
assert!(app.starting.contains("inst0"));
assert_eq!(app.take_pending_action(), None);
app.on_key(key(KeyCode::Char('u')));
let Some(IdentityReq::Answer { then, .. }) = app.take_pending_identity() else { panic!("no answer") };
app.identity_answered(Err("devsandboxes.toml changed while you were asked".into()), then);
assert_eq!(app.take_pending_action(), None);
assert!(!app.starting.contains("inst0"));
assert_eq!(app.status.as_deref(), Some("devsandboxes.toml changed while you were asked"));
}
#[test]
fn the_offer_is_read_on_the_first_snapshot_and_on_u() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
assert_eq!(app.take_pending_identity(), Some(IdentityReq::Offer { routes: vec![Route::default()], asked: false }));
app.set_snapshot(snapshot_with(1));
assert_eq!(app.take_pending_identity(), None, "once");
app.on_key(key(KeyCode::Char('U')));
assert_eq!(app.take_pending_identity(), Some(IdentityReq::Offer { routes: vec![Route::default()], asked: true }));
app.set_tab(Tab::Services);
app.on_key(key(KeyCode::Char('U')));
assert!(app.take_pending_identity().is_some());
app.set_tab(Tab::Ports);
app.on_key(key(KeyCode::Char('U')));
assert_eq!(app.take_pending_identity(), None);
}
#[test]
fn the_offer_reads_machines_roots_with_notes() {
let mut app = machine_app();
app.take_pending_identity();
let mut s = app.snapshot.clone().unwrap();
s.sandboxes[1].identity = vec![note("b", false)];
app.set_snapshot(s);
app.on_key(key(KeyCode::Char('U')));
let box_root = Route { machine: Some("box".into()), root: Some("/r/one".into()) };
assert_eq!(
app.take_pending_identity(),
Some(IdentityReq::Offer { routes: vec![Route::default(), box_root], asked: true })
);
}
#[test]
fn the_offer_lists_and_answers() {
let group = Group::sample("/cfg", &[("a", "sandbox.web: mounts entry \"/root/x\" -> \"${containerEnv:HOME}/x\"")]);
let mut app = app_with(vec![]);
app.identity_offer(Vec::new(), false);
assert_eq!(app.status, None);
app.identity_offer(Vec::new(), true);
assert_eq!(app.status.as_deref(), Some("no config uses /root"));
app.identity_offer(vec![group.clone()], false);
let (_, body) = identity_modal(&app).expect("offer");
assert_eq!(body[0], "/cfg/devsandboxes.toml assumes root in the container, which breaks under identity = \"host\":");
assert!(body[1].contains("${containerEnv:HOME}/x"));
app.on_key(key(KeyCode::Char('c')));
assert_eq!(
app.take_pending_identity(),
Some(IdentityReq::Answer { choice: Choice::Keep, on: IdentityOn::Groups(vec![group.clone()]), then: None })
);
app.identity_offer(vec![group.clone()], true);
app.on_key(key(KeyCode::Char('l')));
assert!(matches!(app.modal, Modal::None));
assert_eq!(app.take_pending_identity(), None);
app.open_help();
app.identity_offer(vec![group], false);
assert!(matches!(app.modal, Modal::Help(_)));
}
}