Skip to content

Commit 1431ed6

Browse files
Merge pull request #16 from nihaltp/feat/hostname
Add HostnameCommand to display hostname
2 parents b412593 + 8060cfc commit 1431ed6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private static void registerCommands(Map<String, Command> commands) {
5151
commands.put("help", new HelpCommand(commands));
5252
commands.put("exit", new ExitCommand());
5353
commands.put("ver", new VersionCommand());
54+
commands.put("hostname", new HostnameCommand());
5455
commands.put("whoami", new WhoamiCommand());
5556
commands.put("touch", new TouchCommand());
5657
commands.put("time", new TimeCommand());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mycmd.commands;
2+
3+
import java.net.InetAddress;
4+
5+
import com.mycmd.Command;
6+
import com.mycmd.ShellContext;
7+
8+
public class HostnameCommand implements Command {
9+
@Override
10+
public void execute(String[] args, ShellContext context) {
11+
String hostname = System.getenv("COMPUTERNAME");
12+
if (hostname == null) {
13+
try {
14+
hostname = InetAddress.getLocalHost().getHostName();
15+
} catch (Exception e) {
16+
hostname = "Unknown Host";
17+
}
18+
}
19+
System.out.println(hostname);
20+
}
21+
}

0 commit comments

Comments
 (0)