crates/flotilla-core/src/hop_chain/builder.rs
Line | Count | Source |
1 | | use flotilla_protocol::HostName; |
2 | | |
3 | | use super::{Arg, Hop, HopPlan}; |
4 | | use crate::attachable::{AttachableId, AttachableStoreApi}; |
5 | | |
6 | | pub struct HopPlanBuilder<'a> { |
7 | | local_host: &'a HostName, |
8 | | } |
9 | | |
10 | | impl<'a> HopPlanBuilder<'a> { |
11 | 32 | pub fn new(local_host: &'a HostName) -> Self { |
12 | 32 | Self { local_host } |
13 | 32 | } |
14 | | |
15 | | /// Build a plan for attaching to a terminal via its AttachableId. |
16 | | /// Used by TerminalManager::attach_command() and future `flotilla attach`. |
17 | 14 | pub fn build_for_attachable(&self, attachable_id: &AttachableId, store: &dyn AttachableStoreApi) -> Result<HopPlan, String> { |
18 | 14 | let registry = store.registry(); |
19 | | |
20 | 14 | let attachable13 = registry.attachables.get(attachable_id).ok_or_else(|| format!1 ("attachable not found: {attachable_id}"))?1 ; |
21 | | |
22 | 13 | let set = registry.sets.get(&attachable.set_id).ok_or_else(|| format!0 ("attachable set not found: {}", attachable.set_id))?0 ; |
23 | | |
24 | 13 | let mut hops = Vec::new(); |
25 | | |
26 | 13 | if let Some(ref host12 ) = set.host_affinity { |
27 | 12 | if host != self.local_host { |
28 | 7 | hops.push(Hop::RemoteToHost { host: host.clone() }); |
29 | 7 | }5 |
30 | 1 | } |
31 | | |
32 | 13 | if let Some(ref env_id2 ) = set.environment_id { |
33 | 2 | hops.push(Hop::EnterEnvironment { env_id: env_id.clone(), provider: "docker".to_string() }); |
34 | 11 | } |
35 | | |
36 | 13 | hops.push(Hop::AttachTerminal { attachable_id: attachable_id.clone() }); |
37 | | |
38 | 13 | Ok(HopPlan(hops)) |
39 | 14 | } |
40 | | |
41 | | /// Build a plan for wrapping a prepared pane command for a remote workspace. |
42 | | /// Used by CreateWorkspaceFromPreparedTerminal. |
43 | 17 | pub fn build_for_prepared_command(&self, target_host: &HostName, command: &[Arg]) -> HopPlan { |
44 | 17 | let mut hops = Vec::new(); |
45 | 17 | if target_host != self.local_host { |
46 | 7 | hops.push(Hop::RemoteToHost { host: target_host.clone() }); |
47 | 10 | } |
48 | 17 | hops.push(Hop::RunCommand { command: command.to_vec() }); |
49 | 17 | HopPlan(hops) |
50 | 17 | } |
51 | | } |