Skip to content

Commit

Permalink
Add Keychain support & atomically write data files
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed May 3, 2019
1 parent fb48145 commit 63c410b
Show file tree
Hide file tree
Showing 19 changed files with 447 additions and 130 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ script:
- ./run-tests.sh -ic ./...
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci; true
- $GOPATH/bin/godacov -t $CODACY_TOKEN -r ./coverage.out -c $TRAVIS_COMMIT; true

3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Compatibility:
- Add support for .alfredXworkflow files (e.g. .alfred3workflow, .alfred4workflow)
- Automatically filter releases based on Alfred version no.

Refactor:
- Embed Alfred, Feedback, MagicActions (...) into Workflow?
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c Cache) Store(name string, data []byte) error {
}
return nil
}
return ioutil.WriteFile(p, data, 0600)
return util.WriteFile(p, data, 0600)
}

// StoreJSON serialises v to JSON and saves it to the cache. If v is nil,
Expand Down
18 changes: 2 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const (
type Config struct {
Env
scripts []string
err error
}

// NewConfig creates a new Config from the environment.
Expand Down Expand Up @@ -252,24 +251,11 @@ func (cfg *Config) Unset(key string, bundleID ...string) *Config {

// Do calls Alfred and runs the accumulated actions.
//
// If an error was encountered while preparing any commands, it will be
// returned here. It also returns an error if there are no commands to run,
// or if the call to Alfred fails.
//
// Returns an error if there are no commands to run, or if the call to Alfred fails.
// Succeed or fail, any accumulated scripts and errors are cleared when Do()
// is called.
func (cfg *Config) Do() error {

var err error

if cfg.err != nil {
// reset
err, cfg.err = cfg.err, nil
cfg.scripts = []string{}

return err
}

if len(cfg.scripts) == 0 {
return errors.New("no commands to run")
}
Expand All @@ -278,7 +264,7 @@ func (cfg *Config) Do() error {
// reset
cfg.scripts = []string{}

_, err = util.RunJS(script)
_, err := util.RunJS(script)

return err
}
Expand Down
1 change: 0 additions & 1 deletion config_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ func extract(v interface{}) ([]*binding, error) {
}

if !isBindable(field.Type.Kind()) {
log.Printf("[bind] unbindable kind: %s", field.Type.Kind())
continue
}

Expand Down
14 changes: 14 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ func TestStringify(t *testing.T) {
}
}

func TestBundleID(t *testing.T) {
cfg := NewConfig()
x := "net.deanishe.awgo"
v := cfg.getBundleID()
if v != x {
t.Errorf("Bad BundleID. Expected=%q, Got=%q", x, v)
}
x = "net.deanishe.awgo2"
v = cfg.getBundleID(x)
if v != x {
t.Errorf("Bad BundleID. Expected=%q, Got=%q", x, v)
}
}

// Basic usage of Config.Get. Returns an empty string if variable is unset.
func ExampleConfig_Get() {
// Set some test variables
Expand Down
102 changes: 57 additions & 45 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,71 @@
// MIT Licence - http://opensource.org/licenses/MIT

/*
Package aw is a "plug-and-play" workflow development library/framework for Alfred 3 & 4
(https://www.alfredapp.com/).
Package aw is a "plug-and-play" workflow development library/framework for
Alfred 3 & 4 (https://www.alfredapp.com/)
It provides everything you need to create a polished and blazing-fast Alfred
frontend for your project.
It combines features for interacting with Alfred (feedback, settings,
AppleScript API) with simple APIs for common workflow functionality
(fuzzy search, caching, processes & updates) to make it easy to create
a top-shelf workflow in very little time indeed.
Features
As of AwGo 0.17, all applicable features of Alfred 4.0 are supported.
The main features are:
- Full support for Alfred 3 & 4
- Bi-directional interface to workflow's configuration
- Fluent API for generating Script Filter JSON
- Fuzzy filtering
- Simple, powerful API for caching/saving workflow data
- Keychain API to securely store (and sync) sensitive data
- API to call Alfred's AppleScript methods from Go code
- Helpers to easily run scripts and script code
- Workflow update API with built-in support for GitHub & Gitea.
- Pre-configured logging for easier debugging, with a rotated log file.
- Catches panics, logs stack trace and shows user an error message.
- "Magic" queries/actions for simplified development and user support.
- Some default icons based on macOS system icons.
Usage
AwGo is an opinionated framework that expects to be used in a certain way in
order to eliminate boilerplate. It *will* panic if not run in a valid,
minimally Alfred-like environment. At a minimum the following environment
variables should be set to meaningful values:
alfred_workflow_bundleid
alfred_workflow_cache
alfred_workflow_data
// And if you're using the update API
alfred_workflow_version
NOTE: AwGo is currently in development. The API *will* change and should
not be considered stable until v1.0. Until then, vendoring AwGo (e.g.
with dep or vgo) is strongly recommended.
// Absolutely required. No ifs or buts.
alfred_workflow_bundleid
// Cache & data dirs can be set to anything, but for best
// results, point them at the same directories as Alfred uses
// Alfred 3: ~/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/<bundle ID>/
// Alfred 4+: ~/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data/<bundle ID>/
alfred_workflow_cache
Links
// Alfred 3: ~/Library/Application Support/Alfred 3/Workflow Data/<bundle ID>/
// Alfred 4+: ~/Library/Application Support/Alfred/Workflow Data/<bundle ID>/
alfred_workflow_data
Docs: https://godoc.org/github.com/deanishe/awgo
// If you're using the Updater API, a semantic-ish workflow version
// must be set otherwise the Updater will panic
alfred_workflow_version
Source: https://github.com/deanishe/awgo
// If you're using the Alfred API and running Alfred 3, you need to
// set `alfred_version` as AwGo defaults to calling Alfred 4+
alfred_version=3
Issues: https://github.com/deanishe/awgo/issues
Licence: https://github.com/deanishe/awgo/blob/master/LICENCE
NOTE: AwGo is currently in development. The API *will* change and should
not be considered stable until v1.0. Until then, be sure to pin a version
using go modules or similar.
Be sure to also check out the _examples/ subdirectory, which contains
some simple, but complete, workflows that demonstrate the features
of AwGo and useful workflow idioms.
Features
As of AwGo 0.15, all applicable features of Alfred 4.0 are supported.
The main features are:
- Simple access to workflow settings.
- Fluent API for generating Alfred JSON.
- Fuzzy filtering.
- Simple, but powerful, API for caching/saving workflow data.
- Run scripts and script code.
- Call Alfred's AppleScript API from Go.
- Read and write workflow settings from info.plist.
- Workflow update API with built-in support for GitHub releases.
- Pre-configured logging for easier debugging, with a rotated log file.
- Catches panics, logs stack trace and shows user an error message.
- "Magic" queries/actions for simplified development and user support.
- Some default icons based on macOS system icons.
Usage
Typically, you'd call your program's main entry point via Workflow.Run().
This way, the library will rescue any panic, log the stack trace and show an
error message to the user in Alfred.
Expand Down Expand Up @@ -145,7 +147,7 @@ generate output from Run Script actions.
Be sure to set TextErrors to true to prevent Workflow from generating
Alfred JSON if it catches a panic:
wf.Configure(TextErrors(true))
wf.Configure(aw.TextErrors(true))
See ArgVars for more information.
Expand Down Expand Up @@ -291,5 +293,15 @@ Filters extremely responsive.
See _examples/update and _examples/workflows for demonstrations of this API.
Links
Docs: https://godoc.org/github.com/deanishe/awgo
Source: https://github.com/deanishe/awgo
Issues: https://github.com/deanishe/awgo/issues
Licence: https://github.com/deanishe/awgo/blob/master/LICENCE
*/
package aw
4 changes: 2 additions & 2 deletions feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (it *Item) Vars() map[string]string {
return it.vars
}

// MarshalJSON serializes Item to Alfred 3's JSON format.
// MarshalJSON serializes Item to Alfred's JSON format.
// You shouldn't need to call this directly: use SendFeedback() instead.
func (it *Item) MarshalJSON() ([]byte, error) {
var (
Expand Down Expand Up @@ -408,7 +408,7 @@ func (fb *Feedback) NewItem(title string) *Item {
return it
}

// MarshalJSON serializes Feedback to Alfred 3's JSON format.
// MarshalJSON serializes Feedback to Alfred's JSON format.
// You shouldn't need to call this: use Send() instead.
func (fb *Feedback) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/deanishe/awgo
go 1.12

require (
github.com/pkg/errors v0.8.1
golang.org/x/text v0.3.0
howett.net/plist v0.0.0-20181124034731-591f970eefbb
)
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
howett.net/plist v0.0.0-20181124034731-591f970eefbb h1:jhnBjNi9UFpfpl8YZhA9CrOqpnJdvzuiHsl/dnxl11M=
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
Loading

0 comments on commit 63c410b

Please sign in to comment.