reibitto / command-center

A CLI-based launcher and general productivity tool.
Apache License 2.0
103 stars 12 forks source link

LocalIPCommand for other OSes #46

Closed tuleism closed 4 years ago

tuleism commented 4 years ago

Currently, to get local IP address, we use:

localIP <- PCommand("ipconfig", "getifaddr", "en0").string.bimap(CommandError.UnexpectedException, _.trim)

It works for macOS, but for Linux the command can vary based on the distro, not to mention Windows. For example, I'm using Manjaro and the default command to list addresses is ip a, which then needs some command line magic to extract the IP address.

We can use Java/Scala to get this information. This seems to work with my box (multiple interfaces, running together with docker and VMs)

val interfaces = NetworkInterface.getNetworkInterfaces.asScala.toList
interfaces.filter { interface =>
  !interface.isLoopback && !interface.isVirtual && interface.isUp
}.flatMap { interface =>
  interface.getInetAddresses.asScala.collect {
    case address: Inet4Address => interface.getDisplayName -> address.getHostAddress
  }
}
reibitto commented 4 years ago

That is indeed better and cross-platform. That snippet works fine here too. I think that would be a good change.