Skip to content

Commit

Permalink
Add workflow updating
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 20, 2017
1 parent a2b2fa5 commit 914590e
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 38 deletions.
Binary file added ForkLift-Favourites-0.1.1.alfredworkflow
Binary file not shown.
Binary file removed ForkLift-Favourites-0.1.alfredworkflow
Binary file not shown.
1 change: 1 addition & 0 deletions build-workflow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ log "copying assets to ./build ..."
mkdir -vp ./build

cp -v icon.png ./build/
cp -v update-available.png ./build/
cp -v info.plist ./build/
cp -v README.md ./build/
cp -v LICENCE.txt ./build/
Expand Down
121 changes: 84 additions & 37 deletions forklift.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,31 @@ Filter ForkLift favourites in Alfred 3.
Usage:
forklift [<query>]
forklift --help | --version
forklift --datadir | --cachedir | --distname | --logfile
forklift --distname
forklift --logfile
forklift --update
Options:
--datadir Print path to workflow's data directory and exit.
--cachedir Print path to workflow's cache directory and exit.
--logfile Print path to workflow's log file and exit.
--distname Print filename of distributable .alfredworkflow file
(for the build script).
-h, --help Show this message and exit.
--logfile Print path to workflow's log file and exit.
-u, --update Check if an update is available.
`
favesFile = os.ExpandEnv("$HOME/Library/Application Support/ForkLift/Favorites/Favorites.json")
)

var (
connectionTypes = []string{"Local", "SFTP", "NFS", "Rackspace", "S3", "Search", "FTP", "Sync", "VNC", "WebDAV", "Workspace"}
iconDefault = &aw.Icon{Value: "icon.png", Type: aw.IconTypeImageFile}
connectionIcons map[string]*aw.Icon
iconDefault = &aw.Icon{Value: "icon.png", Type: aw.IconTypeImageFile}
iconUpdate = &aw.Icon{Value: "update-available.png", Type: aw.IconTypeImageFile}
favesFile = os.ExpandEnv("$HOME/Library/Application Support/ForkLift/Favorites/Favorites.json")
// workflow configuration
repo = "deanishe/alfred-forklift"
wf *aw.Workflow
// CLI options
query string
doLogfile bool
doDist bool
doUpdate bool
)

func init() {
Expand All @@ -61,6 +68,7 @@ func init() {
path := fmt.Sprintf("/Applications/ForkLift.app/Contents/Resources/Connection%s.icns", s)
connectionIcons[s] = &aw.Icon{Value: path, Type: aw.IconTypeImageFile}
}
wf = aw.NewWorkflow(&aw.Options{GitHub: repo})
}

type faves struct {
Expand Down Expand Up @@ -160,62 +168,101 @@ func loadFavourites(path string) ([]*Favourite, error) {
return favourites, nil
}

// run starts the workflow
func run() {
var query string

vstr := fmt.Sprintf("%s/%v (awgo/%v)", aw.Name(), aw.Version(), aw.AwGoVersion)
args, err := docopt.Parse(usage, nil, true, vstr, false)
func parseArgs() error {
vstr := fmt.Sprintf("%s/%v (awgo/%v)", wf.Name(), wf.Version(), aw.AwGoVersion)
args, err := docopt.Parse(usage, wf.Args(), true, vstr, false)
if err != nil {
log.Fatalf("error parsing CLI options: %s", err)
return err
}

log.Printf("args=%+v", args)

// ================ Alternate actions ====================
if args["--logfile"] == true {
doLogfile = true
}

if args["--datadir"] == true {
fmt.Println(aw.DataDir())
return
if args["--distname"] == true {
doDist = true
}

if args["--cachedir"] == true {
fmt.Println(aw.CacheDir())
return
if args["--update"] == true {
doUpdate = true
}

if args["--logfile"] == true {
fmt.Println(aw.LogFile())
if args["<query>"] != nil {
query = args["<query>"].(string)
}
return nil
}

// run starts the workflow
func run() {
// ================ Alternate actions ====================

if err := parseArgs(); err != nil {
panic(fmt.Sprintf("error parsing arguments: %s", err))
}

if doLogfile == true {
fmt.Println(wf.LogFile())
return
}

if args["--distname"] == true {
if doDist == true {
name := strings.Replace(
fmt.Sprintf("%s-%s.alfredworkflow", aw.Name(), aw.Version()),
fmt.Sprintf("%s-%s.alfredworkflow", wf.Name(), wf.Version()),
" ", "-", -1)
fmt.Println(name)
return
}

if doUpdate == true {
wf.TextErrors = true
log.Printf("checking for update...")
if err := wf.CheckForUpdate(); err != nil {
wf.FatalError(err)
}
return
}

// ================ Script Filter ====================

if args["<query>"] != nil {
query = fmt.Sprintf("%v", args["<query>"])
}
var noUID bool
log.Printf("query=%s", query)

// Notify updates
if wf.UpdateCheckDue() == true {
log.Printf("update check due")
wf.Var("check_update", "1")
}

if wf.UpdateAvailable() == true {
log.Printf("update available")
wf.NewItem("An update is available").
Subtitle("↩ or ⇥ to install update").
Valid(false).
Autocomplete("workflow:update").
Icon(iconUpdate)
noUID = true
}

// Load favourites
faves, err := loadFavourites(favesFile)
if err != nil {
panic(fmt.Sprintf("couldn't load favourites: %s", err))
}
log.Printf("%d favourites", len(faves))
log.Printf("%d favourite(s)", len(faves))

for _, f := range faves {
var uid string
if noUID == false {
uid = f.UUID
}
log.Printf("%s (%s)", f.Name, f.Type)
it := aw.NewItem(f.Name).
it := wf.NewItem(f.Name).
Subtitle(f.Server).
Arg(f.UUID).
UID(uid).
SortKey(fmt.Sprintf("%s %s", f.Name, f.Server)).
Icon(f.Icon()).
Valid(true)
Expand All @@ -224,16 +271,16 @@ func run() {
}

if query != "" {
res := aw.Filter(query)
log.Printf("%d favourites match '%s'", len(res), query)
res := wf.Filter(query)
log.Printf("%d favourite(s) match '%s'", len(res), query)
}

aw.WarnEmpty("No matching favourites", "Try a different query?")
wf.WarnEmpty("No favourite found", "Try a different query?")

aw.SendFeedback()
wf.SendFeedback()
}

// main calls run via aw.Run()
func main() {
aw.Run(run)
wf.Run(run)
}
93 changes: 92 additions & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
<string>net.deanishe.alfred.forklift</string>
<key>connections</key>
<dict>
<key>24E248A5-4469-4931-8B6B-1C0564F9CDEF</key>
<array>
<dict>
<key>destinationuid</key>
<string>4784A8CD-774A-4916-AC0D-2C32C0F711D8</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>BC70CD57-DACE-4F66-A7F5-758FBEA3A948</key>
<array>
<dict>
Expand All @@ -18,6 +31,16 @@
<key>vitoclose</key>
<false/>
</dict>
<dict>
<key>destinationuid</key>
<string>24E248A5-4469-4931-8B6B-1C0564F9CDEF</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
</dict>
<key>createdby</key>
Expand Down Expand Up @@ -94,28 +117,96 @@
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>./forklift --update</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>4784A8CD-774A-4916-AC0D-2C32C0F711D8</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>inputstring</key>
<string>{var:check_update}</string>
<key>matchcasesensitive</key>
<true/>
<key>matchmode</key>
<integer>0</integer>
<key>matchstring</key>
<string>1</string>
</dict>
<key>type</key>
<string>alfred.workflow.utility.filter</string>
<key>uid</key>
<string>24E248A5-4469-4931-8B6B-1C0564F9CDEF</string>
<key>version</key>
<integer>1</integer>
</dict>
</array>
<key>readme</key>
<string></string>
<key>uidata</key>
<dict>
<key>0C7F2339-51B3-4C59-A4E6-D134D8F91FD0</key>
<dict>
<key>colorindex</key>
<integer>3</integer>
<key>xpos</key>
<integer>240</integer>
<key>ypos</key>
<integer>40</integer>
</dict>
<key>24E248A5-4469-4931-8B6B-1C0564F9CDEF</key>
<dict>
<key>colorindex</key>
<integer>8</integer>
<key>note</key>
<string>check_update == 1</string>
<key>xpos</key>
<integer>280</integer>
<key>ypos</key>
<integer>240</integer>
</dict>
<key>4784A8CD-774A-4916-AC0D-2C32C0F711D8</key>
<dict>
<key>colorindex</key>
<integer>8</integer>
<key>note</key>
<string>Check for update</string>
<key>xpos</key>
<integer>410</integer>
<key>ypos</key>
<integer>210</integer>
</dict>
<key>BC70CD57-DACE-4F66-A7F5-758FBEA3A948</key>
<dict>
<key>colorindex</key>
<integer>3</integer>
<key>xpos</key>
<integer>50</integer>
<key>ypos</key>
<integer>40</integer>
</dict>
</dict>
<key>version</key>
<string>0.1</string>
<string>0.1.1</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down
Binary file added update-available.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 914590e

Please sign in to comment.