-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow only common trigger options in multi-trigger apps #2257
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -166,14 +166,28 @@ impl UpCommand { | |||||||||
let trigger_cmds = trigger_command_for_resolved_app_source(&resolved_app_source) | ||||||||||
.with_context(|| format!("Couldn't find trigger executor for {app_source}"))?; | ||||||||||
|
||||||||||
let is_multi = trigger_cmds.len() > 1; | ||||||||||
|
||||||||||
if self.help { | ||||||||||
if is_multi { | ||||||||||
// For now, only common flags are allowed on multi-trigger apps. | ||||||||||
let mut child = self | ||||||||||
.start_trigger(trigger_command(HELP_ARGS_ONLY_TRIGGER_TYPE), None) | ||||||||||
.await?; | ||||||||||
let _ = child.wait().await?; | ||||||||||
return Ok(()); | ||||||||||
} | ||||||||||
for cmd in trigger_cmds { | ||||||||||
let mut help_process = self.start_trigger(cmd.clone(), None).await?; | ||||||||||
_ = help_process.wait().await; | ||||||||||
} | ||||||||||
return Ok(()); | ||||||||||
} | ||||||||||
|
||||||||||
if is_multi { | ||||||||||
self.allow_only_common_flags()?; | ||||||||||
} | ||||||||||
|
||||||||||
let mut locked_app = self | ||||||||||
.load_resolved_app_source(resolved_app_source, &working_dir) | ||||||||||
.await?; | ||||||||||
|
@@ -190,7 +204,6 @@ impl UpCommand { | |||||||||
}; | ||||||||||
|
||||||||||
let trigger_processes = self.start_trigger_processes(trigger_cmds, run_opts).await?; | ||||||||||
let is_multi = trigger_processes.len() > 1; | ||||||||||
let pids = get_pids(&trigger_processes); | ||||||||||
|
||||||||||
set_kill_on_ctrl_c(&pids)?; | ||||||||||
|
@@ -410,6 +423,57 @@ impl UpCommand { | |||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
fn allow_only_common_flags(&self) -> anyhow::Result<()> { | ||||||||||
allow_only_common_flags(&self.trigger_args) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
const COMMON_FLAGS: &[&str] = &[ | ||||||||||
"-L", | ||||||||||
"--log-dir", | ||||||||||
"--disable-cache", | ||||||||||
"--cache", | ||||||||||
"--disable-pooling", | ||||||||||
"--follow", | ||||||||||
"-q", | ||||||||||
"--quiet", | ||||||||||
"--sh", | ||||||||||
"--shush", | ||||||||||
"--allow-transient-write", | ||||||||||
"--runtime-config-file", | ||||||||||
"--state-dir", | ||||||||||
"--key-value", | ||||||||||
"--sqlite", | ||||||||||
"--help-args-only", | ||||||||||
]; | ||||||||||
|
||||||||||
fn allow_only_common_flags(args: &[OsString]) -> anyhow::Result<()> { | ||||||||||
for i in 0..(args.len()) { | ||||||||||
let Some(value) = args[i].to_str() else { | ||||||||||
Comment on lines
+452
to
+453
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
// Err on the side of forgiveness | ||||||||||
continue; | ||||||||||
}; | ||||||||||
if value.starts_with('-') { | ||||||||||
// Flag candidate. Is it allowed? | ||||||||||
if !is_common_flag(value) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I would personally inline this function. |
||||||||||
anyhow::bail!("Cannot use trigger option '{value}'. Apps with multiple trigger types do not yet support options specific to one trigger type."); | ||||||||||
} | ||||||||||
} else if i >= 1 { | ||||||||||
// Value candidate. Does it immediately follow a flag? | ||||||||||
let Some(prev) = args[i - 1].to_str() else { | ||||||||||
continue; | ||||||||||
}; | ||||||||||
if !prev.starts_with('-') { | ||||||||||
anyhow::bail!("Cannot use trigger option '{value}'. Apps with multiple trigger types do not yet support options specific to one trigger type."); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
Ok(()) | ||||||||||
} | ||||||||||
|
||||||||||
fn is_common_flag(candidate: &str) -> bool { | ||||||||||
COMMON_FLAGS.contains(&candidate) | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(windows)] | ||||||||||
|
@@ -703,4 +767,28 @@ mod test { | |||||||||
UpCommand::try_parse_from(["up", "--listen", "127.0.0.1:39453"]) | ||||||||||
.expect("Failed to parse implicit source with trigger option"); | ||||||||||
} | ||||||||||
|
||||||||||
#[test] | ||||||||||
fn multi_accept_no_trigger_args() { | ||||||||||
allow_only_common_flags(&[]).expect("should allow no trigger args"); | ||||||||||
} | ||||||||||
|
||||||||||
#[test] | ||||||||||
fn multi_accept_only_common_args() { | ||||||||||
let args: Vec<_> = ["--log-dir", "/fie", "-q", "--sqlite", "SOME SQL"] | ||||||||||
.iter() | ||||||||||
.map(OsString::from) | ||||||||||
.collect(); | ||||||||||
allow_only_common_flags(&args).expect("should allow common trigger args"); | ||||||||||
} | ||||||||||
|
||||||||||
#[test] | ||||||||||
fn multi_reject_trigger_specific_args() { | ||||||||||
let args: Vec<_> = ["--log-dir", "/fie", "--listen", "some.address"] | ||||||||||
.iter() | ||||||||||
.map(OsString::from) | ||||||||||
.collect(); | ||||||||||
let e = allow_only_common_flags(&args).expect_err("should reject trigger-specific args"); | ||||||||||
assert!(e.to_string().contains("'--listen'")); | ||||||||||
} | ||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we comment that these flags are common to all well-known trigger types.