Skip to content

Commit

Permalink
feat: use local_ip() and fallback to prompt (#6290)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Feb 16, 2023
1 parent 14d03d4 commit ec007ef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changes/cli-mobile-auto-ip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cli.rs': 'patch'
---

Auto select an external IP for mobile development and fallback to prompting the user.
57 changes: 32 additions & 25 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,39 @@ fn command_internal(mut options: Options) -> Result<()> {
pub fn local_ip_address() -> &'static IpAddr {
static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
LOCAL_IP.get_or_init(|| {
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
.expect("failed to list networks")
.into_iter()
.map(|(_, ipaddr)| ipaddr)
.filter(|ipaddr| match ipaddr {
IpAddr::V4(i) => i != &Ipv4Addr::LOCALHOST,
_ => false,
})
.collect();
match addresses.len() {
0 => panic!("No external IP detected."),
1 => {
let ipaddr = addresses.first().unwrap();
log::info!("Detected external IP {ipaddr}.");
*ipaddr
}
_ => {
let selected = dialoguer::Select::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt("What external IP should we use for your development server?")
.items(&addresses)
.default(0)
.interact()
.expect("failed to select external IP");
*addresses.get(selected).unwrap()
let ip = local_ip_address::local_ip().unwrap_or_else(|_| {
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
.expect("failed to list networks")
.into_iter()
.map(|(_, ipaddr)| ipaddr)
.filter(|ipaddr| match ipaddr {
IpAddr::V4(i) => i != &Ipv4Addr::LOCALHOST,
_ => false,
})
.collect();
match addresses.len() {
0 => panic!("No external IP detected."),
1 => {
let ipaddr = addresses.first().unwrap();
*ipaddr
}
_ => {
let selected = dialoguer::Select::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt(
"Failed to detect external IP, What IP should we use to access your development server?",
)
.items(&addresses)
.default(0)
.interact()
.expect("failed to select external IP");
*addresses.get(selected).unwrap()
}
}
}
});

log::info!("Using {ip} to access the development server.");

ip
})
}

Expand Down

0 comments on commit ec007ef

Please sign in to comment.