mirror of
				https://github.com/GayPizzaSpecifications/darwin-apk.git
				synced 2025-11-04 07:59:38 +00:00 
			
		
		
		
	prefer to print to stderr for console chatter
This commit is contained in:
		
							
								
								
									
										38
									
								
								Sources/dpk-cli/ApkRepositoriesConfig.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								Sources/dpk-cli/ApkRepositoriesConfig.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
/*
 | 
			
		||||
 * darwin-apk © 2024 Gay Pizza Specifications
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
import ArgumentParser
 | 
			
		||||
import darwin_apk
 | 
			
		||||
 | 
			
		||||
public struct ApkRepositoriesConfig {
 | 
			
		||||
  public let repositories: [ApkIndexRepository]
 | 
			
		||||
 | 
			
		||||
  public init() async throws(ExitCode) {
 | 
			
		||||
    do {
 | 
			
		||||
      self.repositories = try await Self.readConfig(name: "repositories").flatMap { repo in
 | 
			
		||||
        Self.readConfig(name: "arch").map { arch in
 | 
			
		||||
          ApkIndexRepository(name: repo, arch: arch)
 | 
			
		||||
        }
 | 
			
		||||
      }.reduce(into: []) { $0.append($1) }
 | 
			
		||||
    } catch {
 | 
			
		||||
      eprint("Failed to read repository configurations, \(error.localizedDescription)")
 | 
			
		||||
      throw .failure
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static func readConfig(name: String)
 | 
			
		||||
      -> AsyncFilterSequence<AsyncMapSequence<AsyncLineSequence<URL.AsyncBytes>, String>> {
 | 
			
		||||
    return URL(filePath: name, directoryHint: .notDirectory).lines
 | 
			
		||||
      .map { $0.trimmingCharacters(in: .whitespaces) }
 | 
			
		||||
      .filter { !$0.isEmpty && $0.first != "#" }  // Ignore empty & commented lines
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public extension ApkIndex {
 | 
			
		||||
  @inlinable static func resolve(_ config: ApkRepositoriesConfig, fetch: ApkIndexFetchMode) async throws -> Self {
 | 
			
		||||
    try await Self.resolve(config.repositories, fetch: fetch)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -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
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										17
									
								
								Sources/dpk-cli/StandardErrorPrint.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Sources/dpk-cli/StandardErrorPrint.swift
									
									
									
									
									
										Normal file
									
								
							@ -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))
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -15,6 +15,6 @@ struct DpkInstallCommand: ParsableCommand {
 | 
			
		||||
  var packages: [String]
 | 
			
		||||
 | 
			
		||||
  func run() throws {
 | 
			
		||||
    print("installing \"\(packages.joined(separator: "\", \""))\"")
 | 
			
		||||
    eprint("installing \"\(packages.joined(separator: "\", \""))\"")
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -15,6 +15,6 @@ struct DpkRemoveCommand: ParsableCommand {
 | 
			
		||||
  var packages: [String]
 | 
			
		||||
 | 
			
		||||
  func run() throws {
 | 
			
		||||
    print("uninstalling \"\(packages.joined(separator: "\", \""))\"")
 | 
			
		||||
    eprint("uninstalling \"\(packages.joined(separator: "\", \""))\"")
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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)")
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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: "\", \""))\"")
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user