|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/csv" |
5 | | - "log" |
6 | | - "os" |
7 | | - "os/exec" |
8 | | - "strings" |
| 4 | + "fmt" |
9 | 5 |
|
| 6 | + "github.com/alecthomas/kingpin" |
10 | 7 | "github.com/deanishe/awgo" |
11 | 8 | "github.com/deanishe/awgo/update" |
12 | | - "github.com/docopt/docopt-go" |
13 | 9 | ) |
14 | 10 |
|
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 | +) |
30 | 15 |
|
| 16 | +// Icons |
31 | 17 | var ( |
32 | | - // Icons |
33 | 18 | iconAvailable = &aw.Icon{Value: "icons/update.png"} |
34 | 19 | redditIcon = &aw.Icon{Value: "icons/reddit.png"} |
35 | 20 | githubIcon = &aw.Icon{Value: "icons/github.png"} |
36 | 21 | forumsIcon = &aw.Icon{Value: "icons/forums.png"} |
37 | 22 | stackIcon = &aw.Icon{Value: "icons/stack.png"} |
38 | 23 | docIcon = &aw.Icon{Value: "icons/doc.png"} |
39 | | - |
40 | | - repo = "nikitavoloboev/alfred-ask-create-share" |
41 | | - wf *aw.Workflow |
42 | 24 | ) |
43 | 25 |
|
44 | | -func init() { |
45 | | - wf = aw.New(update.GitHub(repo)) |
46 | | -} |
| 26 | +var ( |
| 27 | + // Kingpin and script options |
| 28 | + app *kingpin.Application |
47 | 29 |
|
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 |
62 | 34 |
|
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 |
68 | 37 |
|
69 | | - log.Printf("query=%s", query) |
| 38 | + repo = "nikitavoloboev/alfred-ask-create-share" |
70 | 39 |
|
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 | +) |
80 | 43 |
|
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")) |
91 | 47 |
|
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.") |
109 | 49 |
|
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") |
113 | 55 |
|
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 | + } |
116 | 62 | } |
117 | 63 |
|
118 | | -// parseCSV parses CSV for links and arguments |
119 | | -func parseCSV() map[string]string { |
| 64 | +func run() { |
120 | 65 | var err error |
121 | 66 |
|
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()) |
124 | 68 | if err != nil { |
125 | | - panic(err) |
| 69 | + wf.FatalError(err) |
126 | 70 | } |
127 | | - defer f.Close() |
128 | | - |
129 | | - r := csv.NewReader(f) |
130 | 71 |
|
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) |
134 | 79 | } |
135 | 80 |
|
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() |
141 | 84 | } |
142 | 85 |
|
143 | | - return links |
144 | | - |
| 86 | + if err != nil { |
| 87 | + wf.FatalError(err) |
| 88 | + } |
145 | 89 | } |
146 | 90 |
|
147 | 91 | func main() { |
|
0 commit comments