From 7fe4c65820d012d684405c56942b01da8e77bd23 Mon Sep 17 00:00:00 2001 From: Anatoliy Sablin Date: Mon, 6 Nov 2017 16:34:39 +0300 Subject: Added help command. --- .../src/main/java/com/juick/command/ShowHelp.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 juick-commands/src/main/java/com/juick/command/ShowHelp.java (limited to 'juick-commands/src/main/java/com/juick/command/ShowHelp.java') diff --git a/juick-commands/src/main/java/com/juick/command/ShowHelp.java b/juick-commands/src/main/java/com/juick/command/ShowHelp.java new file mode 100644 index 00000000..78e01706 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowHelp.java @@ -0,0 +1,52 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import lombok.Getter; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import javax.annotation.PostConstruct; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ShowHelp implements Command, ApplicationContextAware { + + private static final Pattern PATTERN = Pattern.compile("^help$", Pattern.CASE_INSENSITIVE); + + private ApplicationContext applicationContext; + private String help; + + @PostConstruct + public void init() { + help = getApplicationContext().getBeansOfType(Command.class).values().stream().map(Command::help).sorted() + .collect(Collectors.joining("\n")); + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "HELP - returns this help message."; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + return getHelp(); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} -- cgit v1.2.3