railwayapp 5.37.6

Interact with Railway via CLI
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use colored::*;
use inquire::{
    Autocomplete,
    validator::{Validation, ValueRequiredValidator},
};
use std::{
    borrow::Cow,
    fmt::Display,
    path::{MAIN_SEPARATOR, Path, PathBuf},
};

use crate::{
    commands::{Configs, queries::project::ProjectProjectServicesEdgesNode},
    controllers::project::ProjectServiceInstanceNode,
    controllers::variables::Variable,
};
use anyhow::{Context, Result};

/// Set while a full-screen TUI holds the terminal — raw mode, alternate
/// screen, and its own crossterm event reader. An inquire prompt started then
/// can never complete: the TUI's event stream consumes the keystrokes while
/// the prompt blocks forever, and the next frame paints over its output.
static TERMINAL_OWNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Mark the terminal as owned (or released) by a full-screen TUI. While set,
/// every prompt in this module fails fast instead of deadlocking.
pub fn set_terminal_owned(owned: bool) {
    TERMINAL_OWNED.store(owned, std::sync::atomic::Ordering::SeqCst);
}

/// Whether a full-screen TUI currently owns the terminal. Code that would
/// prompt as a fallback should treat this the same as a non-interactive stdin.
pub fn terminal_owned() -> bool {
    TERMINAL_OWNED.load(std::sync::atomic::Ordering::SeqCst)
}

/// Refuse to prompt while a TUI owns the terminal: loud in debug builds so
/// the call site gets fixed, a clean error in release builds so the user gets
/// a message instead of a hang.
fn ensure_promptable() -> Result<()> {
    if terminal_owned() {
        debug_assert!(
            false,
            "interactive prompt attempted while a TUI owns the terminal"
        );
        anyhow::bail!("an interactive prompt can't run while the TUI holds the terminal");
    }
    Ok(())
}

pub fn prompt_options<T: Display>(message: &str, options: Vec<T>) -> Result<T> {
    ensure_promptable()?;
    let select = inquire::Select::new(message, options);
    select
        .with_render_config(Configs::get_render_config())
        .prompt()
        .context("Failed to prompt for options")
}

pub fn prompt_options_skippable<T: Display>(message: &str, options: Vec<T>) -> Result<Option<T>> {
    ensure_promptable()?;
    let select = inquire::Select::new(message, options);
    select
        .with_render_config(Configs::get_render_config())
        .prompt_skippable()
        .context("Failed to prompt for options")
}

pub fn prompt_options_skippable_with_default<T: Display>(
    message: &str,
    options: Vec<T>,
    default_index: usize,
) -> Result<Option<T>> {
    ensure_promptable()?;
    let select = inquire::Select::new(message, options);
    select
        .with_render_config(Configs::get_render_config())
        .with_starting_cursor(default_index)
        .prompt_skippable()
        .context("Failed to prompt for options")
}

pub fn prompt_text(message: &str) -> Result<String> {
    ensure_promptable()?;
    let select = inquire::Text::new(message);
    select
        .with_render_config(Configs::get_render_config())
        .prompt()
        .context("Failed to prompt for options")
}

/// Prompt for a secret (token, key): input is masked as it's typed and never
/// echoed back, with no confirmation round-trip.
pub fn prompt_secret(message: &str) -> Result<String> {
    ensure_promptable()?;
    inquire::Password::new(message)
        .with_render_config(Configs::get_render_config())
        .with_display_mode(inquire::PasswordDisplayMode::Masked)
        .without_confirmation()
        .prompt()
        .context("Failed to prompt for secret")
}

pub fn prompt_u64_with_placeholder_and_validation_and_cancel(
    message: &str,
    placeholder: &str,
) -> Result<Option<String>> {
    ensure_promptable()?;
    let validator = |input: &str| {
        if input.parse::<u64>().is_ok() {
            Ok(Validation::Valid)
        } else {
            Ok(Validation::Invalid("Not a valid number".into()))
        }
    };
    let select = inquire::Text::new(message);
    select
        .with_render_config(Configs::get_render_config())
        .with_placeholder(placeholder)
        .with_validator(ValueRequiredValidator::new("Input most not be empty"))
        .with_validator(validator)
        .prompt_skippable()
        .context("Failed to prompt for options")
}

pub fn prompt_text_with_placeholder_if_blank(
    message: &str,
    placeholder: &str,
    blank_message: &str,
) -> Result<String> {
    ensure_promptable()?;
    let select = inquire::Text::new(message);
    select
        .with_render_config(Configs::get_render_config())
        .with_placeholder(placeholder)
        .with_formatter(&|input: &str| {
            if input.is_empty() {
                String::from(blank_message)
            } else {
                input.to_string()
            }
        })
        .prompt()
        .context("Failed to prompt for options")
}

pub fn prompt_text_with_placeholder_disappear(message: &str, placeholder: &str) -> Result<String> {
    ensure_promptable()?;
    let select = inquire::Text::new(message);
    select
        .with_render_config(Configs::get_render_config())
        .with_placeholder(placeholder)
        .prompt()
        .context("Failed to prompt for options")
}

pub fn prompt_text_with_placeholder_disappear_skippable(
    message: &str,
    placeholder: &str,
) -> Result<Option<String>> {
    ensure_promptable()?;
    let select = inquire::Text::new(message);
    select
        .with_render_config(Configs::get_render_config())
        .with_placeholder(placeholder)
        .prompt_skippable()
        .context("Failed to prompt for options")
}

pub fn prompt_confirm_with_default(message: &str, default: bool) -> Result<bool> {
    ensure_promptable()?;
    let confirm = inquire::Confirm::new(message);
    confirm
        .with_default(default)
        .with_render_config(Configs::get_render_config())
        .prompt()
        .context("Failed to prompt for confirm")
}

pub fn prompt_confirm_with_default_with_cancel(
    message: &str,
    default: bool,
) -> Result<Option<bool>> {
    ensure_promptable()?;
    let confirm = inquire::Confirm::new(message);
    confirm
        .with_default(default)
        .with_render_config(Configs::get_render_config())
        .prompt_skippable()
        .context("Failed to prompt for confirm")
}

pub fn prompt_multi_options<T: Display>(message: &str, options: Vec<T>) -> Result<Vec<T>> {
    ensure_promptable()?;
    let multi_select = inquire::MultiSelect::new(message, options);
    multi_select
        .with_render_config(Configs::get_render_config())
        .prompt()
        .context("Failed to prompt for multi options")
}

pub fn prompt_select<T: Display>(message: &str, options: Vec<T>) -> Result<T> {
    ensure_promptable()?;
    inquire::Select::new(message, options)
        .with_render_config(Configs::get_render_config())
        .prompt()
        .context("Failed to prompt for select")
}

pub fn prompt_select_with_cancel<T: Display>(message: &str, options: Vec<T>) -> Result<Option<T>> {
    ensure_promptable()?;
    inquire::Select::new(message, options)
        .with_render_config(Configs::get_render_config())
        .prompt_skippable()
        .context("Failed to prompt for select")
}

pub fn fake_select(message: &str, selected: &str) {
    eprintln!("{} {} {}", ">".green(), message, selected.cyan().bold());
}

pub fn prompt_variables(service_name: Option<&str>) -> Result<Vec<Variable>> {
    let mut variables: Vec<Variable> = Vec::new();
    loop {
        if let Some(variable) = prompt_text_with_placeholder_disappear_skippable(
            if let Some(ref service_name) = service_name {
                format!("Enter a variable for {service_name} <esc to skip>")
            } else {
                String::from("Enter a variable <esc to skip>")
            }
            .as_str(),
            "<KEY=VALUE, press esc to finish>",
        )? {
            if variable.is_empty() {
                break Ok(variables);
            }
            match variable.parse::<Variable>() {
                Ok(v) => variables.push(v),
                Err(err) => eprintln!("{} {:?}", "Warn".yellow(), err),
            }
        } else {
            break Ok(variables);
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct PromptService<'a>(pub &'a ProjectProjectServicesEdgesNode);

impl Display for PromptService<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0.name)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct PromptServiceInstance<'a>(pub &'a ProjectServiceInstanceNode);

impl Display for PromptServiceInstance<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0.service_name)
    }
}

/// Bash style completion of paths
#[derive(Clone)]
pub struct PathAutocompleter;

impl PathAutocompleter {
    /// Parse input path and extract directory and filename prefix
    fn parse_input(input: &str) -> (Cow<'_, Path>, Cow<'_, str>) {
        if input.is_empty() {
            return (Cow::Borrowed(Path::new(".")), Cow::Borrowed(""));
        }

        let path = Path::new(input);

        // Check if input ends with a path separator
        if input.ends_with(MAIN_SEPARATOR) {
            (Cow::Borrowed(path), Cow::Borrowed(""))
        } else if !input.contains(MAIN_SEPARATOR) {
            // Input is just a filename with no path separators - search in current directory
            (Cow::Borrowed(Path::new(".")), Cow::Borrowed(input))
        } else {
            let parent = path.parent().unwrap_or(Path::new("."));
            let prefix = path.file_name().and_then(|s| s.to_str()).unwrap_or("");

            (Cow::Borrowed(parent), Cow::Borrowed(prefix))
        }
    }

    /// Build the completion path from components
    fn build_completion(input: &str, dir: &Path, filename: &str, is_dir: bool) -> String {
        let mut result = if input.ends_with(MAIN_SEPARATOR) {
            // Input ends with separator, append filename directly
            format!("{input}{filename}")
        } else if dir == Path::new(".") && !input.contains(MAIN_SEPARATOR) {
            // Current directory and input has no path separators, use filename only
            filename.to_string()
        } else if dir == Path::new(".") {
            // Current directory but input had separators, preserve the ./ format
            format!(".{MAIN_SEPARATOR}{filename}")
        } else {
            // Build full path
            let mut path = dir.to_string_lossy().into_owned();
            if !path.ends_with(MAIN_SEPARATOR) {
                path.push(MAIN_SEPARATOR);
            }
            path.push_str(filename);
            path
        };

        if is_dir {
            result.push(MAIN_SEPARATOR);
        }

        result
    }
}

impl Autocomplete for PathAutocompleter {
    fn get_suggestions(&mut self, _input: &str) -> Result<Vec<String>, inquire::CustomUserError> {
        Ok(vec![]) // Hide suggestion list for bash-style completion
    }

    fn get_completion(
        &mut self,
        input: &str,
        _highlighted_suggestion: Option<String>,
    ) -> Result<inquire::autocompletion::Replacement, inquire::CustomUserError> {
        let (dir, prefix) = Self::parse_input(input);

        // Early return if directory doesn't exist or can't be read
        if !dir.exists() || !dir.is_dir() {
            return Ok(inquire::autocompletion::Replacement::None);
        }

        let entries = match std::fs::read_dir(&*dir) {
            Ok(entries) => entries,
            Err(_) => return Ok(inquire::autocompletion::Replacement::None),
        };

        let mut matches = Vec::new();

        // Collect all matching entries
        for entry in entries.flatten() {
            let file_name = entry.file_name();
            let Some(name_str) = file_name.to_str() else {
                continue;
            };

            if !name_str.starts_with(&*prefix) {
                continue;
            }

            let is_dir = entry.file_type().is_ok_and(|ft| ft.is_dir());
            let completion = Self::build_completion(input, &dir, name_str, is_dir);
            matches.push((name_str.to_string(), completion));
        }

        if matches.is_empty() {
            return Ok(inquire::autocompletion::Replacement::None);
        }

        // Check for exact match first (e.g., "test" matches "test" exactly, not "test-two")
        if let Some((_, completion)) = matches.iter().find(|(name, _)| name == &*prefix) {
            return Ok(inquire::autocompletion::Replacement::Some(
                completion.clone(),
            ));
        }

        // Find the closest match (shortest name that starts with prefix)
        let closest_match = matches.iter().min_by_key(|(name, _)| name.len()).unwrap();

        Ok(inquire::autocompletion::Replacement::Some(
            closest_match.1.clone(),
        ))
    }
}

pub fn prompt_path(message: &str) -> Result<PathBuf> {
    ensure_promptable()?;
    inquire::Text::new(message)
        .with_autocomplete(PathAutocompleter)
        .with_render_config(Configs::get_render_config())
        .prompt()
        .map(PathBuf::from)
        .context("Failed to prompt for path")
}

pub fn prompt_path_with_default(message: &str, default: &str) -> Result<PathBuf> {
    ensure_promptable()?;
    inquire::Text::new(message)
        .with_autocomplete(PathAutocompleter)
        .with_render_config(Configs::get_render_config())
        .with_default(default)
        .prompt()
        .map(PathBuf::from)
        .context("Failed to prompt for path")
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The guard is what turns "prompt under the TUI" from a silent deadlock
    /// (two crossterm readers racing for the same keystrokes) into a loud
    /// failure: an assert in debug builds, a clean error in release.
    #[test]
    fn prompts_refuse_while_a_tui_owns_the_terminal() {
        set_terminal_owned(true);
        let hook = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        let result = std::panic::catch_unwind(ensure_promptable);
        std::panic::set_hook(hook);
        set_terminal_owned(false);
        match result {
            // Release builds: the prompt call fails instead of hanging.
            Ok(outcome) => assert!(outcome.is_err()),
            // Debug builds: the debug_assert fired, naming the call site.
            Err(_) => {}
        }
        assert!(ensure_promptable().is_ok());
    }
}