Skip to content

Commit

Permalink
Add metadata.json updater
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jun 9, 2019
1 parent e6f9b36 commit 0cc2f41
Show file tree
Hide file tree
Showing 19 changed files with 1,632 additions and 1,002 deletions.
6 changes: 5 additions & 1 deletion _examples/update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"os"
"os/exec"

"github.com/deanishe/awgo"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/update"
)

Expand All @@ -61,6 +61,8 @@ var (
iconAvailable = &aw.Icon{Value: "update-available.png"}
repo = "deanishe/alfred-ssh" // GitHub repo
wf *aw.Workflow // Our Workflow struct
// URL of Alfred metadata.json file to use update.Metadata updater.
// metadataURL = "https://raw.githubusercontent.com/deanishe/alfred-ssh/master/metadata.json"

// Fake data for Script Filter
items = []string{
Expand Down Expand Up @@ -112,6 +114,8 @@ func init() {
flag.BoolVar(&doCheck, "check", false, "check for a new version")

wf = aw.New(update.GitHub(repo))
// To user metadata.json updater
// wf = aw.New(update.Metadata(metaDataURL))
}

func run() {
Expand Down
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ Updates
AwGo can check for and install new versions of your workflow.
Subpackage update provides an implementation of the Updater interface and
sources to load updates from GitHub or Gitea releases.
sources to load updates from GitHub or Gitea releases, or from the URL of
an Alfred `metadata.json` file.
See subpackage update and _examples/update.
Expand Down
24 changes: 11 additions & 13 deletions update/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
// MIT Licence - http://opensource.org/licenses/MIT

/*
Package update implements an API for fetching updates to workflows from remote servers.
Package update implements an API for fetching workflow updates from remote servers.
The Updater/Releaser API provides the ability to check for newer versions
of your workflow. Support for updating from GitHub and Gitea releases is built in.
See GitHub example.
It is the "backend" for aw.Workflow's update API, and provides concrete updaters for
GitHub and Gitea releases, and Alfred metadata.json files (as aw.Options). Updater
implements aw.Updater and you can create a custom Updater to use with
aw.Workflow/aw.Update() by passing a custom implementation of Source to NewUpdater().
You can use your own backend by implementing the Releaser interface.
The only hard requirement is support for (mostly) semantic version numbers. See
SemVer documentation and http://semver.org for details.
The only hard requirement is support for (mostly) semantic version numbers.
See SemVer documentation and http://semver.org for details.
Updater is also Alfred-version-aware, and ignores incompatible workflow version,
e.g. workflow files with the extension ".alfred4workflow" are ignored
when Updater is run in Alfred 3.
This package is the "backend". You should set an Updater on an aw.Workflow
struct (using e.g. the GitHub aw.Option) and use the Workflow methods
CheckForUpdate(), UpdateAvailable() and InstallUpdate() to interact with
the Updater.
See ../_examples/update for one possible way to use this API.
See ../_examples/update for one possible way to using the updater API.
*/
package update
2 changes: 1 addition & 1 deletion update/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// Gitea is a Workflow Option. It sets a Workflow Updater for the specified Gitea repo.
// Repo name should be the full URL of the repo, e.g. "git.deanishe.net/deanishe/alfred-ssh".
// Repo name should be the URL of the repo, e.g. "git.deanishe.net/deanishe/alfred-ssh".
func Gitea(repo string) aw.Option {
return func(wf *aw.Workflow) aw.Option {
u, _ := NewUpdater(&giteaSource{Repo: repo, fetch: getURL},
Expand Down
27 changes: 14 additions & 13 deletions update/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package update

import (
"fmt"
"io/ioutil"
"log"
"reflect"
"testing"

aw "github.com/deanishe/awgo"
)

// 6 valid releases, including one prerelease
// v1.0, v2.0, v6.0, v7.1.0-beta, v9.0 (Alfred 4+ only), v10.0-beta
var testGiteaDownloads = []Download{
// Latest version for Alfred 4
Download{
Expand Down Expand Up @@ -70,21 +73,25 @@ var testGiteaDownloads = []Download{
func TestParseGitea(t *testing.T) {
t.Parallel()

var (
data = mustRead("testdata/gitea-releases.json")
dls []Download
err error
)

src := &giteaSource{
Repo: "deanishe/alfred-workflow-dummy",
fetch: func(URL string) ([]byte, error) {
return []byte(giteaReleasesEmptyJSON), nil
return ioutil.ReadFile("testdata/empty.json")
}}
dls, err := src.Downloads()
if err != nil {
if dls, err = src.Downloads(); err != nil {
t.Fatal("parse empty JSON")
}
if len(dls) != 0 {
t.Fatal("releases in empty JSON")
}

dls, err = parseGiteaReleases([]byte(giteaReleasesJSON))
if err != nil {
if dls, err = parseGiteaReleases(data); err != nil {
t.Fatal("parse Gitea JSON.")
}
if len(dls) != len(testGiteaDownloads) {
Expand All @@ -101,7 +108,8 @@ func TestParseGitea(t *testing.T) {

func makeGiteaSource() *giteaSource {
src := &giteaSource{Repo: "git.deanishe.net/deanishe/nonexistent"}
dls, err := parseGiteaReleases([]byte(giteaReleasesJSON))

dls, err := parseGiteaReleases(mustRead("testdata/gitea-releases.json"))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -201,10 +209,3 @@ func ExampleGitea() {
// Output:
// true
}

var (
giteaReleasesEmptyJSON = `[]`
// 6 valid releases, including one prerelease
// v1.0, v2.0, v6.0, v7.1.0-beta, v9.0 (Alfred 4+ only), v10.0-beta
giteaReleasesJSON = `[{"id":64612,"tag_name":"v10.0-beta","target_commitish":"master","name":"Latest release (pre-release)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/64612","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v10.0-beta.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v10.0-beta.zip","draft":false,"prerelease":true,"created_at":"2019-05-03T12:27:30Z","published_at":"2019-05-03T12:27:30Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":20,"name":"Dummy-10.0-beta.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-05-03T14:46:11Z","uuid":"8c1b2442-eba2-4740-91b3-c57dab219096","browser_download_url":"https://git.deanishe.net/attachments/8c1b2442-eba2-4740-91b3-c57dab219096"}]},{"id":64613,"tag_name":"v9.0","target_commitish":"master","name":"Latest release (Alfred 4)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/64613","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v9.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v9.0.zip","draft":false,"prerelease":false,"created_at":"2019-05-03T12:24:12Z","published_at":"2019-05-03T12:24:12Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":19,"name":"Dummy-9.0.alfred4workflow","size":36063,"download_count":0,"created_at":"2019-05-03T14:45:38Z","uuid":"acd4dc64-1c85-4d23-b053-711bb4f976c5","browser_download_url":"https://git.deanishe.net/attachments/acd4dc64-1c85-4d23-b053-711bb4f976c5"}]},{"id":61642,"tag_name":"v8point0","target_commitish":"master","name":"Invalid tag (non-semantic)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61642","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v8point0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v8point0.zip","draft":false,"prerelease":false,"created_at":"2018-12-07T16:03:23Z","published_at":"2018-12-07T16:03:23Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":18,"name":"Dummy-eight.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-04-06T19:03:54Z","uuid":"6b3e403f-4151-4f59-8956-c4a848f36d4b","browser_download_url":"https://git.deanishe.net/attachments/6b3e403f-4151-4f59-8956-c4a848f36d4b"}]},{"id":61643,"tag_name":"v7.1.0-beta","target_commitish":"master","name":"Invalid release (pre-release status)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61643","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v7.1.0-beta.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v7.1.0-beta.zip","draft":false,"prerelease":true,"created_at":"2014-10-10T10:58:14Z","published_at":"2014-10-10T10:58:14Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":17,"name":"Dummy-7.1-beta.alfredworkflow","size":35726,"download_count":0,"created_at":"2019-04-06T19:03:20Z","uuid":"36d70923-d65d-4670-a1c1-adb5d6980b0c","browser_download_url":"https://git.deanishe.net/attachments/36d70923-d65d-4670-a1c1-adb5d6980b0c"}]},{"id":61645,"tag_name":"v7.0","target_commitish":"master","name":"Invalid release (contains no files)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61645","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v7.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v7.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T19:25:55Z","published_at":"2014-09-14T19:25:55Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[]},{"id":61646,"tag_name":"v6.0","target_commitish":"master","name":"Latest valid release","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61646","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v6.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v6.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T19:24:41Z","published_at":"2014-09-14T19:24:41Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":14,"name":"Dummy-6.0.zip","size":36063,"download_count":0,"created_at":"2019-04-06T19:01:30Z","uuid":"683e09ce-5643-456b-82ab-9bd6d8d1bbb8","browser_download_url":"https://git.deanishe.net/attachments/683e09ce-5643-456b-82ab-9bd6d8d1bbb8"},{"id":15,"name":"Dummy-6.0.alfred3workflow","size":36063,"download_count":0,"created_at":"2019-04-06T19:01:30Z","uuid":"eb86751a-7f31-49f0-be4c-1dd1e0557c9d","browser_download_url":"https://git.deanishe.net/attachments/eb86751a-7f31-49f0-be4c-1dd1e0557c9d"},{"id":16,"name":"Dummy-6.0.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-04-06T19:01:30Z","uuid":"61aa34a1-1877-4a41-ae50-01c18c8e2598","browser_download_url":"https://git.deanishe.net/attachments/61aa34a1-1877-4a41-ae50-01c18c8e2598"},{"id":21,"name":"Dummy-6.0.alfred4workflow","size":36063,"download_count":0,"created_at":"2019-05-03T19:28:16Z","uuid":"13392981-721e-4880-b2a9-aad50225d0af","browser_download_url":"https://git.deanishe.net/attachments/13392981-721e-4880-b2a9-aad50225d0af"}]},{"id":61647,"tag_name":"v5.0","target_commitish":"master","name":"Invalid release (contains no files)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61647","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v5.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v5.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T19:22:44Z","published_at":"2014-09-14T19:22:44Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[]},{"id":61648,"tag_name":"v4.0","target_commitish":"master","name":"Invalid release (contains 2 .alfredworkflow files)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61648","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v4.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v4.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T16:34:44Z","published_at":"2014-09-14T16:34:44Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":12,"name":"Dummy-4.0.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-04-06T19:00:21Z","uuid":"d13764be-c63a-4435-9104-e0df7e1b62c5","browser_download_url":"https://git.deanishe.net/attachments/d13764be-c63a-4435-9104-e0df7e1b62c5"},{"id":13,"name":"Dummy-4.1.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-04-06T19:00:21Z","uuid":"75d6eadf-922e-4179-a179-af703e18f4f6","browser_download_url":"https://git.deanishe.net/attachments/75d6eadf-922e-4179-a179-af703e18f4f6"}]},{"id":61649,"tag_name":"v3.0","target_commitish":"master","name":"Invalid release (no .alfredworkflow file)","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61649","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v3.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v3.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T16:34:16Z","published_at":"2014-09-14T16:34:16Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":11,"name":"Dummy-3.0.zip","size":36063,"download_count":0,"created_at":"2019-04-06T18:59:37Z","uuid":"d6e88cc4-1f2b-4cb2-9749-deb5f6a16e0e","browser_download_url":"https://git.deanishe.net/attachments/d6e88cc4-1f2b-4cb2-9749-deb5f6a16e0e"}]},{"id":61650,"tag_name":"v2.0","target_commitish":"master","name":"v2.0","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61650","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v2.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v2.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T16:33:36Z","published_at":"2014-09-14T16:33:36Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":10,"name":"Dummy-2.0.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-04-06T18:59:08Z","uuid":"03a01b52-93bc-48f0-9b09-37ba212a03fd","browser_download_url":"https://git.deanishe.net/attachments/03a01b52-93bc-48f0-9b09-37ba212a03fd"}]},{"id":61651,"tag_name":"v1.0","target_commitish":"master","name":"v1.0","body":"","url":"https://git.deanishe.net/api/v1/deanishe/alfred-workflow-dummy/releases/61651","tarball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v1.0.tar.gz","zipball_url":"https://git.deanishe.net/deanishe/alfred-workflow-dummy/archive/v1.0.zip","draft":false,"prerelease":false,"created_at":"2014-09-14T16:33:06Z","published_at":"2014-09-14T16:33:06Z","author":{"id":1,"login":"deanishe","full_name":"","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/f8a47e9dc5393dabf96054d4abb76478?d=identicon","language":"en-US","is_admin":true,"username":"deanishe"},"assets":[{"id":9,"name":"Dummy-1.0.alfredworkflow","size":36063,"download_count":0,"created_at":"2019-04-06T18:58:02Z","uuid":"d71ad702-cfce-46ba-aa26-2096d34ff97b","browser_download_url":"https://git.deanishe.net/attachments/d71ad702-cfce-46ba-aa26-2096d34ff97b"}]}]`
)
Loading

0 comments on commit 0cc2f41

Please sign in to comment.