From c239b8e42414b066a04e889899b8bfcc305ae7e0 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Sun, 17 Nov 2024 02:59:13 +1100 Subject: [PATCH] prefer to print to stderr for console chatter --- Sources/apk/Index/ApkIndexReading.swift | 1 + .../ApkRepositoriesConfig.swift | 3 ++- Sources/dpk-cli/RegexMatcher.swift | 2 +- Sources/dpk-cli/StandardErrorPrint.swift | 17 +++++++++++++++++ .../dpk-cli/Subcommands/DpkInstallCommand.swift | 2 +- .../dpk-cli/Subcommands/DpkRemoveCommand.swift | 2 +- .../dpk-cli/Subcommands/DpkSearchCommand.swift | 4 ++-- .../dpk-cli/Subcommands/DpkUpdateCommand.swift | 4 ++-- .../dpk-cli/Subcommands/DpkUpgradeCommand.swift | 4 ++-- 9 files changed, 29 insertions(+), 10 deletions(-) rename Sources/{apk/Common => dpk-cli}/ApkRepositoriesConfig.swift (91%) create mode 100644 Sources/dpk-cli/StandardErrorPrint.swift diff --git a/Sources/apk/Index/ApkIndexReading.swift b/Sources/apk/Index/ApkIndexReading.swift index fcdf2b1..123b33a 100644 --- a/Sources/apk/Index/ApkIndexReading.swift +++ b/Sources/apk/Index/ApkIndexReading.swift @@ -45,6 +45,7 @@ public extension ApkIndex { } local = URL(filePath: repository.localName) case .update: + //FIXME: Don't call print in the lib print("Fetching \"\(repository.resolved)\"") local = try await ApkIndexDownloader.fetch(repository: repository) } diff --git a/Sources/apk/Common/ApkRepositoriesConfig.swift b/Sources/dpk-cli/ApkRepositoriesConfig.swift similarity index 91% rename from Sources/apk/Common/ApkRepositoriesConfig.swift rename to Sources/dpk-cli/ApkRepositoriesConfig.swift index eef075e..0c42479 100644 --- a/Sources/apk/Common/ApkRepositoriesConfig.swift +++ b/Sources/dpk-cli/ApkRepositoriesConfig.swift @@ -5,6 +5,7 @@ import Foundation import ArgumentParser +import darwin_apk public struct ApkRepositoriesConfig { public let repositories: [ApkIndexRepository] @@ -17,7 +18,7 @@ public struct ApkRepositoriesConfig { } }.reduce(into: []) { $0.append($1) } } catch { - print("Failed to read repository configurations, \(error.localizedDescription)") + eprint("Failed to read repository configurations, \(error.localizedDescription)") throw .failure } } diff --git a/Sources/dpk-cli/RegexMatcher.swift b/Sources/dpk-cli/RegexMatcher.swift index 42fb6a2..1587dd9 100644 --- a/Sources/dpk-cli/RegexMatcher.swift +++ b/Sources/dpk-cli/RegexMatcher.swift @@ -13,7 +13,7 @@ struct RegexMatcher: PatternMatcher { do { self._patterns = try patterns.map(Regex.init) } catch { - print("Bad pattern \(error.localizedDescription)") + eprint("Bad pattern \(error.localizedDescription)") throw .validationFailure } } diff --git a/Sources/dpk-cli/StandardErrorPrint.swift b/Sources/dpk-cli/StandardErrorPrint.swift new file mode 100644 index 0000000..8c9f9f4 --- /dev/null +++ b/Sources/dpk-cli/StandardErrorPrint.swift @@ -0,0 +1,17 @@ +/* + * darwin-apk © 2024 Gay Pizza Specifications + * SPDX-License-Identifier: Apache-2.0 + */ + +import Foundation + +internal func eprint(_ items: Any..., separator: String = " ", terminator: String = "\n") { + var stderr = FileHandle.standardError + print(items, separator: separator, terminator: terminator, to: &stderr) +} + +extension FileHandle: @retroactive TextOutputStream { + public func write(_ string: String) { + self.write(Data(string.utf8)) + } +} diff --git a/Sources/dpk-cli/Subcommands/DpkInstallCommand.swift b/Sources/dpk-cli/Subcommands/DpkInstallCommand.swift index e1dbef1..544e407 100644 --- a/Sources/dpk-cli/Subcommands/DpkInstallCommand.swift +++ b/Sources/dpk-cli/Subcommands/DpkInstallCommand.swift @@ -15,6 +15,6 @@ struct DpkInstallCommand: ParsableCommand { var packages: [String] func run() throws { - print("installing \"\(packages.joined(separator: "\", \""))\"") + eprint("installing \"\(packages.joined(separator: "\", \""))\"") } } diff --git a/Sources/dpk-cli/Subcommands/DpkRemoveCommand.swift b/Sources/dpk-cli/Subcommands/DpkRemoveCommand.swift index e34aa40..09995c5 100644 --- a/Sources/dpk-cli/Subcommands/DpkRemoveCommand.swift +++ b/Sources/dpk-cli/Subcommands/DpkRemoveCommand.swift @@ -15,6 +15,6 @@ struct DpkRemoveCommand: ParsableCommand { var packages: [String] func run() throws { - print("uninstalling \"\(packages.joined(separator: "\", \""))\"") + eprint("uninstalling \"\(packages.joined(separator: "\", \""))\"") } } diff --git a/Sources/dpk-cli/Subcommands/DpkSearchCommand.swift b/Sources/dpk-cli/Subcommands/DpkSearchCommand.swift index b3a0258..1cbb8dd 100644 --- a/Sources/dpk-cli/Subcommands/DpkSearchCommand.swift +++ b/Sources/dpk-cli/Subcommands/DpkSearchCommand.swift @@ -27,7 +27,7 @@ struct DpkSearchCommand: AsyncParsableCommand { func run() async throws(ExitCode) { if self.regex && self.exact { - print("Only one of \(self._regex.description) and \(self._exact.description) is allowed") + eprint("Only one of \(self._regex.description) and \(self._exact.description) is allowed") throw .validationFailure } @@ -46,7 +46,7 @@ struct DpkSearchCommand: AsyncParsableCommand { do { index = try await ApkIndex.resolve(localRepositories, fetch: .local) } catch { - print("Failed to build package index: \(error.localizedDescription)") + eprint("Failed to build package index: \(error.localizedDescription)") throw .failure } diff --git a/Sources/dpk-cli/Subcommands/DpkUpdateCommand.swift b/Sources/dpk-cli/Subcommands/DpkUpdateCommand.swift index 7319077..16ae139 100644 --- a/Sources/dpk-cli/Subcommands/DpkUpdateCommand.swift +++ b/Sources/dpk-cli/Subcommands/DpkUpdateCommand.swift @@ -18,8 +18,8 @@ struct DpkUpdateCommand: AsyncParsableCommand { func run() async throws { let repositories = try await ApkRepositoriesConfig().repositories - print("Updating package repositories") + eprint("Updating package repositories") let index = try await ApkIndex.resolve(repositories, fetch: self.lazyDownload ? .lazy : .update) - print("Indexed \(index.packages.count) package(s)") + eprint("Indexed \(index.packages.count) package(s)") } } diff --git a/Sources/dpk-cli/Subcommands/DpkUpgradeCommand.swift b/Sources/dpk-cli/Subcommands/DpkUpgradeCommand.swift index eed76e5..050f0e3 100644 --- a/Sources/dpk-cli/Subcommands/DpkUpgradeCommand.swift +++ b/Sources/dpk-cli/Subcommands/DpkUpgradeCommand.swift @@ -16,9 +16,9 @@ struct DpkUpgradeCommand: ParsableCommand { func run() throws { if packages.isEmpty { - print("upgrading system") + eprint("upgrading system") } else { - print("upgrading invidual packages: \"\(packages.joined(separator: "\", \""))\"") + eprint("upgrading individual packages: \"\(packages.joined(separator: "\", \""))\"") } } }