Skip to content

Commit

Permalink
Update dependencies and tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Oct 17, 2019
1 parent 841c491 commit 8469ea7
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 517 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
here="$( dirname "$0" )"
ip="${here}/info.plist"
pl=/usr/libexec/PlistBuddy

export alfred_workflow_bundleid="$( $pl -c "Print :bundleid" "$ip" )"
export alfred_workflow_name="$( $pl -c "Print :name" "$ip" )"
export alfred_workflow_version="$( $pl -c "Print :version" "$ip" )"

export alfred_workflow_cache="${HOME}/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data/${alfred_workflow_bundleid}"
export alfred_workflow_data="${HOME}/Library/Application Support/Alfred/Workflow Data/${alfred_workflow_bundleid}"

19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<div align="center">
<img src="./icon.png" width="128" height="128">
</div>

ForkLift Favourites for Alfred
==============================

Browse, filter and open [ForkLift 3][forklift] favourites from [Alfred][alfred].
Browse, filter and open [ForkLift 3][forklift] favourites from [Alfred 3+][alfred].

![](./demo.gif)
![](https://github.com/deanishe/alfred-forklift/raw/master/demo.gif)


Download & installation
Expand All @@ -19,6 +23,15 @@ Usage
- `` — Open favourite in ForkLift


Configuration
-------------

The workflow has one setting in its configuration sheet (the `[𝑥]` symbol):

`IGNORE_LOCAL` — Set to `1` or `true` to exclude Local favourites from results; set to `0`, `false` or empty to include Local favourites in results.



Licensing & thanks
------------------

Expand All @@ -27,7 +40,7 @@ This workflow is released under the [MIT licence][mit].
It is based on the [AwGo library][awgo], also released under the MIT licence.


[alfred]: https://www.alfredapp.com
[alfred]: https://www.alfredapp.com/
[awgo]: https://git.deanishe.net/deanishe/awgo/
[forklift]: http://www.binarynights.com/forklift/
[ghr]: https://github.com/deanishe/alfred-forklift/releases
Expand Down
9 changes: 0 additions & 9 deletions alfred_env.sh

This file was deleted.

100 changes: 39 additions & 61 deletions build-workflow.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

set -e

here="$( cd "$( dirname "$0" )"; pwd )"
source "${here}/alfred_env.sh"
here="${${(%):-%x}:A:h}"
source "${here}/.env"

build=true
devmode=true
verbose=

log() {
echo "$@" > /dev/stderr
}

usage() {
cat <<EOS
build-workflow.sh [options]
build-workflow.zsh [options]
Usage:
build-workflow.sh [-x] [-d] [-v]
build-workflow.sh -h
build-workflow.zsh [-d] [-v]
build-workflow.zsh -h
Options:
-d Distribution. Also build .alfredworkflow file.
-x Don't build executable.
-v Verbose.
-h Show this help message and exit.
-d also build .alfredworkflow file
-v verbose
-h show this help message and exit
EOS
}

while getopts ":dhvx" opt; do
while getopts ":dhv" opt; do
case $opt in
d)
devmode=false
Expand All @@ -37,82 +39,58 @@ while getopts ":dhvx" opt; do
v)
verbose=-v
;;
x)
build=false
;;
\?)
log "Invalid option: -$OPTARG"
log "invalid option: -$OPTARG"
exit 1
;;
esac
done
shift $((OPTIND-1))


log() {
echo "$@" > /dev/stderr
}

pushd "$here" &> /dev/null
cd "$here"

test -d "build" && {
log "cleaning ./build ..."
rm $verbose -rf ./build
command rm $verbose -rf ./build/*
log
}
} || true


log "copying assets to ./build ..."

mkdir $verbose -p ./build

ln $verbose icon.png ./build/
ln $verbose update-available.png ./build/
ln $verbose info.plist ./build/
ln $verbose README.md ./build/
ln $verbose LICENCE.txt ./build/
cd ./build
ln -s $verbose ../*.png .
ln -s $verbose ../info.plist .
ln -s $verbose ../README.md .
ln -s $verbose ../LICENCE.txt .
cd -
log

$build && {
log "building executable(s) ..."
go build -v -o ./forklift .
ST_BUILD=$?
if [ "$ST_BUILD" != 0 ]; then
log "error building executable."
rm $verbose -rf ./build/
popd &> /dev/null
exit $ST_BUILD
fi
}

chmod 755 ./forklift
ln $verbose ./forklift ./build/forklift

# Get the dist filename from the executable
zipfile="$(./forklift --distname 2> /dev/null)"
log "building executable(s) ..."
go build -v -o ./build/forklift .
log

if test -e "$zipfile"; then
log "removing existing .alfredworkflow file ..."
rm $verbose -rf "$zipfile"
log
fi

$devmode || {
# Get the dist filename from the executable
zipfile="ForkLift-Favourites-${alfred_workflow_version}.alfredworkflow"

test -f "./dist/$zipfile" && {
log "removing existing .alfredworkflow file ..."
rm $verbose -f "./dist/$zipfile"
log
} || true

log "building $zipfile ..."
pushd ./build/ &> /dev/null
zip $verbose "../${zipfile}" *
ST_ZIP=$?
if [ "$ST_ZIP" != 0 ]; then
log "error creating .alfredworkflow file."
rm $verbose -rf ./build/
popd &> /dev/null
exit $ST_ZIP
fi
popd &> /dev/null
mkdir $verbose -p ./dist
cd ./build
zip $verbose "../dist/${zipfile}" *
log
}


popd &> /dev/null
log "all done."
log "all done"

148 changes: 148 additions & 0 deletions favourites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) 2019 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"

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

// ForkLift's icon directory
const resources = "/Applications/ForkLift.app/Contents/Resources/"

var (
// path to favourites file
favesFile = os.ExpandEnv("$HOME/Library/Application Support/ForkLift/Favorites/Favorites.json")
)

// Favourite is a ForkLift favourite
type Favourite struct {
UUID string // Favourite UUID
Name string // Name of favourite
Group string // Name of favourite's group
Path string // Favourite's path
Server string // Hostname of favourite
Type string // Type of favourite (SFTP, Local etc.)
}

// ByName sorts a slice of Favourites by name.
type ByName []Favourite

func (s ByName) Len() int { return len(s) }
func (s ByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
func (s ByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// Icon returns the appropriate icon for the favourite's type
func (f Favourite) Icon() *aw.Icon {
switch f.Type {
case "Local":
return &aw.Icon{Value: f.Path, Type: aw.IconTypeFileIcon}
case "Backblaze":
return &aw.Icon{Value: resources + "ConnectionBackblaze.icns"}
case "FTP":
return &aw.Icon{Value: resources + "ConnectionFTP.icns"}
case "GoogleDrive":
return &aw.Icon{Value: resources + "ConnectionGoogleDrive.icns"}
case "NFS":
return &aw.Icon{Value: resources + "ConnectionNFS.icns"}
case "Rackspace":
return &aw.Icon{Value: resources + "ConnectionRackspace.icns"}
case "S3":
return &aw.Icon{Value: resources + "ConnectionS3.icns"}
case "SFTP":
return &aw.Icon{Value: resources + "ConnectionSFTP.icns"}
case "Search":
return &aw.Icon{Value: resources + "ConnectionSearch.icns"}
case "Sync":
return &aw.Icon{Value: resources + "ConnectionSync.icns"}
case "VNC":
return &aw.Icon{Value: resources + "ConnectionVNC.icns"}
case "WebDAV", "WebDAVHTTPS":
return &aw.Icon{Value: resources + "ConnectionWebDAV.icns"}
case "Workspace":
return &aw.Icon{Value: resources + "ConnectionWorkspace.icns"}
default:
log.Printf("[WARN] unknown type: %s", f.Type)
return iconDefault
}
}

// loadFavourites reads favourites from JSON file at path.
func loadFavourites(path string) ([]Favourite, error) {
var faves = []Favourite{}

if !util.PathExists(path) {
return nil, fmt.Errorf("favourites file does not exist: %s", path)
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

raw := struct {
Groups []struct {
UUID string `json:"UUID"`
Attr struct {
Name string
Path string
Server string
} `json:"attributes"`
Children []struct {
UUID string `json:"UUID"`
Attr struct {
Name string
Path string
Server string
} `json:"attributes"`
Type string `json:"type"`
} `json:"childItems"`
Type string `json:"type"`
} `json:"favorites"`
}{}
if err := json.Unmarshal(data, &raw); err != nil {
return nil, fmt.Errorf("error unmarshalling %s: %s", path, err)
}
log.Printf("%2d group(s)", len(raw.Groups))
for _, fg := range raw.Groups {
log.Printf("%2d favourite(s) in group %q", len(fg.Children), fg.Attr.Name)
for _, f := range fg.Children {

fav := Favourite{
UUID: f.UUID,
Name: f.Attr.Name,
Group: fg.Attr.Name,
Path: f.Attr.Path,
Server: f.Attr.Server,
Type: f.Type,
}

if ignoreLocal && fav.Type == "Local" {
continue
}

// Ignore local favourites whose path doesn't exist
if fav.Type == "Local" && !util.PathExists(fav.Path) {
continue
}

if fav.Name == "" && fav.Path != "" {
fav.Name = filepath.Base(fav.Path)
}

faves = append(faves, fav)
}
}

sort.Sort(ByName(faves))

return faves, nil
}
Loading

0 comments on commit 8469ea7

Please sign in to comment.