From 28eb8cb588dd36980f79c5d3ce244c3c42543040 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Sat, 23 Nov 2024 20:06:45 +1100 Subject: [PATCH] Simple info/show command --- Sources/apk/Index/ApkIndex.swift | 8 ++++- Sources/dpk-cli/CommandLine.swift | 3 +- .../dpk-cli/Subcommands/DpkInfoCommand.swift | 33 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 Sources/dpk-cli/Subcommands/DpkInfoCommand.swift diff --git a/Sources/apk/Index/ApkIndex.swift b/Sources/apk/Index/ApkIndex.swift index 6c1dc47..2bbf63c 100644 --- a/Sources/apk/Index/ApkIndex.swift +++ b/Sources/apk/Index/ApkIndex.swift @@ -7,12 +7,18 @@ public struct ApkIndex: Sendable { public let packages: [ApkIndexPackage] } -extension ApkIndex { +public extension ApkIndex { func first(name: String) -> ApkIndexPackage? { self.packages.first { $0.name == name } } + + func filter(name: String) -> [ApkIndexPackage] { + self.packages.filter { + $0.name == name + } + } } public extension ApkIndex { diff --git a/Sources/dpk-cli/CommandLine.swift b/Sources/dpk-cli/CommandLine.swift index d8dcba7..e4c42a7 100644 --- a/Sources/dpk-cli/CommandLine.swift +++ b/Sources/dpk-cli/CommandLine.swift @@ -15,6 +15,7 @@ struct DarwinApkCLI: AsyncParsableCommand { DpkRemoveCommand.self, DpkUpdateCommand.self, DpkUpgradeCommand.self, - DpkSearchCommand.self + DpkSearchCommand.self, + DpkInfoCommand.self ]) } diff --git a/Sources/dpk-cli/Subcommands/DpkInfoCommand.swift b/Sources/dpk-cli/Subcommands/DpkInfoCommand.swift new file mode 100644 index 0000000..691d391 --- /dev/null +++ b/Sources/dpk-cli/Subcommands/DpkInfoCommand.swift @@ -0,0 +1,33 @@ +/* + * darwin-apk © 2024 Gay Pizza Specifications + * SPDX-License-Identifier: Apache-2.0 + */ + +import ArgumentParser +import darwin_apk + +struct DpkInfoCommand: AsyncParsableCommand { + static let configuration = CommandConfiguration( + commandName: "info", + abstract: "Show information about a package", + aliases: [ "S", "show" ]) + + @Argument(help: "One or more package names to print information about.") + var packages: [String] + + func run() async throws(ExitCode) { + let localRepositories = try await ApkRepositoriesConfig() + let index: ApkIndex + do { + index = try await ApkIndexReader.resolve(localRepositories, fetch: .local) + } catch { + eprint("Failed to build package index: \(error.localizedDescription)") + throw .failure + } + + self.packages.lazy + .flatMap(index.filter) + .map(\.description) + .forEach { print($0) } + } +}