001/*
002 * Copyright 2012-2018 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.install;
018
019import java.util.List;
020
021import joptsimple.OptionSet;
022
023import org.springframework.boot.cli.command.Command;
024import org.springframework.boot.cli.command.OptionParsingCommand;
025import org.springframework.boot.cli.command.options.CompilerOptionHandler;
026import org.springframework.boot.cli.command.status.ExitStatus;
027import org.springframework.boot.cli.util.Log;
028import org.springframework.util.Assert;
029
030/**
031 * {@link Command} to install additional dependencies into the CLI.
032 *
033 * @author Dave Syer
034 * @author Andy Wilkinson
035 * @since 1.2.0
036 */
037public class InstallCommand extends OptionParsingCommand {
038
039        public InstallCommand() {
040                super("install", "Install dependencies to the lib/ext directory",
041                                new InstallOptionHandler());
042        }
043
044        @Override
045        public String getUsageHelp() {
046                return "[options] <coordinates>";
047        }
048
049        private static final class InstallOptionHandler extends CompilerOptionHandler {
050
051                @Override
052                @SuppressWarnings("unchecked")
053                protected ExitStatus run(OptionSet options) throws Exception {
054                        List<String> args = (List<String>) options.nonOptionArguments();
055                        Assert.notEmpty(args, "Please specify at least one "
056                                        + "dependency, in the form group:artifact:version, to install");
057                        try {
058                                new Installer(options, this).install(args);
059                        }
060                        catch (Exception ex) {
061                                String message = ex.getMessage();
062                                Log.error((message != null) ? message : ex.getClass().toString());
063                        }
064                        return ExitStatus.OK;
065                }
066
067        }
068
069}