Skip to content

Commit ec007ef

Browse files
authored
feat: use local_ip() and fallback to prompt (#6290)
1 parent 14d03d4 commit ec007ef

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

.changes/cli-mobile-auto-ip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'cli.rs': 'patch'
3+
---
4+
5+
Auto select an external IP for mobile development and fallback to prompting the user.

tooling/cli/src/dev.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,39 @@ fn command_internal(mut options: Options) -> Result<()> {
8989
pub fn local_ip_address() -> &'static IpAddr {
9090
static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
9191
LOCAL_IP.get_or_init(|| {
92-
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
93-
.expect("failed to list networks")
94-
.into_iter()
95-
.map(|(_, ipaddr)| ipaddr)
96-
.filter(|ipaddr| match ipaddr {
97-
IpAddr::V4(i) => i != &Ipv4Addr::LOCALHOST,
98-
_ => false,
99-
})
100-
.collect();
101-
match addresses.len() {
102-
0 => panic!("No external IP detected."),
103-
1 => {
104-
let ipaddr = addresses.first().unwrap();
105-
log::info!("Detected external IP {ipaddr}.");
106-
*ipaddr
107-
}
108-
_ => {
109-
let selected = dialoguer::Select::with_theme(&dialoguer::theme::ColorfulTheme::default())
110-
.with_prompt("What external IP should we use for your development server?")
111-
.items(&addresses)
112-
.default(0)
113-
.interact()
114-
.expect("failed to select external IP");
115-
*addresses.get(selected).unwrap()
92+
let ip = local_ip_address::local_ip().unwrap_or_else(|_| {
93+
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
94+
.expect("failed to list networks")
95+
.into_iter()
96+
.map(|(_, ipaddr)| ipaddr)
97+
.filter(|ipaddr| match ipaddr {
98+
IpAddr::V4(i) => i != &Ipv4Addr::LOCALHOST,
99+
_ => false,
100+
})
101+
.collect();
102+
match addresses.len() {
103+
0 => panic!("No external IP detected."),
104+
1 => {
105+
let ipaddr = addresses.first().unwrap();
106+
*ipaddr
107+
}
108+
_ => {
109+
let selected = dialoguer::Select::with_theme(&dialoguer::theme::ColorfulTheme::default())
110+
.with_prompt(
111+
"Failed to detect external IP, What IP should we use to access your development server?",
112+
)
113+
.items(&addresses)
114+
.default(0)
115+
.interact()
116+
.expect("failed to select external IP");
117+
*addresses.get(selected).unwrap()
118+
}
116119
}
117-
}
120+
});
121+
122+
log::info!("Using {ip} to access the development server.");
123+
124+
ip
118125
})
119126
}
120127

0 commit comments

Comments
 (0)