Search in sources :

Example 1 with DetectOption

use of com.blackducksoftware.integration.hub.detect.help.DetectOption in project hub-detect by blackducksoftware.

the class Application method init.

@PostConstruct
public void init() {
    final long start = System.currentTimeMillis();
    try {
        detectInfo.init();
        detectOptionManager.init();
        final List<DetectOption> options = detectOptionManager.getDetectOptions();
        boolean isPrintHelp = false;
        boolean isPrintHelpDoc = false;
        boolean isInteractive = false;
        for (final String arg : applicationArguments.getSourceArgs()) {
            if (arg.equals("-h") || arg.equals("--help")) {
                isPrintHelp = true;
            } else if (arg.equals("-hdoc") || arg.equals("--helpdocument")) {
                isPrintHelpDoc = true;
            } else if (arg.equals("-i") || arg.equals("--interactive")) {
                isInteractive = true;
            }
        }
        if (isPrintHelp) {
            helpPrinter.printHelpMessage(System.out, options);
            return;
        }
        if (isPrintHelpDoc) {
            helpHtmlWriter.writeHelpMessage(String.format("hub-detect-%s-help.html", detectInfo.getDetectVersion()));
            return;
        }
        if (isInteractive) {
            final InteractiveReader interactiveReader = createInteractiveReader();
            final PrintStream interactivePrintStream = new PrintStream(System.out);
            interactiveManager.interact(interactiveReader, interactivePrintStream);
        }
        detectConfiguration.init();
        logger.info("Configuration processed completely.");
        if (!detectConfiguration.getSuppressConfigurationOutput()) {
            final DetectInfoPrinter infoPrinter = new DetectInfoPrinter();
            final DetectConfigurationPrinter detectConfigurationPrinter = new DetectConfigurationPrinter();
            infoPrinter.printInfo(System.out, detectInfo);
            detectConfigurationPrinter.print(System.out, detectInfo, detectConfiguration, options);
        }
        if (detectConfiguration.getTestConnection()) {
            hubServiceWrapper.assertHubConnection(new SilentLogger());
            return;
        }
        if (!detectConfiguration.getHubOfflineMode()) {
            hubServiceWrapper.init();
        }
        final DetectProject detectProject = detectProjectManager.createDetectProject();
        final List<File> createdBdioFiles = detectProjectManager.createBdioFiles(detectProject);
        if (!detectConfiguration.getHubOfflineMode()) {
            final ProjectVersionView projectVersionView = hubManager.updateHubProjectVersion(detectProject, createdBdioFiles);
            hubManager.performPostHubActions(detectProject, projectVersionView);
        } else if (!detectConfiguration.getHubSignatureScannerDisabled()) {
            hubSignatureScanner.scanPathsOffline(detectProject);
        }
        for (final ExitCodeReporter exitCodeReporter : exitCodeReporters) {
            exitCodeType = ExitCodeType.getWinningExitCodeType(exitCodeType, exitCodeReporter.getExitCodeType());
        }
    } catch (final Exception e) {
        populateExitCodeFromExceptionDetails(e);
    } finally {
        try {
            detectPhoneHomeManager.endPhoneHome();
        } catch (final Exception e) {
            logger.debug(String.format("Error trying to end the phone home task: %s", e.getMessage()));
        }
        if (!detectConfiguration.getSuppressResultsOutput()) {
            detectSummary.logResults(new Slf4jIntLogger(logger), exitCodeType);
        }
        detectFileManager.cleanupDirectories();
    }
    final long end = System.currentTimeMillis();
    logger.info(String.format("Hub-Detect run duration: %s", DurationFormatUtils.formatPeriod(start, end, "HH'h' mm'm' ss's' SSS'ms'")));
    if (detectConfiguration.getForceSuccess() && exitCodeType.getExitCode() != 0) {
        logger.warn("Forcing success: Exiting with 0. Desired exit code was ${exitCodeType.getExitCode()}.");
        System.exit(0);
    } else {
        System.exit(exitCodeType.getExitCode());
    }
}
Also used : PrintStream(java.io.PrintStream) InteractiveReader(com.blackducksoftware.integration.hub.detect.interactive.reader.InteractiveReader) ScannerInteractiveReader(com.blackducksoftware.integration.hub.detect.interactive.reader.ScannerInteractiveReader) ConsoleInteractiveReader(com.blackducksoftware.integration.hub.detect.interactive.reader.ConsoleInteractiveReader) DetectInfoPrinter(com.blackducksoftware.integration.hub.detect.help.print.DetectInfoPrinter) DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) DetectProject(com.blackducksoftware.integration.hub.detect.model.DetectProject) ExitCodeReporter(com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeReporter) DetectConfigurationPrinter(com.blackducksoftware.integration.hub.detect.help.print.DetectConfigurationPrinter) Slf4jIntLogger(com.blackducksoftware.integration.log.Slf4jIntLogger) SilentLogger(com.blackducksoftware.integration.log.SilentLogger) ProjectVersionView(com.blackducksoftware.integration.hub.api.generated.view.ProjectVersionView) File(java.io.File) PostConstruct(javax.annotation.PostConstruct)

Example 2 with DetectOption

use of com.blackducksoftware.integration.hub.detect.help.DetectOption in project hub-detect by blackducksoftware.

the class HelpPrinter method printHelpMessage.

public void printHelpMessage(final PrintStream printStream, final List<DetectOption> options) {
    final List<String> helpMessagePieces = new ArrayList<>();
    helpMessagePieces.add("");
    final List<String> headerColumns = Arrays.asList("Property Name", "Default", "Description");
    final String headerText = formatColumns(headerColumns, 51, 30, 95);
    helpMessagePieces.add(headerText);
    helpMessagePieces.add(StringUtils.repeat('_', 175));
    String group = null;
    for (final DetectOption detectValue : options) {
        final String currentGroup = detectValue.getGroup();
        if (group == null) {
            group = currentGroup;
        } else if (!group.equals(currentGroup)) {
            helpMessagePieces.add("");
            group = currentGroup;
        }
        final List<String> bodyColumns = Arrays.asList("--" + detectValue.getKey(), detectValue.getDefaultValue(), detectValue.getDescription());
        final String bodyText = formatColumns(bodyColumns, 51, 30, 95);
        helpMessagePieces.add(bodyText);
    }
    helpMessagePieces.add("");
    helpMessagePieces.add("Usage : ");
    helpMessagePieces.add("\t--<property name>=<value>");
    helpMessagePieces.add("");
    printMessage(printStream, helpMessagePieces);
}
Also used : DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) ArrayList(java.util.ArrayList)

Example 3 with DetectOption

use of com.blackducksoftware.integration.hub.detect.help.DetectOption in project hub-detect by blackducksoftware.

the class HelpHtmlWriter method writeHelpMessage.

public void writeHelpMessage(final String filename) {
    final List<GroupOptionListing> groupOptions = new ArrayList<>();
    for (final String groupName : detectOptionManager.getDetectGroups()) {
        final List<DetectOption> filteredOptions = getGroupDetectOptions(groupName);
        groupOptions.add(new GroupOptionListing(StringUtils.capitalise(groupName), filteredOptions));
    }
    final Map<String, Object> dataModel = new HashMap<>();
    dataModel.put("options", groupOptions);
    try {
        final File htmlHelpFile = new File(filename);
        final Template htmlTemplate = configuration.getTemplate("templates/helpHtml.ftl");
        htmlTemplate.process(dataModel, new FileWriter(htmlHelpFile));
        logger.info(filename + " was created in your current directory.");
    } catch (final IOException | TemplateException e) {
        logger.error("There was an error when creating the html file", e);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Template(freemarker.template.Template) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) File(java.io.File)

Example 4 with DetectOption

use of com.blackducksoftware.integration.hub.detect.help.DetectOption in project hub-detect by blackducksoftware.

the class DetectConfigurationPrinter method print.

public void print(final PrintStream printStream, final DetectInfo detectInfo, final DetectConfiguration detectConfiguration, final List<DetectOption> detectOptions) throws IllegalArgumentException, IllegalAccessException {
    printStream.println("");
    printStream.println("Current property values:");
    printStream.println("--property = value [notes]");
    printStream.println(StringUtils.repeat("-", 60));
    List<Field> annotatedProperties = new ArrayList<>();
    final Field[] propertyFields = DetectConfiguration.class.getDeclaredFields();
    for (final Field propertyField : propertyFields) {
        final Optional<Annotation> foundField = Arrays.stream(propertyField.getAnnotations()).filter(annotation -> annotation.annotationType() == Value.class).findFirst();
        final int modifiers = propertyField.getModifiers();
        if (foundField.isPresent() && !Modifier.isStatic(modifiers) && Modifier.isPrivate(modifiers)) {
            annotatedProperties.add(propertyField);
        }
    }
    annotatedProperties = annotatedProperties.stream().sorted(new Comparator<Field>() {

        @Override
        public int compare(final Field field1, final Field field2) {
            return field1.getName().compareTo(field2.getName());
        }
    }).collect(Collectors.toList());
    for (final Field field : annotatedProperties) {
        field.setAccessible(true);
        final String fieldName = field.getName();
        Object rawFieldValue;
        rawFieldValue = field.get(detectConfiguration);
        String fieldValue = "";
        if (field.getType().isArray()) {
            fieldValue = String.join(", ", (String[]) rawFieldValue);
        } else {
            if (rawFieldValue != null) {
                fieldValue = rawFieldValue.toString();
            }
        }
        if (!StringUtils.isEmpty(fieldName) && !StringUtils.isEmpty(fieldValue) && "metaClass" != fieldName) {
            final boolean containsPassword = fieldName.toLowerCase().contains("password") || fieldName.toLowerCase().contains("apitoken");
            if (containsPassword) {
                fieldValue = StringUtils.repeat("*", fieldValue.length());
            }
            DetectOption option = null;
            for (final DetectOption opt : detectOptions) {
                if (opt.getFieldName().equals(fieldName)) {
                    option = opt;
                }
            }
            if (option != null && !option.getResolvedValue().equals(fieldValue) && !containsPassword) {
                if (option.interactiveValue != null) {
                    printStream.println(fieldName + " = " + fieldValue + " [interactive]");
                } else if (option.getResolvedValue().equals("latest")) {
                    printStream.println(fieldName + " = " + fieldValue + " [latest]");
                } else if (option.getResolvedValue().trim().length() == 0) {
                    printStream.println(fieldName + " = " + fieldValue + " [calculated]");
                } else {
                    printStream.println(fieldName + " = " + fieldValue + " + [" + option.getResolvedValue() + "]");
                }
            } else {
                printStream.println(fieldName + " = " + fieldValue);
            }
        }
        field.setAccessible(false);
    }
    printStream.println(StringUtils.repeat("-", 60));
    printStream.println("");
}
Also used : PrintStream(java.io.PrintStream) Arrays(java.util.Arrays) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Comparator(java.util.Comparator) DetectConfiguration(com.blackducksoftware.integration.hub.detect.DetectConfiguration) DetectInfo(com.blackducksoftware.integration.hub.detect.DetectInfo) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) Field(java.lang.reflect.Field)

Aggregations

DetectOption (com.blackducksoftware.integration.hub.detect.help.DetectOption)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 PrintStream (java.io.PrintStream)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1 ProjectVersionView (com.blackducksoftware.integration.hub.api.generated.view.ProjectVersionView)1 DetectConfiguration (com.blackducksoftware.integration.hub.detect.DetectConfiguration)1 DetectInfo (com.blackducksoftware.integration.hub.detect.DetectInfo)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 ExitCodeReporter (com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeReporter)1 DetectConfigurationPrinter (com.blackducksoftware.integration.hub.detect.help.print.DetectConfigurationPrinter)1 DetectInfoPrinter (com.blackducksoftware.integration.hub.detect.help.print.DetectInfoPrinter)1 ConsoleInteractiveReader (com.blackducksoftware.integration.hub.detect.interactive.reader.ConsoleInteractiveReader)1 InteractiveReader (com.blackducksoftware.integration.hub.detect.interactive.reader.InteractiveReader)1 ScannerInteractiveReader (com.blackducksoftware.integration.hub.detect.interactive.reader.ScannerInteractiveReader)1 DetectProject (com.blackducksoftware.integration.hub.detect.model.DetectProject)1 SilentLogger (com.blackducksoftware.integration.log.SilentLogger)1 Slf4jIntLogger (com.blackducksoftware.integration.log.Slf4jIntLogger)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1