001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2020-2024 Andres Almiray.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.kordamp.maven.checker.cli;
019
020import org.apache.maven.project.MavenProject;
021import org.kordamp.maven.checker.BomChecker;
022import org.kordamp.maven.checker.PomCheckException;
023import org.kordamp.maven.checker.cli.internal.PomParser;
024import picocli.CommandLine;
025
026/**
027 * @author Andres Almiray
028 * @since 1.1.0
029 */
030@CommandLine.Command(name = "check-bom",
031    description = "Checks if a POM file is a minimal BOM file")
032public class CheckBom extends AbstractCommand<Main> {
033    @CommandLine.Option(names = {"--fail-on-error"},
034        negatable = true,
035        defaultValue = "true", fallbackValue = "true",
036        description = "Fails the build on error.")
037    boolean failOnError;
038
039    @Override
040    protected void execute() {
041        try {
042            logger.info("BOM checks: {}", pomFile.toAbsolutePath().toString());
043            MavenProject project = PomParser.createMavenProject(pomFile.toFile());
044            BomChecker.check(logger, project, new BomChecker.Configuration()
045                .withFailOnError(failOnError));
046        } catch (PomCheckException e) {
047            throw new HaltExecutionException(e);
048        }
049    }
050}