core/src/main/java/org/owasp/dependencycheck/analyzer/GolangModAnalyzer.java
/**
     * Initialize the go mod analyzer; ensures that go is installed and can be
     * called.
     *
     * @param engine a reference to the dependency-check engine
     * @throws InitializationException never thrown
     */
Initialize the go mod analyzer; ensures that go is installed and can be
called.

@param engine a reference to the dependency-check engine
@throws InitializationException never thrown
Initialize the go mod analyzer ; ensures that go is installed and can be called . @param engine a reference to the dependency - check engine @throws InitializationException never thrown
@SuppressWarnings("fallthrough")
    @SuppressFBWarnings(justification = "The fallthrough is intentional to avoid code duplication", value = {"SF_SWITCH_NO_DEFAULT"})
    @Override
    protected void prepareFileTypeAnalyzer(Engine engine) throws InitializationException {
        setEnabled(false);
        final Process process;
        try {
            process = testGoMod(getSettings().getTempDirectory());
        } catch (AnalysisException ae) {
            final String msg = String.format("Exception from go process: %s. Disabling %s", ae.getCause(), ANALYZER_NAME);
            throw new InitializationException(msg, ae);
        } catch (IOException ex) {
            throw new InitializationException("Unable to create temporary file, the Go Mod Analyzer will be disabled", ex);
        }
        final int exitValue;
        try {
            exitValue = process.waitFor();
        } catch (InterruptedException ex) {
            final String msg = String.format("Go mod process was interrupted. Disabling %s", ANALYZER_NAME);
            Thread.currentThread().interrupt();
            throw new InitializationException(msg);
        }
        final int expectedNoModuleFoundExitValue = 1;
        final int possiblyGoTooOldExitValue = 2;
        final int goExecutableNotFoundExitValue = 127;
        switch (exitValue) {
            case expectedNoModuleFoundExitValue:
                setEnabled(true);
                LOGGER.debug("{} is enabled.", ANALYZER_NAME);
                return;
            case goExecutableNotFoundExitValue:
                throw new InitializationException(String.format("Go executable not found. Disabling %s: %s", ANALYZER_NAME, exitValue));
            case possiblyGoTooOldExitValue:
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) {
                    if (!reader.ready()) {
                        LOGGER.warn("Go mod error stream unexpectedly not ready. Disabling {}", ANALYZER_NAME);
                        throw new InitializationException("Go mod error stream unexpectedly not ready.");
                    } else {
                        final String line = reader.readLine();
                        if (line == null) {
                            LOGGER.warn("An error occurred calling `go` - no output could be read. Disabling {}.", ANALYZER_NAME);
                            throw new InitializationException("Error calling `go` - no output could be read.");
                        }
                        if (line.contains("unknown subcommand \"mod\"")) {
                            LOGGER.warn("Your version of `go` does not support modules. Disabling {}. Error: `{}`", ANALYZER_NAME, line);
                            throw new InitializationException("Go version does not support modules.");
                        }
                    }
                } catch (UnsupportedEncodingException ex) {
                    throw new InitializationException("Unexpected go encoding.", ex);
                } catch (IOException ex) {
                    throw new InitializationException("Unable to read go output.", ex);
                }
            default:
                final String msg = String.format("Unexpected exit code from go process. Disabling %s: %s", ANALYZER_NAME, exitValue);
                throw new InitializationException(msg);
        }
    }
@ SuppressWarnings ( "fallthrough" ) @ SuppressFBWarnings ( justification = "The fallthrough is intentional to avoid code duplication" , value = { "SF_SWITCH_NO_DEFAULT" } ) @ Override protected void prepareFileTypeAnalyzer ( Engine engine ) throws InitializationException { setEnabled ( false ) ; final Process process ; try { process = testGoMod ( getSettings ( ) . getTempDirectory ( ) ) ; } catch ( AnalysisException ae ) { final String msg = String . format ( "Exception from go process: %s. Disabling %s" , ae . getCause ( ) , ANALYZER_NAME ) ; throw new InitializationException ( msg , ae ) ; } catch ( IOException ex ) { throw new InitializationException ( "Unable to create temporary file, the Go Mod Analyzer will be disabled" , ex ) ; } final int exitValue ; try { exitValue = process . waitFor ( ) ; } catch ( InterruptedException ex ) { final String msg = String . format ( "Go mod process was interrupted. Disabling %s" , ANALYZER_NAME ) ; Thread . currentThread ( ) . interrupt ( ) ; throw new InitializationException ( msg ) ; } final int expectedNoModuleFoundExitValue = 1 ; final int possiblyGoTooOldExitValue = 2 ; final int goExecutableNotFoundExitValue = 127 ; switch ( exitValue ) { case expectedNoModuleFoundExitValue : setEnabled ( true ) ; LOGGER . debug ( "{} is enabled." , ANALYZER_NAME ) ; return ; case goExecutableNotFoundExitValue : throw new InitializationException ( String . format ( "Go executable not found. Disabling %s: %s" , ANALYZER_NAME , exitValue ) ) ; case possiblyGoTooOldExitValue : try ( BufferedReader reader = new BufferedReader ( new InputStreamReader ( process . getErrorStream ( ) , StandardCharsets . UTF_8 ) ) ) { if ( ! reader . ready ( ) ) { LOGGER . warn ( "Go mod error stream unexpectedly not ready. Disabling {}" , ANALYZER_NAME ) ; throw new InitializationException ( "Go mod error stream unexpectedly not ready." ) ; } else { final String line = reader . readLine ( ) ; if ( line == null ) { LOGGER . warn ( "An error occurred calling `go` - no output could be read. Disabling {}." , ANALYZER_NAME ) ; throw new InitializationException ( "Error calling `go` - no output could be read." ) ; } if ( line . contains ( "unknown subcommand \"mod\"" ) ) { LOGGER . warn ( "Your version of `go` does not support modules. Disabling {}. Error: `{}`" , ANALYZER_NAME , line ) ; throw new InitializationException ( "Go version does not support modules." ) ; } } } catch ( UnsupportedEncodingException ex ) { throw new InitializationException ( "Unexpected go encoding." , ex ) ; } catch ( IOException ex ) { throw new InitializationException ( "Unable to read go output." , ex ) ; } default : final String msg = String . format ( "Unexpected exit code from go process. Disabling %s: %s" , ANALYZER_NAME , exitValue ) ; throw new InitializationException ( msg ) ; } }
Initialize the go mod analyzer; ensures that go is installed and can be
 called.
Initialize the go mod analyzer ; ensures that go is installed and can be called .
// fall through