Skip to content

Commit

Permalink
feat: add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Saiya committed Mar 19, 2022
1 parent 4dca067 commit a3a6688
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Search bookmarks in SourceTree and launch them in Alfred.
## Usage
launch alfred then input `st` and `keyword` separated with a space to search bookmarks, press enter to launch the bookmark in SourceTree.

> press `cmd` + `enter` to reveal the repo in Finder
## Contributions and Support
I'm new to swift, feel free to make a pull request if you are willing to improve the code quality or its functions.
Binary file modified SourceTree.alfredworkflow
Binary file not shown.
60 changes: 52 additions & 8 deletions workflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,46 @@ import Foundation

class SourceTree {
init() { }
func parsePlist () {

func run () {
guard let data = try? Data(contentsOf: plistPath) else {
let alfredResult = AlfredResult(items: [
AlfredItem(title: "SourceTree not installed", subtitle: "Press enter to open SourceTree homepage and download it", arg: "open \"https://sourcetreeapp.com/\"")
])
prettyPrint(alfredResult)
return
}
do {
let parsed = try PropertyListDecoder().decode(SourceTreePlist.self, from: data)
let alfredResult = toAlfredResult(parsed.objects)
prettyPrint(alfredResult)
} catch {
print("failed to parse: ")
print(error)
let githubNewIssueUrl = "https://github.com/oe/sourcetree-alfred-workflow/issues/new"
var urlComponents = URLComponents(string: githubNewIssueUrl)!
let issueBody = """
error message:
\(error.localizedDescription)
environment info:
macOS version: [pleaase fill your version]
swift version: [run `swift --version` to get its version]
"""
let queryItems = [
URLQueryItem(name: "title", value: "SourceTree plist parse error"),
URLQueryItem(name: "body", value: issueBody)
]
if urlComponents.queryItems == nil {
urlComponents.queryItems = []
}
urlComponents.queryItems!.append(contentsOf: queryItems)
let alfredResult = AlfredResult(items: [
AlfredItem(
title: "Error occurred",
subtitle: "Press enter to open github and report an issue to me",
arg: "open \"\(urlComponents.url?.absoluteString ?? githubNewIssueUrl)\""
)
])
prettyPrint(alfredResult)
}
}

Expand All @@ -40,9 +68,14 @@ class SourceTree {
name = str
}
}

var items: [AlfredItem] = namePathGroups.map { (name, path) in
let mod = AlfredItemModItem(valid: true, arg: "open \"\(path)\"", subtitle: "Reveal in Finder")
return AlfredItem(title: name, subtitle: path, match: spaceWords(name), arg: path, mods: AlfredItemMod(cmd: mod))
}

let items = namePathGroups.map { (name, path) in
AlfredItem(title: name, subtitle: path, arg: path, match: spaceWords(name))
if items.isEmpty {
items.append(AlfredItem(title: "Your SourceTree Bookmark Is Empty ", subtitle: "Please add repos to SourceTree first"))
}

return AlfredResult(items: items)
Expand Down Expand Up @@ -70,8 +103,19 @@ class SourceTree {
struct AlfredItem: Codable {
var title: String
var subtitle: String
var match: String?
var arg: String?
var mods: AlfredItemMod?
}

struct AlfredItemMod: Codable {
var cmd: AlfredItemModItem
}

struct AlfredItemModItem: Codable {
var valid: Bool
var arg: String
var match: String
var subtitle: String
}

struct SourceTreePlist: Codable {
Expand Down Expand Up @@ -114,4 +158,4 @@ extension UnkeyedDecodingContainer {
}
}

SourceTree().parsePlist()
SourceTree().run()

0 comments on commit a3a6688

Please sign in to comment.