Skip to content

Commit

Permalink
ScannerCommand: Move out scanner configuration to a separate function
Browse files Browse the repository at this point in the history
For a better overview as the runCommand() function already became quite
large.

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch-si.com>
  • Loading branch information
sschuberth committed Jul 11, 2019
1 parent 667b981 commit 646b223
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions cli/src/main/kotlin/commands/ScannerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,34 +121,7 @@ object ScannerCommand : CommandWithHelp() {
)
private var outputFormats = listOf(OutputFormat.YAML)

override fun runCommand(jc: JCommander): Int {
require((ortFile == null) != (inputPath == null)) {
"Either '--ort-file' or '--input-path' must be specified."
}

val absoluteOutputDir = outputDir.expandTilde().normalize()
val absoluteNativeOutputDir = absoluteOutputDir.resolve("native-scan-results")

val outputFiles = outputFormats.distinct().map { format ->
File(absoluteOutputDir, "scan-result.${format.fileExtension}")
}

val existingOutputFiles = outputFiles.filter { it.exists() }
if (existingOutputFiles.isNotEmpty()) {
log.error { "None of the output files $existingOutputFiles must exist yet." }
return 2
}

if (absoluteNativeOutputDir.exists() && absoluteNativeOutputDir.list().isNotEmpty()) {
log.error { "The directory '$absoluteNativeOutputDir' must not contain any files yet." }
return 2
}

val absoluteDownloadDir = downloadDir?.expandTilde()
require(absoluteDownloadDir?.exists() != true) {
"The download directory '$absoluteDownloadDir' must not exist yet."
}

private fun configureScanner(configFile: File?): Scanner {
val config = configFile?.expandTilde()?.let {
require(it.isFile) {
"The provided configuration file '$it' is not actually a file."
Expand Down Expand Up @@ -200,6 +173,38 @@ object ScannerCommand : CommandWithHelp() {
localFileStorageLogFunction("Local file storage has $fileCount scan results files.")
}

return scanner
}

override fun runCommand(jc: JCommander): Int {
require((ortFile == null) != (inputPath == null)) {
"Either '--ort-file' or '--input-path' must be specified."
}

val absoluteOutputDir = outputDir.expandTilde().normalize()
val absoluteNativeOutputDir = absoluteOutputDir.resolve("native-scan-results")

val outputFiles = outputFormats.distinct().map { format ->
File(absoluteOutputDir, "scan-result.${format.fileExtension}")
}

val existingOutputFiles = outputFiles.filter { it.exists() }
if (existingOutputFiles.isNotEmpty()) {
log.error { "None of the output files $existingOutputFiles must exist yet." }
return 2
}

if (absoluteNativeOutputDir.exists() && absoluteNativeOutputDir.list().isNotEmpty()) {
log.error { "The directory '$absoluteNativeOutputDir' must not contain any files yet." }
return 2
}

val absoluteDownloadDir = downloadDir?.expandTilde()
require(absoluteDownloadDir?.exists() != true) {
"The download directory '$absoluteDownloadDir' must not exist yet."
}

val scanner = configureScanner(configFile)

val ortResult = ortFile?.expandTilde()?.let {
scanner.scanOrtResult(
Expand Down

0 comments on commit 646b223

Please sign in to comment.