-
Notifications
You must be signed in to change notification settings - Fork 1k
Use cli.toml for default start listen address #4900
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
Open
0monish
wants to merge
1
commit into
clockworklabs:master
Choose a base branch
from
0monish:feature/start-listen-addr-cli-toml
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−5
Open
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ use clap::{Arg, ArgMatches}; | |
| use spacetimedb_paths::SpacetimePaths; | ||
|
|
||
| use crate::util::resolve_sibling_binary; | ||
| use crate::Config; | ||
|
|
||
| pub fn cli() -> clap::Command { | ||
| clap::Command::new("start") | ||
|
|
@@ -15,6 +16,11 @@ pub fn cli() -> clap::Command { | |
| "\ | ||
| Start a local SpacetimeDB instance | ||
|
|
||
| Set a persistent default listen address in cli.toml with: | ||
| listen_addr = \"0.0.0.0:4000\" | ||
|
|
||
| When present, `listen_addr` is used unless `--listen-addr` is passed explicitly. | ||
|
|
||
| Run `spacetime start --help` to see all options.", | ||
| ) | ||
| .disable_help_flag(true) | ||
|
|
@@ -40,9 +46,33 @@ enum Edition { | |
| Cloud, | ||
| } | ||
|
|
||
| pub async fn exec(paths: &SpacetimePaths, args: &ArgMatches) -> anyhow::Result<ExitCode> { | ||
| /// Check whether the forwarded args already contain `--listen-addr` or `-l`. | ||
| /// | ||
| /// Handles all common forms: | ||
| /// - `--listen-addr <value>` (two separate tokens) | ||
| /// - `--listen-addr=<value>` | ||
| /// - `-l <value>` (two separate tokens) | ||
| /// - `-l<value>` (short flag with attached value, e.g. `-l0.0.0.0:4000`) | ||
| fn has_listen_addr_arg(args: impl Iterator<Item = impl AsRef<std::ffi::OsStr>>) -> bool { | ||
| for arg in args { | ||
| let s = arg.as_ref().to_string_lossy(); | ||
| if s == "--listen-addr" || s.starts_with("--listen-addr=") { | ||
| return true; | ||
| } | ||
| if s == "-l" | ||
| || (s.starts_with("-l") | ||
| && !s.starts_with("--") | ||
| && s.as_bytes().get(2).is_some_and(|b| !b.is_ascii_alphabetic())) | ||
|
Collaborator
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. can you explain this block? at first glance it seems like if |
||
| { | ||
| return true; | ||
| } | ||
| } | ||
| false | ||
| } | ||
|
|
||
| pub async fn exec(config: Config, paths: &SpacetimePaths, args: &ArgMatches) -> anyhow::Result<ExitCode> { | ||
| let edition = args.get_one::<Edition>("edition").unwrap(); | ||
| let args = args.get_many::<OsString>("args").unwrap_or_default(); | ||
| let forwarded_args: Vec<OsString> = args.get_many::<OsString>("args").unwrap_or_default().cloned().collect(); | ||
| let bin_name = match edition { | ||
| Edition::Standalone => "spacetimedb-standalone", | ||
| Edition::Cloud => "spacetimedb-cloud", | ||
|
|
@@ -53,8 +83,15 @@ pub async fn exec(paths: &SpacetimePaths, args: &ArgMatches) -> anyhow::Result<E | |
| .arg("--data-dir") | ||
| .arg(&paths.data_dir) | ||
| .arg("--jwt-key-dir") | ||
| .arg(&paths.cli_config_dir) | ||
| .args(args); | ||
| .arg(&paths.cli_config_dir); | ||
|
|
||
| if !has_listen_addr_arg(forwarded_args.iter()) | ||
| && let Some(config_addr) = config.start_listen_addr() | ||
| { | ||
| cmd.arg("--listen-addr").arg(config_addr); | ||
| } | ||
|
|
||
| cmd.args(&forwarded_args); | ||
|
|
||
| exec_replace(&mut cmd).with_context(|| format!("exec failed for {}", bin_path.display())) | ||
| } | ||
|
|
@@ -103,3 +140,73 @@ pub(crate) fn exec_replace(cmd: &mut Command) -> io::Result<ExitCode> { | |
| .map(|status| ExitCode::from(status.code().unwrap_or(1).try_into().unwrap_or(1))) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn detects_long_flag_separate_value() { | ||
| assert!(has_listen_addr_arg(["--listen-addr", "0.0.0.0:4000"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_long_flag_equals_value() { | ||
| assert!(has_listen_addr_arg(["--listen-addr=0.0.0.0:4000"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_short_flag_separate_value() { | ||
| assert!(has_listen_addr_arg(["-l", "0.0.0.0:4000"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_short_flag_attached_value() { | ||
| assert!(has_listen_addr_arg(["-l0.0.0.0:4000"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_short_flag_attached_ipv6() { | ||
| assert!(has_listen_addr_arg(["-l[::1]:4000"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ignores_unrelated_long_flag() { | ||
| assert!(!has_listen_addr_arg(["--data-dir", "/tmp"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ignores_unrelated_short_flag() { | ||
| assert!(!has_listen_addr_arg(["-d", "/tmp"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_false_positive_on_hyphen_l_prefix_flag() { | ||
| assert!(!has_listen_addr_arg(["-log"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_false_positive_on_hyphen_li() { | ||
| assert!(!has_listen_addr_arg(["-li"].iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn returns_false_for_empty() { | ||
| let empty: Vec<&str> = vec![]; | ||
| assert!(!has_listen_addr_arg(empty.iter())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_among_many_args() { | ||
| assert!(has_listen_addr_arg( | ||
| ["--data-dir", "/tmp", "--listen-addr", "0.0.0.0:4000", "--in-memory"].iter() | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn detects_short_among_many_args() { | ||
| assert!(has_listen_addr_arg( | ||
| ["--data-dir", "/tmp", "-l", "127.0.0.1:5000"].iter() | ||
| )); | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
this function can be removed entirely. if
--listen-addris passed multiple times tospacetimedb-standalone, the last one will be respected.I would add a comment to this effect when passing
--listen-addr.