diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 2a1fafb..1cad5b4 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -51,6 +51,7 @@ private static void registerCommands(Map commands) { commands.put("help", new HelpCommand(commands)); commands.put("exit", new ExitCommand()); commands.put("ver", new VersionCommand()); + commands.put("hostname", new HostnameCommand()); commands.put("whoami", new WhoamiCommand()); commands.put("touch", new TouchCommand()); commands.put("time", new TimeCommand()); diff --git a/src/main/java/com/mycmd/commands/HostnameCommand.java b/src/main/java/com/mycmd/commands/HostnameCommand.java new file mode 100644 index 0000000..f8078f9 --- /dev/null +++ b/src/main/java/com/mycmd/commands/HostnameCommand.java @@ -0,0 +1,21 @@ +package com.mycmd.commands; + +import java.net.InetAddress; + +import com.mycmd.Command; +import com.mycmd.ShellContext; + +public class HostnameCommand implements Command { + @Override + public void execute(String[] args, ShellContext context) { + String hostname = System.getenv("COMPUTERNAME"); + if (hostname == null) { + try { + hostname = InetAddress.getLocalHost().getHostName(); + } catch (Exception e) { + hostname = "Unknown Host"; + } + } + System.out.println(hostname); + } +}