From 44fba5edec9fecaf989bd703a236dbbe5cdda364 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 20 Feb 2024 14:02:54 +0100 Subject: [PATCH] feat(helper-cli): Add a command to delete stored provenance by package id Signed-off-by: Sebastian Schuberth --- helper-cli/src/main/kotlin/HelperMain.kt | 2 + .../provenancestorage/DeleteCommand.kt | 89 +++++++++++++++++++ .../ProvenanceStorageCommand.kt | 33 +++++++ 3 files changed, 124 insertions(+) create mode 100644 helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt create mode 100644 helper-cli/src/main/kotlin/commands/provenancestorage/ProvenanceStorageCommand.kt diff --git a/helper-cli/src/main/kotlin/HelperMain.kt b/helper-cli/src/main/kotlin/HelperMain.kt index 6c65931daf843..935be5c2012cc 100644 --- a/helper-cli/src/main/kotlin/HelperMain.kt +++ b/helper-cli/src/main/kotlin/HelperMain.kt @@ -38,6 +38,7 @@ import org.ossreviewtoolkit.helper.commands.classifications.LicenseClassificatio import org.ossreviewtoolkit.helper.commands.dev.DevCommand import org.ossreviewtoolkit.helper.commands.packageconfig.PackageConfigurationCommand import org.ossreviewtoolkit.helper.commands.packagecuration.PackageCurationsCommand +import org.ossreviewtoolkit.helper.commands.provenancestorage.ProvenanceStorageCommand import org.ossreviewtoolkit.helper.commands.repoconfig.RepositoryConfigurationCommand import org.ossreviewtoolkit.helper.commands.scanstorage.ScanStorageCommand import org.ossreviewtoolkit.helper.utils.ORTH_NAME @@ -97,6 +98,7 @@ internal class HelperMain : CliktCommand( MergeRepositoryConfigurationsCommand(), PackageConfigurationCommand(), PackageCurationsCommand(), + ProvenanceStorageCommand(), RepositoryConfigurationCommand(), ScanStorageCommand(), SetDependencyRepresentationCommand(), diff --git a/helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt b/helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt new file mode 100644 index 0000000000000..13f19c1ec4c17 --- /dev/null +++ b/helper-cli/src/main/kotlin/commands/provenancestorage/DeleteCommand.kt @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2024 The ORT Project Authors (see ) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * License-Filename: LICENSE + */ + +package org.ossreviewtoolkit.helper.commands.provenancestorage + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.core.terminal +import com.github.ajalt.clikt.parameters.options.associate +import com.github.ajalt.clikt.parameters.options.convert +import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.options.flag +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.options.required +import com.github.ajalt.clikt.parameters.types.file +import com.github.ajalt.mordant.rendering.Theme +import com.github.ajalt.mordant.terminal.YesNoPrompt + +import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.config.OrtConfiguration +import org.ossreviewtoolkit.scanner.ScanStorages +import org.ossreviewtoolkit.utils.common.expandTilde +import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME +import org.ossreviewtoolkit.utils.ort.ortConfigDirectory + +internal class DeleteCommand : CliktCommand( + help = "Deletes stored provenance results matching the options." +) { + private val configFile by option( + "--config", + help = "The path to the ORT configuration file that configures the scan storages." + ).convert { it.expandTilde() } + .file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true) + .convert { it.absoluteFile.normalize() } + .default(ortConfigDirectory.resolve(ORT_CONFIG_FILENAME)) + + private val configArguments by option( + "-P", + help = "Override a key-value pair in the configuration file. For example: " + + "-P ort.scanner.storages.postgres.connection.schema=testSchema" + ).associate() + + private val packageId by option( + "--package-id", + help = "Coordinates of the package ID to delete." + ).convert { Identifier(it) } + .required() + + private val forceYes by option( + "--yes", "-y", + help = "Force yes on all prompts." + ).flag() + + override fun run() { + val config = OrtConfiguration.load(configArguments, configFile) + val scanStorages = ScanStorages.createFromConfig(config.scanner) + + val provenances = scanStorages.packageProvenanceStorage.readProvenances(packageId) + if (provenances.isEmpty()) { + val pkgCoords = Theme.Default.success(packageId.toCoordinates()) + echo(Theme.Default.info("No stored provenance found for '$pkgCoords'.")) + return + } + + val count = Theme.Default.warning(provenances.size.toString()) + + echo(Theme.Default.danger("About to delete the following $count provenance(s):")) + provenances.forEach(::echo) + + if (forceYes || YesNoPrompt("Continue?", terminal).ask() == true) { + scanStorages.packageProvenanceStorage.deleteProvenances(packageId) + } + } +} diff --git a/helper-cli/src/main/kotlin/commands/provenancestorage/ProvenanceStorageCommand.kt b/helper-cli/src/main/kotlin/commands/provenancestorage/ProvenanceStorageCommand.kt new file mode 100644 index 0000000000000..3aa7f7f7f0b98 --- /dev/null +++ b/helper-cli/src/main/kotlin/commands/provenancestorage/ProvenanceStorageCommand.kt @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2024 The ORT Project Authors (see ) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * License-Filename: LICENSE + */ + +package org.ossreviewtoolkit.helper.commands.provenancestorage + +import com.github.ajalt.clikt.core.NoOpCliktCommand +import com.github.ajalt.clikt.core.subcommands + +internal class ProvenanceStorageCommand : NoOpCliktCommand( + help = "Commands for working with provenance storages." +) { + init { + subcommands( + DeleteCommand() + ) + } +}