Skip to content

Commit

Permalink
added update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoval committed Jul 27, 2020
1 parent c87bd1e commit 0faacbd
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 13 deletions.
3 changes: 2 additions & 1 deletion env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash

export alfred_workflow_bundleid="com.ryankoval.awsconsoleservices"
export alfred_version=
export alfred_version=3.8
export alfred_workflow_version=1.0

if [ "$(uname)" == "Darwin" ] && [ -z "$TEST" ]; then
# make this mirror where alfred stores cache/data
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ require (
github.com/cheekybits/genny v1.0.0
github.com/deanishe/awgo v0.25.0
github.com/dnaeon/go-vcr v1.0.1
github.com/go-test/deep v1.0.7
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v2 v2.2.2
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ github.com/dnaeon/go-vcr v1.0.1 h1:r8L/HqC0Hje5AXMu1ooW8oyQyOFv4GxqpL0nRP7SLLY=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
Expand Down
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ To add entries to the Alfred Workflow, modify [the .yml config file](src/console
<string>true</string>
</dict>
<key>version</key>
<string>3.x</string>
<string>3.0</string>
<key>webaddress</key>
<string>https://ryankoval.com</string>
</dict>
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/update"
"github.com/rkoval/alfred-aws-console-services-workflow/awsworkflow"
"github.com/rkoval/alfred-aws-console-services-workflow/workflow"
)
Expand All @@ -22,7 +23,7 @@ func init() {
flag.StringVar(&query, "query", "", "query to use")
flag.StringVar(&ymlPath, "yml_path", "console-services.yml", "query to use")
flag.Parse()
wf = aw.New()
wf = aw.New(update.GitHub("rkoval/alfred-aws-console-services-workflow"))
}

func main() {
Expand Down
57 changes: 57 additions & 0 deletions tests/mock_alfred_updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tests

import (
"errors"
"time"

aw "github.com/deanishe/awgo"
)

// ensure MockAlfredUpdater implements Updater
var _ aw.Updater = (*MockAlfredUpdater)(nil)

type MockAlfredUpdater struct {
updateIntervalCalled bool
checkDueCalled bool
checkForUpdateCalled bool
updateAvailableCalled bool
installCalled bool

checkShouldFail bool
installShouldFail bool
}

// UpdateInterval implements Updater.
func (d *MockAlfredUpdater) UpdateInterval(_ time.Duration) {
d.updateIntervalCalled = true
}

// UpdateAvailable implements Updater.
func (d *MockAlfredUpdater) UpdateAvailable() bool {
d.updateAvailableCalled = true
return true
}

// CheckDue implements Updater.
func (d *MockAlfredUpdater) CheckDue() bool {
d.checkDueCalled = true
return true
}

// CheckForUpdate implements Updater.
func (d *MockAlfredUpdater) CheckForUpdate() error {
d.checkForUpdateCalled = true
if d.checkShouldFail {
return errors.New("check failed")
}
return nil
}

// Install implements Updater.
func (d *MockAlfredUpdater) Install() error {
d.installCalled = true
if d.installShouldFail {
return errors.New("install failed")
}
return nil
}
35 changes: 34 additions & 1 deletion workflow/.snapshots/TestRun-#00
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]*aw.Item) (len=1) {
([]*aw.Item) (len=2) {
(*aw.Item)({
title: (string) (len=29) "Search for an AWS Service ...",
subtitle: (*string)((len=33) "e.g., cloudformation, ec2, s3 ..."),
Expand All @@ -16,5 +16,38 @@
mods: (map[aw.ModKey]*aw.Modifier) <nil>,
icon: (*aw.Icon)(<nil>),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=16) "Update available",
subtitle: (*string)((len=42) "Select this result to navigate to download"),
match: (*string)(<nil>),
uid: (*string)(<nil>),
autocomplete: (*string)(<nil>),
arg: (*string)((len=71) "https://github.com/rkoval/alfred-aws-console-services-workflow/releases"),
valid: (bool) true,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) (len=1) {
(string) (len=6) "action": (string) (len=8) "open-url"
},
mods: (map[aw.ModKey]*aw.Modifier) (len=1) {
(aw.ModKey) (len=3) "cmd": (*aw.Modifier)({
Key: (aw.ModKey) (len=3) "cmd",
arg: (*string)(<nil>),
subtitle: (*string)((len=21) "Copy URL to clipboard"),
valid: (bool) false,
icon: (*aw.Icon)(<nil>),
vars: (map[string]string) (len=1) {
(string) (len=6) "action": (string) (len=17) "copy-to-clipboard"
}
})
},
icon: (*aw.Icon)({
Value: (string) (len=81) "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns",
Type: (aw.IconType) ""
}),
noUID: (bool) false
})
}
35 changes: 34 additions & 1 deletion workflow/.snapshots/TestRun-_
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]*aw.Item) (len=1) {
([]*aw.Item) (len=2) {
(*aw.Item)({
title: (string) (len=29) "Search for an AWS Service ...",
subtitle: (*string)((len=33) "e.g., cloudformation, ec2, s3 ..."),
Expand All @@ -16,5 +16,38 @@
mods: (map[aw.ModKey]*aw.Modifier) <nil>,
icon: (*aw.Icon)(<nil>),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=16) "Update available",
subtitle: (*string)((len=42) "Select this result to navigate to download"),
match: (*string)(<nil>),
uid: (*string)(<nil>),
autocomplete: (*string)(<nil>),
arg: (*string)((len=71) "https://github.com/rkoval/alfred-aws-console-services-workflow/releases"),
valid: (bool) true,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) (len=1) {
(string) (len=6) "action": (string) (len=8) "open-url"
},
mods: (map[aw.ModKey]*aw.Modifier) (len=1) {
(aw.ModKey) (len=3) "cmd": (*aw.Modifier)({
Key: (aw.ModKey) (len=3) "cmd",
arg: (*string)(<nil>),
subtitle: (*string)((len=21) "Copy URL to clipboard"),
valid: (bool) false,
icon: (*aw.Icon)(<nil>),
vars: (map[string]string) (len=1) {
(string) (len=6) "action": (string) (len=17) "copy-to-clipboard"
}
})
},
icon: (*aw.Icon)({
Value: (string) (len=81) "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns",
Type: (aw.IconType) ""
}),
noUID: (bool) false
})
}
16 changes: 16 additions & 0 deletions workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"github.com/rkoval/alfred-aws-console-services-workflow/parsers"
"github.com/rkoval/alfred-aws-console-services-workflow/searchers"
"github.com/rkoval/alfred-aws-console-services-workflow/searchtypes"
"github.com/rkoval/alfred-aws-console-services-workflow/util"
)

func Run(wf *aw.Workflow, query string, session *session.Session, forceFetch, openAll bool, ymlPath string) {
log.Println("using workflow cacheDir: " + wf.CacheDir())
log.Println("using workflow dataDir: " + wf.DataDir())
awsServices := parsers.ParseConsoleServicesYml(ymlPath)
fullQuery := query
query, searchType, awsService, promptOpenAll := parsers.ParseQuery(awsServices, query)
Expand All @@ -40,8 +43,21 @@ func Run(wf *aw.Workflow, query string, session *session.Session, forceFetch, op

var err error
if searchType == searchtypes.None {
log.Println("no search type parsed")
wf.NewItem("Search for an AWS Service ...").
Subtitle("e.g., cloudformation, ec2, s3 ...")

if wf.UpdateCheckDue() {
if err := wf.CheckForUpdate(); err != nil {
wf.FatalError(err)
}
}
if wf.UpdateAvailable() {
util.NewURLItem(wf, "Update available").
Subtitle("Select this result to navigate to download").
Arg("https://github.com/rkoval/alfred-aws-console-services-workflow/releases").
Icon(aw.IconInfo)
}
} else if searchType == searchtypes.Services {
log.Println("using searcher associated with services")
if awsService == nil {
Expand Down
10 changes: 5 additions & 5 deletions workflow/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/bradleyjkemp/cupaloy"
aw "github.com/deanishe/awgo"
"github.com/go-test/deep"
"github.com/rkoval/alfred-aws-console-services-workflow/tests"
"github.com/stretchr/testify/assert"
)

type testCase struct {
Expand Down Expand Up @@ -150,7 +150,9 @@ var tcs []testCase = []testCase{
}

func testWorkflow(t *testing.T, tc testCase, forceFetch, snapshot bool) []*aw.Item {
wf := aw.New()
updater := &tests.MockAlfredUpdater{}
wf := aw.New(aw.Update(updater))

session, r := tests.NewAWSRecorderSession(tc.fixtureName)
defer tests.PanicOnError(r.Stop)
Run(wf, tc.query, session, forceFetch, false, "../console-services.yml")
Expand Down Expand Up @@ -180,9 +182,7 @@ func TestRunWithCache(t *testing.T) {
t.Run(tc.query+"_cached", func(t *testing.T) {
fetchedItems := testWorkflow(t, tc, true, false)
cachedItems := testWorkflow(t, tc, false, false)
if diff := deep.Equal(fetchedItems, cachedItems); diff != nil {
t.Error(diff)
}
assert.Equal(t, cachedItems, fetchedItems)
})
}
}

0 comments on commit 0faacbd

Please sign in to comment.