001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.cli.command.core;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.springframework.boot.cli.command.AbstractCommand;
024import org.springframework.boot.cli.command.Command;
025import org.springframework.boot.cli.command.CommandRunner;
026import org.springframework.boot.cli.command.options.OptionHelp;
027import org.springframework.boot.cli.command.status.ExitStatus;
028import org.springframework.boot.cli.util.Log;
029
030/**
031 * Internal {@link Command} to provide hints for shell auto-completion. Expects to be
032 * called with the current index followed by a list of arguments already typed.
033 *
034 * @author Phillip Webb
035 */
036public class HintCommand extends AbstractCommand {
037
038        private final CommandRunner commandRunner;
039
040        public HintCommand(CommandRunner commandRunner) {
041                super("hint", "Provides hints for shell auto-completion");
042                this.commandRunner = commandRunner;
043        }
044
045        @Override
046        public ExitStatus run(String... args) throws Exception {
047                try {
048                        int index = (args.length == 0 ? 0 : Integer.valueOf(args[0]) - 1);
049                        List<String> arguments = new ArrayList<String>(args.length);
050                        for (int i = 2; i < args.length; i++) {
051                                arguments.add(args[i]);
052                        }
053                        String starting = "";
054                        if (index < arguments.size()) {
055                                starting = arguments.remove(index);
056                        }
057                        if (index == 0) {
058                                showCommandHints(starting);
059                        }
060                        else if (!arguments.isEmpty() && (starting.length() > 0)) {
061                                String command = arguments.remove(0);
062                                showCommandOptionHints(command, Collections.unmodifiableList(arguments),
063                                                starting);
064                        }
065                }
066                catch (Exception ex) {
067                        // Swallow and provide no hints
068                        return ExitStatus.ERROR;
069                }
070                return ExitStatus.OK;
071        }
072
073        private void showCommandHints(String starting) {
074                for (Command command : this.commandRunner) {
075                        if (isHintMatch(command, starting)) {
076                                Log.info(command.getName() + " " + command.getDescription());
077                        }
078                }
079        }
080
081        private boolean isHintMatch(Command command, String starting) {
082                if (command instanceof HintCommand) {
083                        return false;
084                }
085                return command.getName().startsWith(starting)
086                                || (this.commandRunner.isOptionCommand(command)
087                                                && ("--" + command.getName()).startsWith(starting));
088        }
089
090        private void showCommandOptionHints(String commandName,
091                        List<String> specifiedArguments, String starting) {
092                Command command = this.commandRunner.findCommand(commandName);
093                if (command != null) {
094                        for (OptionHelp help : command.getOptionsHelp()) {
095                                if (!alreadyUsed(help, specifiedArguments)) {
096                                        for (String option : help.getOptions()) {
097                                                if (option.startsWith(starting)) {
098                                                        Log.info(option + " " + help.getUsageHelp());
099                                                }
100                                        }
101                                }
102                        }
103                }
104        }
105
106        private boolean alreadyUsed(OptionHelp help, List<String> specifiedArguments) {
107                for (String argument : specifiedArguments) {
108                        if (help.getOptions().contains(argument)) {
109                                return true;
110                        }
111                }
112                return false;
113        }
114
115}