Skip to content

Commit 44661f8

Browse files
committed
WIP
1 parent 5788347 commit 44661f8

22 files changed

+199
-112
lines changed

cli/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ dependencies {
151151
implementation(libs.clikt)
152152
implementation(libs.hikari)
153153
implementation(libs.jacksonModuleKotlin)
154+
implementation(libs.koinCore)
154155
implementation(libs.kotlinxCoroutines)
155156
implementation(libs.kotlinxSerialization)
156157
implementation(libs.log4jApiToSlf4j)

cli/src/main/kotlin/OrtMain.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ import java.io.File
4141

4242
import kotlin.system.exitProcess
4343

44+
import org.koin.core.context.GlobalContext.startKoin
45+
import org.koin.dsl.module
46+
4447
import org.ossreviewtoolkit.cli.commands.*
4548
import org.ossreviewtoolkit.cli.utils.logger
46-
import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns
4749
import org.ossreviewtoolkit.model.config.OrtConfiguration
4850
import org.ossreviewtoolkit.utils.common.Os
4951
import org.ossreviewtoolkit.utils.common.expandTilde
@@ -70,7 +72,6 @@ sealed interface GroupTypes {
7072
* Helper class for collecting options that can be passed to subcommands.
7173
*/
7274
data class GlobalOptions(
73-
val config: OrtConfiguration,
7475
val forceOverwrite: Boolean
7576
)
7677

@@ -179,13 +180,21 @@ class OrtMain : CliktCommand(name = ORT_NAME, invokeWithoutSubcommand = true) {
179180

180181
logger.debug { "Used command line arguments: ${currentContext.originalArgv}" }
181182

183+
val ortConfigModule = module {
184+
single {
185+
OrtConfiguration.load(configArguments, configFile)
186+
}
187+
}
188+
189+
startKoin {
190+
modules(ortConfigModule)
191+
}
192+
182193
// Make the parameter globally available.
183194
printStackTrace = stacktrace
184195

185196
// Make options available to subcommands and apply static configuration.
186-
val ortConfiguration = OrtConfiguration.load(configArguments, configFile)
187-
currentContext.findOrSetObject { GlobalOptions(ortConfiguration, forceOverwrite) }
188-
LicenseFilenamePatterns.configure(ortConfiguration.licenseFilePatterns)
197+
currentContext.findOrSetObject { GlobalOptions(forceOverwrite) }
189198

190199
if (helpAll) {
191200
registeredSubcommands().forEach {

cli/src/main/kotlin/commands/AdvisorCommand.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import com.github.ajalt.clikt.parameters.options.split
3434
import com.github.ajalt.clikt.parameters.types.enum
3535
import com.github.ajalt.clikt.parameters.types.file
3636

37+
import org.koin.core.component.KoinComponent
38+
import org.koin.core.component.inject
39+
3740
import org.ossreviewtoolkit.advisor.Advisor
3841
import org.ossreviewtoolkit.cli.GlobalOptions
3942
import org.ossreviewtoolkit.cli.utils.SeverityStats
@@ -42,14 +45,18 @@ import org.ossreviewtoolkit.cli.utils.outputGroup
4245
import org.ossreviewtoolkit.cli.utils.readOrtResult
4346
import org.ossreviewtoolkit.cli.utils.writeOrtResult
4447
import org.ossreviewtoolkit.model.FileFormat
48+
import org.ossreviewtoolkit.model.config.OrtConfiguration
4549
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
4650
import org.ossreviewtoolkit.model.utils.mergeLabels
4751
import org.ossreviewtoolkit.utils.common.expandTilde
4852
import org.ossreviewtoolkit.utils.common.safeMkdirs
4953
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
5054
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
5155

52-
class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies for security vulnerabilities.") {
56+
class AdvisorCommand : KoinComponent, CliktCommand(
57+
name = "advise",
58+
help = "Check dependencies for security vulnerabilities."
59+
) {
5360
private val ortFile by option(
5461
"--ort-file", "-i",
5562
help = "An ORT result file with an analyzer result to use."
@@ -100,6 +107,7 @@ class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies
100107
).flag()
101108

102109
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
110+
private val ortConfig by inject<OrtConfiguration>()
103111

104112
override fun run() {
105113
val outputFiles = outputFormats.mapTo(mutableSetOf()) { format ->
@@ -117,8 +125,7 @@ class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies
117125
println("The following advisors are activated:")
118126
println("\t" + distinctProviders.joinToString())
119127

120-
val config = globalOptionsForSubcommands.config
121-
val advisor = Advisor(distinctProviders, config.advisor)
128+
val advisor = Advisor(distinctProviders, ortConfig.advisor)
122129

123130
val ortResultInput = readOrtResult(ortFile)
124131
val ortResultOutput = advisor.retrieveFindings(ortResultInput, skipExcluded).mergeLabels(labels)
@@ -138,6 +145,6 @@ class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies
138145
advisorResults.collectIssues().flatMap { it.value }.partition { resolutionProvider.isResolved(it) }
139146
val severityStats = SeverityStats.createFromIssues(resolvedIssues, unresolvedIssues)
140147

141-
severityStats.print().conclude(config.severeIssueThreshold, 2)
148+
severityStats.print().conclude(ortConfig.severeIssueThreshold, 2)
142149
}
143150
}

cli/src/main/kotlin/commands/AnalyzerCommand.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import com.github.ajalt.clikt.parameters.options.split
3535
import com.github.ajalt.clikt.parameters.types.enum
3636
import com.github.ajalt.clikt.parameters.types.file
3737

38+
import org.koin.core.component.KoinComponent
39+
import org.koin.core.component.inject
40+
3841
import org.ossreviewtoolkit.analyzer.Analyzer
3942
import org.ossreviewtoolkit.analyzer.PackageManager
4043
import org.ossreviewtoolkit.analyzer.PackageManagerFactory
@@ -54,6 +57,7 @@ import org.ossreviewtoolkit.cli.utils.outputGroup
5457
import org.ossreviewtoolkit.cli.utils.writeOrtResult
5558
import org.ossreviewtoolkit.model.FileFormat
5659
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
60+
import org.ossreviewtoolkit.model.config.OrtConfiguration
5761
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
5862
import org.ossreviewtoolkit.model.readValueOrNull
5963
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
@@ -66,7 +70,10 @@ import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
6670
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
6771
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
6872

69-
class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine dependencies of a software project.") {
73+
class AnalyzerCommand : KoinComponent, CliktCommand(
74+
name = "analyze",
75+
help = "Determine dependencies of a software project."
76+
) {
7077
private val inputDir by option(
7178
"--input-dir", "-i",
7279
help = "The project directory to analyze. As a special case, if only one package manager is enabled, this " +
@@ -177,6 +184,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
177184
)
178185

179186
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
187+
private val ortConfig by inject<OrtConfiguration>()
180188

181189
override fun run() {
182190
val outputFiles = outputFormats.mapTo(mutableSetOf()) { format ->
@@ -209,12 +217,10 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
209217
println("Looking for analyzer-specific configuration in the following files and directories:")
210218
println("\t" + configurationInfo)
211219

212-
val config = globalOptionsForSubcommands.config
213-
214220
val enabledPackageManagers = if (enabledPackageManagers != null || disabledPackageManagers != null) {
215221
(enabledPackageManagers ?: PackageManager.ALL.values).toSet() - disabledPackageManagers.orEmpty().toSet()
216222
} else {
217-
config.analyzer.determineEnabledPackageManagers()
223+
ortConfig.analyzer.determineEnabledPackageManagers()
218224
}
219225

220226
println("The following package managers are enabled:")
@@ -226,7 +232,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
226232
?: RepositoryConfiguration()
227233

228234
val analyzerConfiguration =
229-
repositoryConfiguration.analyzer?.let { config.analyzer.merge(it) } ?: config.analyzer
235+
repositoryConfiguration.analyzer?.let { ortConfig.analyzer.merge(it) } ?: ortConfig.analyzer
230236

231237
val analyzer = Analyzer(analyzerConfiguration, labels)
232238

@@ -237,7 +243,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
237243

238244
val repositoryPackageCurations = repositoryConfiguration.curations.packages
239245

240-
if (config.enableRepositoryPackageCurations) {
246+
if (ortConfig.enableRepositoryPackageCurations) {
241247
add(SimplePackageCurationProvider(repositoryPackageCurations))
242248
} else if (repositoryPackageCurations.isNotEmpty()) {
243249
logger.warn {
@@ -249,7 +255,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
249255

250256
val curationProviders = listOfNotNull(
251257
CompositePackageCurationProvider(defaultCurationProviders),
252-
config.analyzer.sw360Configuration?.let {
258+
ortConfig.analyzer.sw360Configuration?.let {
253259
Sw360PackageCurationProvider(it).takeIf { useSw360Curations }
254260
},
255261
ClearlyDefinedPackageCurationProvider().takeIf { useClearlyDefinedCurations }
@@ -301,7 +307,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
301307
analyzerResult.collectIssues().flatMap { it.value }.partition { resolutionProvider.isResolved(it) }
302308
val severityStats = SeverityStats.createFromIssues(resolvedIssues, unresolvedIssues)
303309

304-
severityStats.print().conclude(config.severeIssueThreshold, 2)
310+
severityStats.print().conclude(ortConfig.severeIssueThreshold, 2)
305311
}
306312
}
307313

cli/src/main/kotlin/commands/ConfigCommand.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
2323
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
2424

2525
import com.github.ajalt.clikt.core.CliktCommand
26-
import com.github.ajalt.clikt.core.requireObject
2726
import com.github.ajalt.clikt.parameters.options.flag
2827
import com.github.ajalt.clikt.parameters.options.option
2928

30-
import org.ossreviewtoolkit.cli.GlobalOptions
29+
import org.koin.core.component.KoinComponent
30+
import org.koin.core.component.inject
31+
3132
import org.ossreviewtoolkit.model.config.OrtConfiguration
3233
import org.ossreviewtoolkit.model.config.OrtConfigurationWrapper
3334
import org.ossreviewtoolkit.model.config.REFERENCE_CONFIG_FILENAME
3435

35-
class ConfigCommand : CliktCommand(name = "config", help = "Show different ORT configurations") {
36+
class ConfigCommand : KoinComponent, CliktCommand(
37+
name = "config",
38+
help = "Show different ORT configurations"
39+
) {
3640
private val showDefault by option(
3741
"--show-default",
3842
help = "Show the default configuration used when no custom configuration is present."
@@ -49,7 +53,7 @@ class ConfigCommand : CliktCommand(name = "config", help = "Show different ORT c
4953
"example entries for all supported configuration options."
5054
).flag()
5155

52-
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
56+
private val ortConfig by inject<OrtConfiguration>()
5357

5458
private val mapper = YAMLMapper().apply {
5559
registerKotlinModule()
@@ -68,7 +72,7 @@ class ConfigCommand : CliktCommand(name = "config", help = "Show different ORT c
6872
if (showActive) {
6973
println("The active configuration is:")
7074
println()
71-
println(globalOptionsForSubcommands.config.renderYaml())
75+
println(ortConfig.renderYaml())
7276
}
7377

7478
if (showReference) {

cli/src/main/kotlin/commands/DownloaderCommand.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package org.ossreviewtoolkit.cli.commands
2121

2222
import com.github.ajalt.clikt.core.CliktCommand
2323
import com.github.ajalt.clikt.core.ProgramResult
24-
import com.github.ajalt.clikt.core.requireObject
2524
import com.github.ajalt.clikt.parameters.groups.default
2625
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
2726
import com.github.ajalt.clikt.parameters.groups.required
@@ -37,7 +36,9 @@ import com.github.ajalt.clikt.parameters.types.file
3736

3837
import java.io.File
3938

40-
import org.ossreviewtoolkit.cli.GlobalOptions
39+
import org.koin.core.component.KoinComponent
40+
import org.koin.core.component.inject
41+
4142
import org.ossreviewtoolkit.cli.GroupTypes.FileType
4243
import org.ossreviewtoolkit.cli.GroupTypes.StringType
4344
import org.ossreviewtoolkit.cli.utils.OPTION_GROUP_INPUT
@@ -56,6 +57,7 @@ import org.ossreviewtoolkit.model.PackageType
5657
import org.ossreviewtoolkit.model.RemoteArtifact
5758
import org.ossreviewtoolkit.model.VcsInfo
5859
import org.ossreviewtoolkit.model.VcsType
60+
import org.ossreviewtoolkit.model.config.OrtConfiguration
5961
import org.ossreviewtoolkit.model.licenses.LicenseCategorization
6062
import org.ossreviewtoolkit.model.licenses.LicenseClassifications
6163
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
@@ -75,7 +77,10 @@ import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
7577
import org.ossreviewtoolkit.utils.ort.showStackTrace
7678
import org.ossreviewtoolkit.utils.spdx.model.SpdxLicenseChoice
7779

78-
class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source code from a remote location.") {
80+
class DownloaderCommand : KoinComponent, CliktCommand(
81+
name = "download",
82+
help = "Fetch source code from a remote location."
83+
) {
7984
private val input by mutuallyExclusiveOptions(
8085
option(
8186
"--ort-file", "-i",
@@ -176,7 +181,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
176181
"result to limit downloads to. If not specified, all packages are downloaded."
177182
).split(",")
178183

179-
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
184+
private val ortConfig by inject<OrtConfiguration>()
180185

181186
override fun run() {
182187
val failureMessages = mutableListOf<String>()
@@ -237,7 +242,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
237242
}
238243
}
239244

240-
val includedLicenseCategories = globalOptionsForSubcommands.config.downloader.includedLicenseCategories
245+
val includedLicenseCategories = ortConfig.downloader.includedLicenseCategories
241246
if (includedLicenseCategories.isNotEmpty() && licenseClassificationsFile.isFile) {
242247
val originalCount = packages.size
243248

@@ -268,7 +273,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
268273

269274
packageDownloadDirs.forEach { (pkg, dir) ->
270275
try {
271-
Downloader(globalOptionsForSubcommands.config.downloader).download(pkg, dir)
276+
Downloader(ortConfig.downloader).download(pkg, dir)
272277

273278
if (archiveMode == ArchiveMode.ENTITY) {
274279
val zipFile = outputDir.resolve("${pkg.id.toPath("-")}.zip")
@@ -369,7 +374,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
369374
// Always allow moving revisions when directly downloading a single project only. This is for
370375
// convenience as often the latest revision (referred to by some VCS-specific symbolic name) of a
371376
// project needs to be downloaded.
372-
val config = globalOptionsForSubcommands.config.downloader.copy(allowMovingRevisions = true)
377+
val config = ortConfig.downloader.copy(allowMovingRevisions = true)
373378
val provenance = Downloader(config).download(dummyPackage, outputDir)
374379
println("Successfully downloaded $provenance.")
375380
}.onFailure {

cli/src/main/kotlin/commands/EvaluatorCommand.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import java.io.File
3939

4040
import kotlin.time.measureTimedValue
4141

42+
import org.koin.core.component.KoinComponent
43+
import org.koin.core.component.inject
44+
4245
import org.ossreviewtoolkit.analyzer.curation.FilePackageCurationProvider
4346
import org.ossreviewtoolkit.cli.GlobalOptions
4447
import org.ossreviewtoolkit.cli.GroupTypes.FileType
@@ -58,7 +61,7 @@ import org.ossreviewtoolkit.evaluator.Evaluator
5861
import org.ossreviewtoolkit.model.FileFormat
5962
import org.ossreviewtoolkit.model.RuleViolation
6063
import org.ossreviewtoolkit.model.config.CopyrightGarbage
61-
import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns
64+
import org.ossreviewtoolkit.model.config.OrtConfiguration
6265
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
6366
import org.ossreviewtoolkit.model.config.createFileArchiver
6467
import org.ossreviewtoolkit.model.config.orEmpty
@@ -81,7 +84,10 @@ import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
8184
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
8285
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
8386

84-
class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT result files against policy rules.") {
87+
class EvaluatorCommand : KoinComponent, CliktCommand(
88+
name = "evaluate",
89+
help = "Evaluate ORT result files against policy rules."
90+
) {
8591
private val ortFile by option(
8692
"--ort-file", "-i",
8793
help = "The ORT result file to read as input."
@@ -204,6 +210,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
204210
).flag()
205211

206212
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
213+
private val ortConfig by inject<OrtConfiguration>()
207214

208215
override fun run() {
209216
val configurationFiles = listOfNotNull(
@@ -272,9 +279,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
272279
ortResultInput = ortResultInput.replacePackageCurations(curations)
273280
}
274281

275-
val config = globalOptionsForSubcommands.config
276-
277-
val packageConfigurationProvider = if (config.enableRepositoryPackageConfigurations) {
282+
val packageConfigurationProvider = if (ortConfig.enableRepositoryPackageConfigurations) {
278283
CompositePackageConfigurationProvider(
279284
SimplePackageConfigurationProvider(ortResultInput.repository.config.packageConfigurations),
280285
packageConfigurationOption.createProvider()
@@ -292,9 +297,9 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
292297
val licenseInfoResolver = LicenseInfoResolver(
293298
provider = DefaultLicenseInfoProvider(ortResultInput, packageConfigurationProvider),
294299
copyrightGarbage = copyrightGarbage,
295-
addAuthorsToCopyrights = config.addAuthorsToCopyrights,
296-
archiver = config.scanner.archive.createFileArchiver(),
297-
licenseFilenamePatterns = LicenseFilenamePatterns.getInstance()
300+
addAuthorsToCopyrights = ortConfig.addAuthorsToCopyrights,
301+
archiver = ortConfig.scanner.archive.createFileArchiver(),
302+
licenseFilenamePatterns = ortConfig.licenseFilePatterns
298303
)
299304

300305
val resolutionProvider = DefaultResolutionProvider.create(ortResultInput, resolutionsFile)
@@ -322,7 +327,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
322327
evaluatorRun.violations.partition { resolutionProvider.isResolved(it) }
323328
val severityStats = SeverityStats.createFromRuleViolations(resolvedViolations, unresolvedViolations)
324329

325-
severityStats.print().conclude(config.severeRuleViolationThreshold, 2)
330+
severityStats.print().conclude(ortConfig.severeRuleViolationThreshold, 2)
326331
}
327332
}
328333

0 commit comments

Comments
 (0)