Skip to content

Commit

Permalink
Merge pull request #4 from atoftegaard-git/feature/add-auto-update
Browse files Browse the repository at this point in the history
Add auto update function
  • Loading branch information
atoftegaard-git authored Nov 10, 2023
2 parents 50119c6 + b67b1b1 commit b2d339a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "flag"

var (
opts = &options{}
cli = flag.NewFlagSet("alfred-toggl-jira", flag.ContinueOnError)
)

type options struct {
Update bool
}

func init() {
cli.BoolVar(&opts.Update, "update", false, "check for updates")
}
33 changes: 32 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
"github.com/deanishe/awgo/update"
"github.com/ncruces/zenity"
)

Expand All @@ -27,7 +28,9 @@ type CurrentTogglTrack struct {
}

const (
repo = "atoftegaard/alfred-toggl-jira"
keychainAccount = "alfred-jira-toggl"
updateJobName = "checkForUpdates"
)

var (
Expand All @@ -45,7 +48,9 @@ var (
)

func init() {
wf = aw.New()
wf = aw.New(
update.GitHub(repo),
)
flag.BoolVar(&clearAuthFlag, "clear-auth", false, "clear toggl api token from keychain")
flag.BoolVar(&authFlag, "auth", false, "adds toggl api token to keychain")
flag.BoolVar(&stopTogglEntryFlag, "stop-entry", false, "stops current toggl entry")
Expand Down Expand Up @@ -75,6 +80,32 @@ func run() {
wf.Args()
flag.Parse()

if opts.Update {
wf.Configure(aw.TextErrors(true))
log.Println("Checking for updates...")
if err := wf.CheckForUpdate(); err != nil {
wf.FatalError(err)
}
return
}

if wf.UpdateCheckDue() && !wf.IsRunning(updateJobName) {
log.Println("Running update check in background...")
cmd := exec.Command(os.Args[0], "--update")
if err := wf.RunInBackground(updateJobName, cmd); err != nil {
log.Printf("Error starting update check: %s", err)
}
}

if wf.UpdateAvailable() {
wf.Configure(aw.SuppressUIDs(true))
wf.NewItem("Update Available!").
Subtitle("Press ⏎ to install").
Autocomplete("workflow:update").
Valid(false).
Icon(aw.IconInfo)
}

if clearAuthFlag {
err := wf.Keychain.Delete(keychainAccount)

Expand Down
2 changes: 1 addition & 1 deletion workflow/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ To start tracking with this workflow, you'll need a workspace ID, this can be fo
</dict>
</array>
<key>version</key>
<string>1.7.3</string>
<string>1.7.4</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down

0 comments on commit b2d339a

Please sign in to comment.