Skip to content

Commit 8a623a8

Browse files
committed
Make the hostname on MacOS omit the ".local" at the end.
1 parent a3409f3 commit 8a623a8

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/main/java/xyz/e3ndr/jeofetch/Jeofetch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Jeofetch {
3131
USERNAME = System.getProperty("user.name", "user");
3232

3333
try {
34-
HOSTNAME = InetAddress.getLocalHost().getHostName();
34+
HOSTNAME = SystemUtils.getSystemHandler().getHostname();
3535
} catch (UnknownHostException e) {
3636
HOSTNAME = "localhost";
3737
}

src/main/java/xyz/e3ndr/jeofetch/SystemUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44

5+
import lombok.Getter;
56
import xyz.e3ndr.jeofetch.system.ISystem;
67
import xyz.e3ndr.jeofetch.system.MacOSSystem;
78
import xyz.e3ndr.jeofetch.system.UnixSystem;
@@ -12,7 +13,7 @@
1213

1314
public class SystemUtils {
1415
private static UnknownSystem unknownSystemHandler = new UnknownSystem();
15-
private static ISystem systemHandler;
16+
private static @Getter ISystem systemHandler;
1617

1718
static {
1819
String name = System.getProperty("os.name", "").toLowerCase();

src/main/java/xyz/e3ndr/jeofetch/system/ISystem.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package xyz.e3ndr.jeofetch.system;
22

33
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.UnknownHostException;
46

57
import xyz.e3ndr.jeofetch.types.CpuInfo;
68

@@ -13,5 +15,9 @@ public interface ISystem {
1315
public String getOsName() throws IOException;
1416

1517
public String getVersion() throws IOException;
18+
19+
default String getHostname() throws UnknownHostException {
20+
return InetAddress.getLocalHost().getHostName();
21+
}
1622

1723
}

src/main/java/xyz/e3ndr/jeofetch/system/MacOSSystem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.net.InetAddress;
6+
import java.net.UnknownHostException;
57
import java.nio.charset.StandardCharsets;
68
import java.util.ArrayList;
79
import java.util.List;
@@ -33,6 +35,16 @@ public String getOsName() throws IOException {
3335
return System.getProperty("os.name", "Unknown");
3436
}
3537

38+
public String getHostname() throws UnknownHostException {
39+
String hostname = InetAddress.getLocalHost().getHostName();
40+
41+
if (hostname.endsWith(".local")) {
42+
return hostname.substring(0, hostname.length() - ".local".length());
43+
} else {
44+
return hostname;
45+
}
46+
}
47+
3648
private static CpuInfo getCpuInfo_sysctl() throws IOException {
3749
String[] sysctl = IOUtil.readInputStreamString(Runtime.getRuntime().exec("sysctl -a").getInputStream(), StandardCharsets.UTF_8).split("\n");
3850

0 commit comments

Comments
 (0)