Skip to content

Commit

Permalink
feat(cli): improve local IP detection (#5817)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Dec 13, 2022
1 parent a9b4cf2 commit 76204b8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/improve-local-ip-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Improve local IP address detection with user selection.
31 changes: 29 additions & 2 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::{
interface::{AppInterface, DevProcess, ExitReason, Interface},
CommandExt, Result,
};
use clap::{ArgAction, Parser};

use anyhow::{bail, Context};
use clap::{ArgAction, Parser};
use log::{error, info, warn};
use once_cell::sync::OnceCell;
use shared_child::SharedChild;
Expand Down Expand Up @@ -88,7 +88,34 @@ fn command_internal(mut options: Options) -> Result<()> {

fn local_ip_address() -> &'static IpAddr {
static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
LOCAL_IP.get_or_init(|| local_ip_address::local_ip().expect("failed to resolve local IP address"))
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()
}
}
})
}

pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
Expand Down

0 comments on commit 76204b8

Please sign in to comment.