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;
022import joptsimple.OptionSpec;
023
024import org.springframework.boot.cli.command.Command;
025import org.springframework.boot.cli.command.OptionParsingCommand;
026import org.springframework.boot.cli.command.options.CompilerOptionHandler;
027import org.springframework.boot.cli.command.status.ExitStatus;
028import org.springframework.boot.cli.util.Log;
029
030/**
031 * {@link Command} to uninstall dependencies from the CLI's lib/ext directory.
032 *
033 * @author Dave Syer
034 * @author Andy Wilkinson
035 * @since 1.2.0
036 */
037public class UninstallCommand extends OptionParsingCommand {
038
039        public UninstallCommand() {
040                super("uninstall", "Uninstall dependencies from the lib/ext directory",
041                                new UninstallOptionHandler());
042        }
043
044        @Override
045        public String getUsageHelp() {
046                return "[options] <coordinates>";
047        }
048
049        private static class UninstallOptionHandler extends CompilerOptionHandler {
050
051                private OptionSpec<Void> allOption;
052
053                @Override
054                protected void doOptions() {
055                        this.allOption = option("all", "Uninstall all");
056                }
057
058                @Override
059                @SuppressWarnings("unchecked")
060                protected ExitStatus run(OptionSet options) throws Exception {
061                        List<String> args = (List<String>) options.nonOptionArguments();
062                        try {
063                                if (options.has(this.allOption)) {
064                                        if (!args.isEmpty()) {
065                                                throw new IllegalArgumentException(
066                                                                "Please use --all without specifying any dependencies");
067                                        }
068                                        new Installer(options, this).uninstallAll();
069                                }
070                                if (args.isEmpty()) {
071                                        throw new IllegalArgumentException(
072                                                        "Please specify at least one dependency, in the form group:artifact:version, to uninstall");
073                                }
074                                new Installer(options, this).uninstall(args);
075                        }
076                        catch (Exception ex) {
077                                String message = ex.getMessage();
078                                Log.error((message != null) ? message : ex.getClass().toString());
079                        }
080                        return ExitStatus.OK;
081                }
082
083        }
084
085}