1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! Getting data *out*: yanking to the clipboard, writing JSON / TSV /
//! Markdown exports, and opening the current selection in the AWS
//! console.
use super::*;
/// Message for a region whose partition has no console host we can name.
///
/// Built from `util::PARTITIONS` rather than restating which partitions
/// have one — the hardcoded prose was copied into three handlers and
/// would start lying the moment an ISO console host is filled in.
fn no_console_host(region: &str) -> String {
let known: Vec<&str> = crate::util::PARTITIONS
.iter()
.filter(|p| p.console_host.is_some())
.map(|p| p.arn)
.collect();
format!(
"no console host known for the {} partition — ebman can build links for: {}",
crate::util::partition_for_region(region).arn,
known.join(", ")
)
}
impl App {
/// The region a row's own data lives in.
///
/// Under a multi-region fan-out this differs from
/// `context.region`, and using the home region builds links and CLI
/// snippets that point at a region where the environment doesn't
/// exist. One accessor because three sites needed this and two of
/// them got it ad-hoc.
pub(crate) fn region_for(&self, env: &crate::aws::Environment) -> String {
env.region
.clone()
.unwrap_or_else(|| self.context.region.clone())
}
}
impl App {
pub(crate) fn yank_cli(&mut self) {
let env_opt = if let Some(d) = self.detail.as_ref() {
Some(d.env_snapshot.clone())
} else {
self.selected_env().cloned()
};
let Some(env) = env_opt else {
self.status_message =
Some("no env selected — press 1-9, click a row, or type ' to jump by name".into());
return;
};
// The row's own region — this snippet is the surface most
// likely to be pasted into a war-room channel as evidence, and
// with the home region it returns an empty array, or worse the
// WRONG environment when a same-named one exists at home.
let region = self.region_for(&env);
let cmd = build_describe_cli(
&env.name,
®ion,
self.override_profile
.as_deref()
.or(self.context.profile.as_deref()),
);
// Recorded so the test can assert on what was copied without
// reaching into the system clipboard.
self.last_yanked_cli = Some(cmd.clone());
match yank(&cmd) {
Ok(()) => {
self.status_message = Some("equivalent AWS CLI command copied".into());
}
Err(e) => self.error_message = Some(format!("clipboard error: {e}")),
}
}
pub(crate) fn export_json(&mut self) {
// Rows are collected and joined rather than emitted with a
// "comma unless last" test against the FILTERED length. Those
// two disagree the moment a row is skipped — and rows can be
// skipped, because a cached view index can outlive a mutation
// of `environments`. Skipping the last one left a trailing
// comma, i.e. invalid JSON handed to whatever consumes the
// clipboard. Joining cannot produce that by construction.
let mut rows: Vec<String> = Vec::new();
for &i in self.view.filtered() {
let Some(e) = self.env_at(i) else { continue };
let cname = if self.view.redact {
redact_block(&e.cname)
} else {
e.cname.clone()
};
let updated = e
.updated
.map(|u| format!("\"{}\"", u.to_rfc3339()))
.unwrap_or_else(|| "null".into());
rows.push(format!(
" {{\"name\":\"{}\",\"application\":\"{}\",\"tier\":\"{}\",\"status\":\"{}\",\"health\":\"{}\",\"platform\":\"{}\",\"version\":\"{}\",\"cname\":\"{}\",\"updated\":{}}}",
json_escape(&e.name),
json_escape(&e.application),
json_escape(&e.tier),
json_escape(&e.status),
json_escape(&e.health),
json_escape(&e.platform),
json_escape(&e.version_label),
json_escape(&cname),
updated,
));
}
let count = rows.len();
let mut out = String::from("[\n");
out.push_str(&rows.join(",\n"));
out.push('\n');
out.push(']');
match yank(&out) {
Ok(()) => {
self.status_message = Some(format!("exported {count} rows (JSON) to clipboard"));
}
Err(e) => self.error_message = Some(format!("clipboard error: {e}")),
}
}
pub(crate) fn export_markdown(&mut self) {
let count = self.view.filtered().len();
let mut out = String::new();
out.push_str("| NAME | APPLICATION | TIER | STATUS | HEALTH | PLATFORM | VERSION | CNAME | UPDATED |\n");
out.push_str("| ---- | ----------- | ---- | ------ | ------ | -------- | ------- | ----- | ------- |\n");
for &i in self.view.filtered() {
let Some(e) = self.env_at(i) else { continue };
let cname = if self.view.redact {
redact_block(&e.cname)
} else {
e.cname.clone()
};
let updated = e.updated.map(|u| u.to_rfc3339()).unwrap_or_default();
out.push_str(&format!(
"| {} | {} | {} | {} | {} | {} | {} | {} | {} |\n",
md_escape(&e.name),
md_escape(&e.application),
e.tier,
e.status,
e.health,
md_escape(&e.platform),
md_escape(&e.version_label),
md_escape(&cname),
updated,
));
}
match yank(&out) {
Ok(()) => {
self.status_message =
Some(format!("exported {count} rows (Markdown) to clipboard"));
}
Err(e) => self.error_message = Some(format!("clipboard error: {e}")),
}
}
pub(crate) fn open_describe_overlay(&mut self) {
let env = if let Some(d) = self.detail.as_ref() {
Some(d.env_snapshot.clone())
} else {
self.selected_env().cloned()
};
let Some(env) = env else {
self.status_message =
Some("no env selected — press 1-9, click a row, or type ' to jump by name".into());
return;
};
self.current_overlay = Some(Overlay::Describe(describe_env(&env)));
}
pub(crate) fn open_in_console(&mut self) {
let env_opt = if let Some(d) = self.detail.as_ref() {
Some(d.env_snapshot.clone())
} else {
self.selected_env().cloned()
};
let Some(env) = env_opt else {
self.status_message =
Some("no env selected — press 1-9, click a row, or type ' to jump by name".into());
return;
};
// The row's OWN region, not the home region. Under a
// multi-region fan-out these differ, and using `context.region`
// opened the home region's EB console — where that environment
// doesn't exist — and evaluated the partition guard against the
// wrong region too, so it could never fire for the fan-out row
// it was added for.
let region = self.region_for(&env);
let Some(url) = console_url(®ion, &env.application, &env.name) else {
self.error_message = Some(no_console_host(®ion));
return;
};
match open_url(&url) {
Ok(()) => {
self.status_message = Some(format!("opened {} in browser", env.name));
}
Err(e) => {
self.error_message = Some(format!("couldn't open browser: {e}"));
}
}
}
/// Open the currently-selected instance (in the Instances tab) in the
/// EC2 console. No-op when no instance is selected.
/// `(region, instance id)` for the instance under Detail's cursor.
///
/// Split out so the region CHOICE is testable — `open_url` can't be
/// observed from a test, and the choice is the part that was wrong.
pub(crate) fn instance_console_target(&self) -> Option<(String, String)> {
let d = self.detail.as_ref()?;
let inst = d.instances.get(d.instances_cursor)?;
Some((self.region_for(&d.env_snapshot), inst.id.clone()))
}
pub(crate) fn open_instance_in_console(&mut self) {
let Some((region, id)) = self.instance_console_target() else {
return;
};
// The ROW's region, matching where the instance list came
// from. This used to be the home region ON PURPOSE: Detail's
// instances were fetched through `self.aws`, so an ID in this
// list came from the home region whatever region the row lived
// in, and the link had to agree with the data it named. 0.30.0
// fixed the fetch and left the compensation behind, which
// turned the workaround into the bug — a row's real instance
// ID pointed at the home region's console, where it resolves
// to "does not exist".
let Some(base) = crate::util::console_base_url(®ion) else {
self.error_message = Some(no_console_host(®ion));
return;
};
let url = format!("{base}/ec2/home?region={region}#InstanceDetails:instanceId={id}");
// `open_url`, not a hand-rolled launcher: this one picked
// between `open` and `xdg-open` and so had no Windows arm,
// while its two siblings in this file did.
match open_url(&url) {
Ok(()) => {
self.status_message = Some(format!("opened {id} in EC2 console"));
}
Err(e) => {
self.error_message = Some(format!("could not open browser: {e}"));
}
}
}
/// Copy the currently-selected instance ID to the clipboard.
pub(crate) fn yank_instance_id(&mut self) {
let Some(d) = self.detail.as_ref() else {
return;
};
let Some(inst) = d.instances.get(d.instances_cursor) else {
return;
};
let id = inst.id.clone();
match yank(&id) {
Ok(()) => self.status_message = Some(format!("yanked instance id: {id}")),
Err(e) => self.error_message = Some(format!("clipboard error: {e}")),
}
}
pub(crate) fn yank_selected(&mut self, kind: YankKind) {
let Some(env) = self.selected_env() else {
self.status_message = Some("nothing to yank".into());
return;
};
let value = match kind {
YankKind::Cname => env.cname.clone(),
YankKind::Name => env.name.clone(),
};
if value.is_empty() {
self.status_message = Some("selected env has no value to yank".into());
return;
}
match yank(&value) {
Ok(()) => {
self.status_message = Some(format!(
"copied {} to clipboard",
match kind {
YankKind::Cname => "CNAME",
YankKind::Name => "name",
}
));
}
Err(e) => self.error_message = Some(format!("clipboard error: {e}")),
}
}
pub(crate) fn export_tsv(&mut self) {
let count = self.view.filtered().len();
let mut out = String::new();
out.push_str(
"NAME\tAPPLICATION\tTIER\tSTATUS\tHEALTH\tPLATFORM\tVERSION\tCNAME\tUPDATED\n",
);
for &i in self.view.filtered() {
let Some(e) = self.env_at(i) else { continue };
let cname = if self.view.redact {
redact_block(&e.cname)
} else {
e.cname.clone()
};
let updated = e.updated.map(|u| u.to_rfc3339()).unwrap_or_default();
out.push_str(&format!(
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
e.name,
e.application,
e.tier,
e.status,
e.health,
e.platform,
e.version_label,
cname,
updated
));
}
match yank(&out) {
Ok(()) => {
self.status_message = Some(format!("exported {count} rows (TSV) to clipboard"));
}
Err(e) => self.error_message = Some(format!("clipboard error: {e}")),
}
}
/// Open the EB applications-page console URL for the selected
/// application in the browser. Mirrors `open_in_console`'s
/// `arboard`-clipboard-on-failure shape so the operator still has
/// the URL available when the browser launch fails (SSH session,
/// no DISPLAY, etc.).
pub(crate) fn open_app_in_console(&mut self) {
let Some(idx) = self.app_table_state.selected() else {
self.status_message = Some("no application selected".into());
return;
};
let Some(name) = self.applications.get(idx).map(|a| a.name.clone()) else {
return;
};
let region = self.context.region.clone();
let app_enc = urlencode(&name);
let Some(base) = crate::util::console_base_url(®ion) else {
self.error_message = Some(no_console_host(®ion));
return;
};
let url = format!(
"{base}/elasticbeanstalk/home?region={region}#/application/overview?applicationName={app_enc}"
);
match open_url(&url) {
Ok(()) => {
self.status_message = Some(format!("opened {name} in browser"));
}
Err(e) => {
self.error_message = Some(format!("couldn't open browser: {e}"));
}
}
}
}