mirror of
https://github.com/GayPizzaSpecifications/darwin-apk.git
synced 2025-08-04 05:51:31 +00:00
add different match types to search
This commit is contained in:
36
Sources/dpk-cli/ExactMatcher.swift
Normal file
36
Sources/dpk-cli/ExactMatcher.swift
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* darwin-apk © 2024 Gay Pizza Specifications
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import ArgumentParser
|
||||||
|
|
||||||
|
struct ExactMatcher: PatternMatcher {
|
||||||
|
private let _matches: [String]
|
||||||
|
private let _ignoreCase: Bool
|
||||||
|
|
||||||
|
init(patterns: [String], ignoreCase: Bool) throws(ArgumentParser.ExitCode) {
|
||||||
|
self._matches = patterns
|
||||||
|
self._ignoreCase = ignoreCase
|
||||||
|
}
|
||||||
|
|
||||||
|
func match(_ field: String) -> Bool {
|
||||||
|
if self._ignoreCase {
|
||||||
|
for match in self._matches {
|
||||||
|
// May want to use localizedCaseInsensitiveCompare
|
||||||
|
// if localised descriptions ever become involved
|
||||||
|
if field.caseInsensitiveCompare(match) == .orderedSame {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for match in self._matches {
|
||||||
|
if field == match {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
38
Sources/dpk-cli/GlobMatcher.swift
Normal file
38
Sources/dpk-cli/GlobMatcher.swift
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* darwin-apk © 2024 Gay Pizza Specifications
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import ArgumentParser
|
||||||
|
|
||||||
|
struct GlobMatcher: PatternMatcher {
|
||||||
|
private let _patterns: [String]
|
||||||
|
private let _flags: Int32
|
||||||
|
|
||||||
|
init(patterns: [String], ignoreCase: Bool) throws(ArgumentParser.ExitCode) {
|
||||||
|
self._patterns = patterns
|
||||||
|
self._flags = ignoreCase ? FNM_CASEFOLD : 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func match(_ field: String) -> Bool {
|
||||||
|
for pattern in self._patterns {
|
||||||
|
// Quick hack to make matching without explicit globs easier
|
||||||
|
if pattern.rangeOfCharacter(from: .init(charactersIn: "*?[]")) == nil {
|
||||||
|
if self._flags & FNM_CASEFOLD != 0 {
|
||||||
|
return field.localizedCaseInsensitiveContains(pattern)
|
||||||
|
} else {
|
||||||
|
return field.contains(pattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let res = fnmatch(pattern, field, self._flags)
|
||||||
|
if res == FNM_NOMATCH {
|
||||||
|
continue
|
||||||
|
} else if res == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
fatalError("fnmatch error \(res)")
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
12
Sources/dpk-cli/PatternMatcher.swift
Normal file
12
Sources/dpk-cli/PatternMatcher.swift
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* darwin-apk © 2024 Gay Pizza Specifications
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import ArgumentParser
|
||||||
|
|
||||||
|
protocol PatternMatcher {
|
||||||
|
init(patterns: [String], ignoreCase: Bool) throws(ExitCode)
|
||||||
|
func match(_ field: String) -> Bool
|
||||||
|
}
|
29
Sources/dpk-cli/RegexMatcher.swift
Normal file
29
Sources/dpk-cli/RegexMatcher.swift
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* darwin-apk © 2024 Gay Pizza Specifications
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import ArgumentParser
|
||||||
|
|
||||||
|
struct RegexMatcher: PatternMatcher {
|
||||||
|
private let _patterns: [Regex<_StringProcessing.AnyRegexOutput>]
|
||||||
|
|
||||||
|
init(patterns: [String], ignoreCase: Bool) throws(ExitCode) {
|
||||||
|
do {
|
||||||
|
self._patterns = try patterns.map(Regex.init)
|
||||||
|
} catch {
|
||||||
|
print("Bad pattern \(error.localizedDescription)")
|
||||||
|
throw .validationFailure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func match(_ field: String) -> Bool {
|
||||||
|
for pattern in self._patterns {
|
||||||
|
if (try? pattern.firstMatch(in: field)) != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
@ -13,21 +13,34 @@ struct DpkSearchCommand: AsyncParsableCommand {
|
|||||||
abstract: "Search for packages with a pattern matching name and description",
|
abstract: "Search for packages with a pattern matching name and description",
|
||||||
aliases: [ "s" ])
|
aliases: [ "s" ])
|
||||||
|
|
||||||
@Flag
|
@Flag(name: .shortAndLong, help: "Use regular expressions instead of globbing")
|
||||||
|
var regex: Bool = false
|
||||||
|
@Flag(name: [ .customShort("x"), .long ], help: "Match given strings exactly")
|
||||||
|
var exact: Bool = false
|
||||||
|
@Flag(name: [ .customShort("I"), .long ], help: "Use case-sensitive matching")
|
||||||
|
var caseSensitive: Bool = false
|
||||||
|
@Flag(name: .shortAndLong, help: "Only match names instead of names & descriptions")
|
||||||
var nameOnly: Bool = false
|
var nameOnly: Bool = false
|
||||||
|
|
||||||
@Argument
|
@Argument
|
||||||
var patterns: [String]
|
var patterns: [String]
|
||||||
|
|
||||||
func run() async throws(ExitCode) {
|
func run() async throws(ExitCode) {
|
||||||
let re: [Regex<_StringProcessing.AnyRegexOutput>]
|
if self.regex && self.exact {
|
||||||
do {
|
print("Only one of \(self._regex.description) and \(self._exact.description) is allowed")
|
||||||
re = try patterns.map(Regex.init)
|
|
||||||
} catch {
|
|
||||||
print("Bad pattern \(error.localizedDescription)")
|
|
||||||
throw .validationFailure
|
throw .validationFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let matcher: PatternMatcher.Type = if self.regex {
|
||||||
|
RegexMatcher.self
|
||||||
|
} else if self.exact {
|
||||||
|
ExactMatcher.self
|
||||||
|
} else {
|
||||||
|
GlobMatcher.self
|
||||||
|
}
|
||||||
|
let match: any PatternMatcher
|
||||||
|
match = try matcher.init(patterns: patterns, ignoreCase: !self.caseSensitive)
|
||||||
|
|
||||||
let repositories: [String], architectures: [String]
|
let repositories: [String], architectures: [String]
|
||||||
do {
|
do {
|
||||||
repositories = try await PropertyFile.read(name: "repositories")
|
repositories = try await PropertyFile.read(name: "repositories")
|
||||||
@ -55,20 +68,10 @@ struct DpkSearchCommand: AsyncParsableCommand {
|
|||||||
throw .failure
|
throw .failure
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
for package in index.packages {
|
||||||
for package in index.packages {
|
if match.match(package.name) || (!self.nameOnly && match.match(package.packageDescription)) {
|
||||||
for pattern in re {
|
print(package.shortDescription)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user