aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/ShowHelp.java
blob: deea9c75e35d3d1d280c970236ede26021d31ccf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.juick.command;

import com.juick.User;
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, MessageListener protocolListener, String command) {
        return getHelp();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}