Skip to content

Commit 3658ba6

Browse files
authored
Merge pull request #2257 from itowlson/restrict-multi-trigger-flags
Allow only common trigger options in multi-trigger apps
2 parents 31d1075 + 296613e commit 3658ba6

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

src/commands/up.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,28 @@ impl UpCommand {
166166
let trigger_cmds = trigger_command_for_resolved_app_source(&resolved_app_source)
167167
.with_context(|| format!("Couldn't find trigger executor for {app_source}"))?;
168168

169+
let is_multi = trigger_cmds.len() > 1;
170+
169171
if self.help {
172+
if is_multi {
173+
// For now, only common flags are allowed on multi-trigger apps.
174+
let mut child = self
175+
.start_trigger(trigger_command(HELP_ARGS_ONLY_TRIGGER_TYPE), None)
176+
.await?;
177+
let _ = child.wait().await?;
178+
return Ok(());
179+
}
170180
for cmd in trigger_cmds {
171181
let mut help_process = self.start_trigger(cmd.clone(), None).await?;
172182
_ = help_process.wait().await;
173183
}
174184
return Ok(());
175185
}
176186

187+
if is_multi {
188+
self.allow_only_common_flags()?;
189+
}
190+
177191
let mut locked_app = self
178192
.load_resolved_app_source(resolved_app_source, &working_dir)
179193
.await?;
@@ -190,7 +204,6 @@ impl UpCommand {
190204
};
191205

192206
let trigger_processes = self.start_trigger_processes(trigger_cmds, run_opts).await?;
193-
let is_multi = trigger_processes.len() > 1;
194207
let pids = get_pids(&trigger_processes);
195208

196209
set_kill_on_ctrl_c(&pids)?;
@@ -410,6 +423,57 @@ impl UpCommand {
410423
}
411424
}
412425
}
426+
427+
fn allow_only_common_flags(&self) -> anyhow::Result<()> {
428+
allow_only_common_flags(&self.trigger_args)
429+
}
430+
}
431+
432+
const COMMON_FLAGS: &[&str] = &[
433+
"-L",
434+
"--log-dir",
435+
"--disable-cache",
436+
"--cache",
437+
"--disable-pooling",
438+
"--follow",
439+
"-q",
440+
"--quiet",
441+
"--sh",
442+
"--shush",
443+
"--allow-transient-write",
444+
"--runtime-config-file",
445+
"--state-dir",
446+
"--key-value",
447+
"--sqlite",
448+
"--help-args-only",
449+
];
450+
451+
fn allow_only_common_flags(args: &[OsString]) -> anyhow::Result<()> {
452+
for i in 0..(args.len()) {
453+
let Some(value) = args[i].to_str() else {
454+
// Err on the side of forgiveness
455+
continue;
456+
};
457+
if value.starts_with('-') {
458+
// Flag candidate. Is it allowed?
459+
if !is_common_flag(value) {
460+
anyhow::bail!("Cannot use trigger option '{value}'. Apps with multiple trigger types do not yet support options specific to one trigger type.");
461+
}
462+
} else if i >= 1 {
463+
// Value candidate. Does it immediately follow a flag?
464+
let Some(prev) = args[i - 1].to_str() else {
465+
continue;
466+
};
467+
if !prev.starts_with('-') {
468+
anyhow::bail!("Cannot use trigger option '{value}'. Apps with multiple trigger types do not yet support options specific to one trigger type.");
469+
}
470+
}
471+
}
472+
Ok(())
473+
}
474+
475+
fn is_common_flag(candidate: &str) -> bool {
476+
COMMON_FLAGS.contains(&candidate)
413477
}
414478

415479
#[cfg(windows)]
@@ -703,4 +767,28 @@ mod test {
703767
UpCommand::try_parse_from(["up", "--listen", "127.0.0.1:39453"])
704768
.expect("Failed to parse implicit source with trigger option");
705769
}
770+
771+
#[test]
772+
fn multi_accept_no_trigger_args() {
773+
allow_only_common_flags(&[]).expect("should allow no trigger args");
774+
}
775+
776+
#[test]
777+
fn multi_accept_only_common_args() {
778+
let args: Vec<_> = ["--log-dir", "/fie", "-q", "--sqlite", "SOME SQL"]
779+
.iter()
780+
.map(OsString::from)
781+
.collect();
782+
allow_only_common_flags(&args).expect("should allow common trigger args");
783+
}
784+
785+
#[test]
786+
fn multi_reject_trigger_specific_args() {
787+
let args: Vec<_> = ["--log-dir", "/fie", "--listen", "some.address"]
788+
.iter()
789+
.map(OsString::from)
790+
.collect();
791+
let e = allow_only_common_flags(&args).expect_err("should reject trigger-specific args");
792+
assert!(e.to_string().contains("'--listen'"));
793+
}
706794
}

0 commit comments

Comments
 (0)