mirror of
				https://github.com/GayPizzaSpecifications/darwin-apk.git
				synced 2025-11-04 07:59:38 +00:00 
			
		
		
		
	try implementing a command that can search the index for packages
This commit is contained in:
		@ -6,7 +6,7 @@
 | 
			
		||||
import ArgumentParser
 | 
			
		||||
 | 
			
		||||
@main
 | 
			
		||||
struct DarwinApkCLI: ParsableCommand {
 | 
			
		||||
struct DarwinApkCLI: AsyncParsableCommand {
 | 
			
		||||
  static let configuration = CommandConfiguration(
 | 
			
		||||
    commandName: "dpk",
 | 
			
		||||
    abstract: "Command-line interface for managing packages installed via darwin-apk.",
 | 
			
		||||
@ -14,6 +14,7 @@ struct DarwinApkCLI: ParsableCommand {
 | 
			
		||||
      DpkInstallCommand.self,
 | 
			
		||||
      DpkRemoveCommand.self,
 | 
			
		||||
      DpkUpdateCommand.self,
 | 
			
		||||
      DpkUpgradeCommand.self
 | 
			
		||||
      DpkUpgradeCommand.self,
 | 
			
		||||
      DpkSearchCommand.self
 | 
			
		||||
    ])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										83
									
								
								Sources/dpk-cli/Subcommands/DpkSearchCommand.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								Sources/dpk-cli/Subcommands/DpkSearchCommand.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,83 @@
 | 
			
		||||
/*
 | 
			
		||||
 * darwin-apk © 2024 Gay Pizza Specifications
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
import ArgumentParser
 | 
			
		||||
import darwin_apk
 | 
			
		||||
 | 
			
		||||
struct DpkSearchCommand: AsyncParsableCommand {
 | 
			
		||||
  static let configuration = CommandConfiguration(
 | 
			
		||||
    commandName: "search",
 | 
			
		||||
    abstract: "Search for packages with a pattern matching name and description",
 | 
			
		||||
    aliases: [ "s" ])
 | 
			
		||||
 | 
			
		||||
  @Flag
 | 
			
		||||
  var nameOnly: Bool = false
 | 
			
		||||
 | 
			
		||||
  @Argument
 | 
			
		||||
  var patterns: [String]
 | 
			
		||||
 | 
			
		||||
  func run() async throws(ExitCode) {
 | 
			
		||||
    let re: [Regex<_StringProcessing.AnyRegexOutput>]
 | 
			
		||||
    do {
 | 
			
		||||
      re = try patterns.map(Regex.init)
 | 
			
		||||
    } catch {
 | 
			
		||||
      print("Bad pattern \(error.localizedDescription)")
 | 
			
		||||
      throw .validationFailure
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let repositories: [String], architectures: [String]
 | 
			
		||||
    do {
 | 
			
		||||
      repositories = try await PropertyFile.read(name: "repositories")
 | 
			
		||||
    } catch {
 | 
			
		||||
      print("Failed to read repositories: \(error.localizedDescription)")
 | 
			
		||||
      throw .failure
 | 
			
		||||
    }
 | 
			
		||||
    do {
 | 
			
		||||
      architectures = try await PropertyFile.read(name: "arch")
 | 
			
		||||
    } catch {
 | 
			
		||||
      print("Failed to read arch: \(error.localizedDescription)")
 | 
			
		||||
      throw .failure
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let localRepositories = repositories.flatMap { repo in
 | 
			
		||||
      architectures.map { arch in
 | 
			
		||||
        URL(filePath: ApkIndexRepository(name: repo, arch: arch).localName, directoryHint: .notDirectory)
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    let index: ApkIndex
 | 
			
		||||
    do {
 | 
			
		||||
      index = ApkIndex.merge(try localRepositories.map(ApkIndex.init))
 | 
			
		||||
    } catch {
 | 
			
		||||
      print("Failed to build package index: \(error.localizedDescription)")
 | 
			
		||||
      throw .failure
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
      for package in index.packages {
 | 
			
		||||
        for pattern in re {
 | 
			
		||||
          if try
 | 
			
		||||
              pattern.firstMatch(in: package.name) != nil ||
 | 
			
		||||
              (!self.nameOnly && pattern.firstMatch(in: package.packageDescription) != nil) {
 | 
			
		||||
            print(package.shortDescription)
 | 
			
		||||
            break
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    } catch {
 | 
			
		||||
      print("Something went wrong: \(error.localizedDescription)")
 | 
			
		||||
      throw .failure
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct PropertyFile {
 | 
			
		||||
  static func read(name: String) async throws -> [String] {
 | 
			
		||||
    try await URL(filePath: name, directoryHint: .notDirectory).lines
 | 
			
		||||
      .map { $0.trimmingCharacters(in: .whitespaces) }
 | 
			
		||||
      .filter { !$0.isEmpty && $0.first != "#" }  // Ignore empty & commented lines
 | 
			
		||||
      .reduce(into: [String]()) { $0.append($1) }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user