Skip to content

Commit dbbcd41

Browse files
author
Nikita Voloboev
committed
Merge branch 'kingpin'
2 parents e6f7c7c + bfe6604 commit dbbcd41

File tree

6 files changed

+165
-109
lines changed

6 files changed

+165
-109
lines changed

main.go

Lines changed: 51 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,91 @@
11
package main
22

33
import (
4-
"encoding/csv"
5-
"log"
6-
"os"
7-
"os/exec"
8-
"strings"
4+
"fmt"
95

6+
"github.com/alecthomas/kingpin"
107
"github.com/deanishe/awgo"
118
"github.com/deanishe/awgo/update"
12-
"github.com/docopt/docopt-go"
139
)
1410

15-
// Name of the background job that checks for updates
16-
const updateJobName = "checkForUpdate"
17-
18-
var usage = `alfred-ask-create-share [search|check] [<query>]
19-
20-
Open web submissions from Alfred.
21-
22-
Usage:
23-
alfred-ask-create-share search [<query>]
24-
alfred-ask-create-share check
25-
alfred-ask-create-share -h
26-
27-
Options:
28-
-h, --help Show this message and exit.
29-
`
11+
// Defaults for Kingpin flags
12+
const (
13+
defaultMaxResults = "100"
14+
)
3015

16+
// Icons
3117
var (
32-
// Icons
3318
iconAvailable = &aw.Icon{Value: "icons/update.png"}
3419
redditIcon = &aw.Icon{Value: "icons/reddit.png"}
3520
githubIcon = &aw.Icon{Value: "icons/github.png"}
3621
forumsIcon = &aw.Icon{Value: "icons/forums.png"}
3722
stackIcon = &aw.Icon{Value: "icons/stack.png"}
3823
docIcon = &aw.Icon{Value: "icons/doc.png"}
39-
40-
repo = "nikitavoloboev/alfred-ask-create-share"
41-
wf *aw.Workflow
4224
)
4325

44-
func init() {
45-
wf = aw.New(update.GitHub(repo))
46-
}
26+
var (
27+
// Kingpin and script options
28+
app *kingpin.Application
4729

48-
func run() {
49-
// Pass wf.Args() to docopt because our update logic relies on
50-
// AwGo's magic actions.
51-
args, _ := docopt.Parse(usage, wf.Args(), true, wf.Version(), false, true)
52-
53-
// Alternate action: get available releases from remote
54-
if args["check"] != false {
55-
wf.TextErrors = true
56-
log.Println("Checking for updates...")
57-
if err := wf.CheckForUpdate(); err != nil {
58-
wf.FatalError(err)
59-
}
60-
return
61-
}
30+
// Application commands
31+
searchCmd *kingpin.CmdClause
32+
updateCmd *kingpin.CmdClause
33+
testCmd *kingpin.CmdClause
6234

63-
// Script filter
64-
var query string
65-
if args["<query>"] != nil {
66-
query = args["<query>"].(string)
67-
}
35+
// Script options (populated by Kingpin application)
36+
query string
6837

69-
log.Printf("query=%s", query)
38+
repo = "nikitavoloboev/alfred-ask-create-share"
7039

71-
// Call self with "check" command if an update is due and a
72-
// check job isn't already running.
73-
if wf.UpdateCheckDue() && !aw.IsRunning(updateJobName) {
74-
log.Println("Running update check in background...")
75-
cmd := exec.Command("./alfred-ask-create-share", "check")
76-
if err := aw.RunInBackground(updateJobName, cmd); err != nil {
77-
log.Printf("Error starting update check: %s", err)
78-
}
79-
}
40+
// Workflow stuff
41+
wf *aw.Workflow
42+
)
8043

81-
if query == "" { // Only show update status if query is empty
82-
// Send update status to Alfred
83-
if wf.UpdateAvailable() {
84-
wf.NewItem("Update Available!").
85-
Subtitle("↩ to install").
86-
Autocomplete("workflow:update").
87-
Valid(false).
88-
Icon(iconAvailable)
89-
}
90-
}
44+
// Mostly sets up kingpin commands
45+
func init() {
46+
wf = aw.New(update.GitHub(repo), aw.HelpURL(repo+"/issues"))
9147

92-
links := parseCSV()
93-
94-
for key, value := range links {
95-
if strings.Contains(key, "r: ") {
96-
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(redditIcon)
97-
} else if strings.Contains(key, "s: ") {
98-
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(stackIcon)
99-
} else if strings.Contains(key, "g: ") {
100-
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(githubIcon)
101-
} else if strings.Contains(key, "f: ") {
102-
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(forumsIcon)
103-
} else if strings.Contains(key, "d: ") {
104-
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(docIcon)
105-
} else {
106-
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key)
107-
}
108-
}
48+
app = kingpin.New("ask", "Open web submissions.")
10949

110-
if query != "" {
111-
wf.Filter(query)
112-
}
50+
// Update command
51+
updateCmd = app.Command("update", "Check for new workflow version.").Alias("u")
52+
53+
// Commands using query
54+
searchCmd = app.Command("search", "Search web submissions.").Alias("s")
11355

114-
wf.WarnEmpty("No matching items", "Try a different query?")
115-
wf.SendFeedback()
56+
// Common options
57+
for _, cmd := range []*kingpin.CmdClause{
58+
searchCmd,
59+
} {
60+
cmd.Flag("query", "Search query.").Short('q').StringVar(&query)
61+
}
11662
}
11763

118-
// parseCSV parses CSV for links and arguments
119-
func parseCSV() map[string]string {
64+
func run() {
12065
var err error
12166

122-
// Load values from file to a hash map
123-
f, err := os.Open("ask-create-share.csv")
67+
cmd, err := app.Parse(wf.Args())
12468
if err != nil {
125-
panic(err)
69+
wf.FatalError(err)
12670
}
127-
defer f.Close()
128-
129-
r := csv.NewReader(f)
13071

131-
records, err := r.ReadAll()
132-
if err != nil {
133-
log.Fatal(err)
72+
switch cmd {
73+
case searchCmd.FullCommand():
74+
err = doSearch()
75+
case updateCmd.FullCommand():
76+
err = doUpdate()
77+
default:
78+
err = fmt.Errorf("Uknown command: %s", cmd)
13479
}
13580

136-
// Holds user's search arguments and an appropriate search URL
137-
links := make(map[string]string)
138-
139-
for _, record := range records {
140-
links[record[0]] = record[1]
81+
// Check for update
82+
if err == nil && cmd != updateCmd.FullCommand() {
83+
err = checkForUpdate()
14184
}
14285

143-
return links
144-
86+
if err != nil {
87+
wf.FatalError(err)
88+
}
14589
}
14690

14791
func main() {

search.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"encoding/csv"
5+
"log"
6+
"os"
7+
"strings"
8+
)
9+
10+
// doSearch makes a search for web submissions and returns results to Alfred.
11+
func doSearch() error {
12+
showUpdateStatus()
13+
14+
log.Printf("query=%s", query)
15+
16+
links := parseCSV()
17+
18+
for key, value := range links {
19+
if strings.Contains(key, "r: ") {
20+
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(redditIcon)
21+
} else if strings.Contains(key, "s: ") {
22+
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(stackIcon)
23+
} else if strings.Contains(key, "g: ") {
24+
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(githubIcon)
25+
} else if strings.Contains(key, "f: ") {
26+
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(forumsIcon)
27+
} else if strings.Contains(key, "d: ") {
28+
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key).Icon(docIcon)
29+
} else {
30+
wf.NewItem(key).Valid(true).UID(key).Var("URL", value).Var("ARG", key)
31+
}
32+
}
33+
34+
if query != "" {
35+
wf.Filter(query)
36+
}
37+
38+
wf.WarnEmpty("No matching items", "Try a different query?")
39+
wf.SendFeedback()
40+
return nil
41+
}
42+
43+
// parseCSV parses CSV for links and arguments.
44+
func parseCSV() map[string]string {
45+
var err error
46+
47+
// Load values from file to a hash map
48+
f, err := os.Open("ask-create-share.csv")
49+
if err != nil {
50+
panic(err)
51+
}
52+
defer f.Close()
53+
54+
r := csv.NewReader(f)
55+
56+
records, err := r.ReadAll()
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
// Holds user's search arguments and an appropriate search URL
62+
links := make(map[string]string)
63+
64+
for _, record := range records {
65+
links[record[0]] = record[1]
66+
}
67+
68+
return links
69+
70+
}

update.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/exec"
7+
8+
aw "github.com/deanishe/awgo"
9+
)
10+
11+
// doUpdate checks for a newer version of the workflow.
12+
func doUpdate() error {
13+
log.Println("Checking for update...")
14+
return wf.CheckForUpdate()
15+
}
16+
17+
// checkForUpdate runs "./alsf update" in the background if an update check is due.
18+
func checkForUpdate() error {
19+
if !wf.UpdateCheckDue() || aw.IsRunning("update") {
20+
return nil
21+
}
22+
cmd := exec.Command(os.Args[0], "update")
23+
return aw.RunInBackground("update", cmd)
24+
}
25+
26+
// showUpdateStatus adds an "update available!" message to Script Filters if an update is available
27+
// and query is empty.
28+
func showUpdateStatus() {
29+
if query != "" {
30+
return
31+
}
32+
33+
if wf.UpdateAvailable() {
34+
wf.Configure(aw.SuppressUIDs(true))
35+
log.Println("Update available!")
36+
wf.NewItem("An update is available!").
37+
Subtitle("⇥ or ↩ to install update").
38+
Valid(false).
39+
Autocomplete("workflow:update").
40+
Icon(&aw.Icon{Value: "icons/update-available.png"})
41+
}
42+
}

workflow/alfred-ask-create-share

1.47 MB
Binary file not shown.
3.77 KB
Loading

workflow/info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<key>runningsubtext</key>
117117
<string>Loading...</string>
118118
<key>script</key>
119-
<string>./alfred-ask-create-share search "$1"</string>
119+
<string>./alfred-ask-create-share search -q "$1"</string>
120120
<key>scriptargtype</key>
121121
<integer>1</integer>
122122
<key>scriptfile</key>
@@ -225,7 +225,7 @@ Post any issues and feature requests you have there. 💜</string>
225225
</dict>
226226
</dict>
227227
<key>version</key>
228-
<string>2.6.3</string>
228+
<string>2.6.4</string>
229229
<key>webaddress</key>
230230
<string>https://github.com/nikitavoloboev/alfred-ask-create-share</string>
231231
</dict>

0 commit comments

Comments
 (0)