Initial commit

This commit is contained in:
Gabriel Augendre 2020-07-05 15:31:23 +02:00
commit 2931f7039e
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
8 changed files with 131 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/

17
Makefile Normal file
View file

@ -0,0 +1,17 @@
prefix ?= /usr/local
bindir = $(prefix)/bin
libdir = $(prefix)/lib
build:
swift build -c release --disable-sandbox
install: build
install ".build/release/aerc-contacts-cli" "$(bindir)"
uninstall:
rm -rf "$(bindir)/aerc-contacts-cli"
clean:
rm -rf .build
.PHONY: build install uninstall clean

25
Package.swift Normal file
View file

@ -0,0 +1,25 @@
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "aerc-contacts-cli",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "aerc-contacts-cli",
targets: ["aerc-contacts-cli"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "aerc-contacts-cli",
dependencies: []),
]
)

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# aerc-contacts-cli
A description of this package.

View file

@ -0,0 +1,50 @@
import AddressBook
let arguments = CommandLine.arguments.dropFirst()
if arguments.isEmpty {
fputs("No arguments given\n", stderr)
exit(EXIT_FAILURE)
}
guard let addressBook = ABAddressBook.shared() else {
fputs("Failed to create address book (check your Contacts privacy settings)\n", stderr)
exit(EXIT_FAILURE)
}
private func comparison(forProperty property: String, string: String) -> ABSearchElement {
let comparison: ABSearchComparison = CFIndex(kABContainsSubStringCaseInsensitive.rawValue)
return ABPerson.searchElement(forProperty: property, label: nil, key: nil, value: string,
comparison: comparison)
}
var operands = [ABSearchElement]()
for argument in arguments {
let firstNameSearch = comparison(forProperty: kABFirstNameProperty, string: argument)
let lastNameSearch = comparison(forProperty: kABLastNameProperty, string: argument)
let emailSearch = comparison(forProperty: kABEmailProperty, string: argument)
let orComparison = ABSearchElement(forConjunction: CFIndex(kABSearchOr.rawValue),
children: [firstNameSearch, lastNameSearch, emailSearch])
if orComparison != nil {
operands.append(orComparison!)
}
}
let andComparison = ABSearchElement(forConjunction: CFIndex(kABSearchAnd.rawValue), children: operands)
let found = addressBook.records(matching: andComparison) as? [ABRecord] ?? []
if found.count == 0 {
exit(EXIT_SUCCESS)
}
for person in found {
let firstName = person.value(forProperty: kABFirstNameProperty) as? String ?? ""
let lastName = person.value(forProperty: kABLastNameProperty) as? String ?? ""
let emailsProperty = person.value(forProperty: kABEmailProperty) as? ABMultiValue
if let emails = emailsProperty {
for i in 0..<emails.count() {
let email = emails.value(at: i) as? String ?? ""
print("\(email)\t\(firstName) \(lastName)")
}
}
}

7
Tests/LinuxMain.swift Normal file
View file

@ -0,0 +1,7 @@
import XCTest
import aerc_contacts_cliTests
var tests = [XCTestCaseEntry]()
tests += aerc_contacts_cliTests.allTests()
XCTMain(tests)

View file

@ -0,0 +1,9 @@
import XCTest
#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(aerc_contacts_cliTests.allTests),
]
}
#endif

View file

@ -0,0 +1,15 @@
import XCTest
@testable import aerc_contacts_cli
final class aerc_contacts_cliTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
// XCTAssertEqual(aerc_contacts_cli().text, "Hello, World!")
}
static var allTests = [
("testExample", testExample),
]
}