-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (120 loc) · 3.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-07-20
//
// alfred-forklift is a Script Filter for Alfred 3+ to open ForkLift favourites.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/update"
)
var (
// workflow configuration
repo = "deanishe/alfred-forklift"
helpURL = "https://github.com/deanishe/alfred-forklift/issues"
iconDefault = &aw.Icon{Value: "icon.png"}
iconUpdate = &aw.Icon{Value: "update-available.png"}
wf *aw.Workflow
// CLI options and workflow settings
cli *flag.FlagSet
query string
demoMode bool
doUpdate bool
ignoreLocal bool
)
func init() {
wf = aw.New(
update.GitHub(repo),
aw.HelpURL(helpURL),
)
cli = flag.NewFlagSet("forklift", flag.ExitOnError)
cli.Usage = func() {
fmt.Fprintf(os.Stderr, `forklift [options] [<query>]
Alfred 3+ workflow for searching ForkLift 3 favourites.
Usage:
forklift [-demo] [<query>]
forklift -update
forklift -h
Options:
`)
cli.PrintDefaults()
}
cli.BoolVar(&demoMode, "demo", false, "use demo data instead of real favourites")
cli.BoolVar(&doUpdate, "update", false, "check whether an update is available")
}
// run starts the workflow
func run() {
if err := cli.Parse(wf.Args()); err != nil {
wf.FatalError(err)
}
query = cli.Arg(0)
ignoreLocal = wf.Config.GetBool("IGNORE_LOCAL", false)
// Alternate actions ----------------------------------
if doUpdate {
wf.Configure(aw.TextErrors(true))
log.Printf("checking for update...")
if err := wf.CheckForUpdate(); err != nil {
wf.FatalError(err)
}
return
}
// Script Filter -------------------------------------
log.Printf("query=%q", query)
// Notify updates
if wf.UpdateCheckDue() && !wf.IsRunning("update") {
log.Printf("checking for update ...")
cmd := exec.Command(os.Args[0], "-update")
if err := wf.RunInBackground("update", cmd); err != nil {
log.Printf("[ERROR] update check failed: %v", err)
}
}
if query == "" && wf.UpdateAvailable() {
log.Printf("update available")
wf.NewItem("An update is available").
Subtitle("↩ or ⇥ to install update").
Valid(false).
Autocomplete("workflow:update").
Icon(iconUpdate)
wf.Configure(aw.SuppressUIDs(true))
}
var (
faves []Favourite
err error
)
if demoMode {
faves = demoFavourites()
} else {
faves, err = loadFavourites(favesFile)
if err != nil {
wf.Fatalf("couldn't load favourites: %v", err)
}
}
log.Printf("%d favourite(s)", len(faves))
for _, f := range faves {
log.Printf("%s (%s)", f.Name, f.Type)
wf.NewItem(f.Name).
Subtitle(f.Server).
Arg(f.UUID).
UID(f.UUID).
Match(fmt.Sprintf("%s %s", f.Name, f.Server)).
Icon(f.Icon()).
Valid(true)
}
if query != "" {
res := wf.Filter(query)
log.Printf("%d favourite(s) match %q", len(res), query)
}
wf.WarnEmpty("No favourites found", "Try a different query?")
wf.SendFeedback()
}
func main() {
wf.Run(run)
}