From 2931f7039e423419d923bfa19e4bb693d31215b7 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 5 Jul 2020 15:31:23 +0200 Subject: [PATCH] Initial commit --- .gitignore | 5 ++ Makefile | 17 +++++++ Package.swift | 25 ++++++++++ README.md | 3 ++ Sources/aerc-contacts-cli/main.swift | 50 +++++++++++++++++++ Tests/LinuxMain.swift | 7 +++ .../XCTestManifests.swift | 9 ++++ .../aerc_contacts_cliTests.swift | 15 ++++++ 8 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Package.swift create mode 100644 README.md create mode 100644 Sources/aerc-contacts-cli/main.swift create mode 100644 Tests/LinuxMain.swift create mode 100644 Tests/aerc-contacts-cliTests/XCTestManifests.swift create mode 100644 Tests/aerc-contacts-cliTests/aerc_contacts_cliTests.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95c4320 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..da82fdd --- /dev/null +++ b/Makefile @@ -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 diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..d4b5745 --- /dev/null +++ b/Package.swift @@ -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: []), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..b57e3b5 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# aerc-contacts-cli + +A description of this package. diff --git a/Sources/aerc-contacts-cli/main.swift b/Sources/aerc-contacts-cli/main.swift new file mode 100644 index 0000000..50a2b87 --- /dev/null +++ b/Sources/aerc-contacts-cli/main.swift @@ -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.. [XCTestCaseEntry] { + return [ + testCase(aerc_contacts_cliTests.allTests), + ] +} +#endif diff --git a/Tests/aerc-contacts-cliTests/aerc_contacts_cliTests.swift b/Tests/aerc-contacts-cliTests/aerc_contacts_cliTests.swift new file mode 100644 index 0000000..d7e154d --- /dev/null +++ b/Tests/aerc-contacts-cliTests/aerc_contacts_cliTests.swift @@ -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), + ] +}