@@ -2,74 +2,111 @@ package main
22
33import (
44 "encoding/csv"
5- "fmt"
65 "log"
76 "os"
7+ "os/exec"
88
99 "git.deanishe.net/deanishe/awgo"
1010 "git.deanishe.net/deanishe/awgo/update"
11- "gopkg.in/alecthomas/kingpin.v2 "
11+ "github.com/docopt/docopt-go "
1212)
1313
14- // name of the background job that checks for updates
14+ // Name of the background job that checks for updates
1515const updateJobName = "checkForUpdate"
1616
17- var (
18- // kingpin and script
19- app * kingpin.Application
20-
21- // app commands
22- filterSubmissionsCmd * kingpin.CmdClause
17+ var usage = `alfred-ask-create-share [search|check] [<query>]
2318
24- // script options
25- query string
19+ Open web submissions from Alfred
2620
27- // icons
28- // redditIcon = &aw.Icon{Value: "icons/reddit.png"}
29- // docIcon = &aw.Icon{Value: "icons/doc.png"}
30- // gitubIcon = &aw.Icon{Value: "icons/github.png"}
31- // forumsIcon = &aw.Icon{Value: "icons/forums.png"}
32- // translateIcon = &aw.Icon{Value: "icons/translate.png"}
33- // stackIcon = &aw.Icon{Value: "icons/stack.png"}
34- // iconAvailable = &aw.Icon{Value: "icons/update-available.png"}
21+ Usage:
22+ alfred-ask-create-share search [<query>]
23+ alfred-ask-create-share check
24+ alfred-ask-create-share -h
3525
36- repo = "nikitavoloboev/alfred-ask-create-share"
26+ Options:
27+ -h, --help Show this message and exit.
28+ `
3729
38- // workflow
39- wf * aw.Workflow
30+ var (
31+ iconAvailable = & aw.Icon {Value : "icons/update.png" }
32+ repo = "nikitavoloboev/alfred-ask-create-share"
33+ wf * aw.Workflow
4034)
4135
4236func init () {
4337 wf = aw .New (update .GitHub (repo ))
38+ }
4439
45- app = kingpin .New ("ask-create-share" , "make web submissions from Alfred" )
46- // app.HelpFlag.Short('h')
47- app .Version (wf .Version ())
48-
49- filterSubmissionsCmd = app .Command ("filter" , "filters submissions" )
40+ func run () {
41+ // log.Println(wf.Args())
42+
43+ // Pass wf.Args() to docopt because our update logic relies on
44+ // AwGo's magic actions.
45+ args , _ := docopt .Parse (usage , wf .Args (), true , wf .Version (), false , true )
46+
47+ // alternate action: get available releases from remote
48+ if args ["check" ] != false {
49+ wf .TextErrors = true
50+ log .Println ("checking for updates..." )
51+ if err := wf .CheckForUpdate (); err != nil {
52+ wf .FatalError (err )
53+ }
54+ return
55+ }
5056
51- for _ , cmd := range []* kingpin.CmdClause {filterSubmissionsCmd } {
52- cmd .Flag ("query" , "search query" ).Short ('q' ).StringVar (& query )
57+ // _script filter
58+ var query string
59+ if args ["<query>" ] != nil {
60+ query = args ["<query>" ].(string )
5361 }
5462
55- // list action commands
56- app .DefaultEnvars ()
57- }
63+ log .Printf ("query=%s" , query )
5864
59- // _actions
65+ // call self with "check" command if an update is due and a
66+ // check job isn't already running.
67+ if wf .UpdateCheckDue () && ! aw .IsRunning (updateJobName ) {
68+ log .Println ("running update check in background..." )
69+ cmd := exec .Command ("./alfred-ask-create-share" , "check" )
70+ if err := aw .RunInBackground (updateJobName , cmd ); err != nil {
71+ log .Printf ("error starting update check: %s" , err )
72+ }
73+ }
6074
61- // fills Alfred with hash map values and shows keys
62- func filterResults (links map [string ]string ) {
75+ if query == "" { // Only show update status if query is empty
76+ // Send update status to Alfred
77+ if wf .UpdateAvailable () {
78+ wf .NewItem ("update available!" ).
79+ Subtitle ("↩ to install" ).
80+ Autocomplete ("workflow:update" ).
81+ Valid (false ).
82+ Icon (iconAvailable )
83+ }
84+ }
85+
86+ links := parseCSV ()
6387
6488 for key , value := range links {
6589 wf .NewItem (key ).Valid (true ).UID (key ).Var ("URL" , value ).Var ("ARG" , key )
6690 }
6791
68- wf .Filter (query )
92+ // script filter results
93+ // for i := 1; i <= 20; i++ {
94+ // t := fmt.Sprintf("Item #%d", i)
95+ // wf.NewItem(t).
96+ // Icon(aw.IconFavourite).
97+ // Arg(t).
98+ // Valid(true)
99+ // }
100+
101+ if query != "" {
102+ wf .Filter (query )
103+ }
104+
105+ wf .WarnEmpty ("no matching items" , "try a different query?" )
69106 wf .SendFeedback ()
70107}
71108
72- func run () {
109+ func parseCSV () map [ string ] string {
73110 var err error
74111
75112 // load values from file to a hash map
@@ -93,24 +130,17 @@ func run() {
93130 links [record [0 ]] = record [1 ]
94131 }
95132
96- // _arg parsing
97- cmd , err := app .Parse (wf .Args ())
98- if err != nil {
99- wf .FatalError (err )
100- }
133+ return links
101134
102- switch cmd {
103- case filterSubmissionsCmd .FullCommand ():
104- filterResults (links )
105- default :
106- err = fmt .Errorf ("unknown command: %s" , cmd )
107- }
135+ }
108136
109- if err != nil {
110- wf .FatalError (err )
111- }
137+ // fills Alfred with hash map values and shows keys
138+ func filterResults (links map [string ]string ) {
139+
140+ // wf.Filter(query)
141+ // wf.SendFeedback()
112142}
113143
114144func main () {
115- aw .Run (run )
145+ wf .Run (run )
116146}
0 commit comments