Skip to content

Commit

Permalink
Add option to ignore local favourites
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 28, 2017
1 parent 914590e commit 7f454f8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
Binary file removed ForkLift-Favourites-0.1.1.alfredworkflow
Binary file not shown.
Binary file added ForkLift-Favourites-0.1.2.alfredworkflow
Binary file not shown.
62 changes: 47 additions & 15 deletions forklift.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"os"
"path/filepath"
"sort"
"strings"

aw "git.deanishe.net/deanishe/awgo"
Expand All @@ -40,26 +41,32 @@ Usage:
forklift --update
Options:
--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.
-d, --distname Print filename of distributable .alfredworkflow file
(for the build script).
-h, --help Show this message and exit.
-l, --logfile Print path to workflow's log file and exit.
-u, --update Check if an update is available.
`
// Connection types supported by ForkLift
connectionTypes = []string{"Local", "SFTP", "NFS", "Rackspace", "S3", "Search", "FTP", "Sync", "VNC", "WebDAV", "Workspace"}
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")

// Path to favourites file
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

// CLI options and workflow settings
query string
doLogfile bool
doDist bool
doUpdate bool
ignoreLocal bool
)

func init() {
Expand All @@ -71,6 +78,7 @@ func init() {
wf = aw.NewWorkflow(&aw.Options{GitHub: repo})
}

// fave* structs are for unmarshalling Favorites.json.
type faves struct {
Groups []*faveGroup `json:"favorites"`
}
Expand All @@ -94,6 +102,15 @@ type faveAttr struct {
Server string
}

// 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] }

// Favourite is a ForkLift favourite
type Favourite struct {
UUID string // Favourite UUID
Expand All @@ -119,8 +136,8 @@ func (f *Favourite) Icon() *aw.Icon {
// newIcon creates a new workflow icon from an icon file in ForkLift's
// Resources directory.
func newIcon(filename string) *aw.Icon {
path := fmt.Sprintf("/Applications/ForkLift.app/Contents/Resources/Connection%s.icns", filename)
return &aw.Icon{Value: path, Type: aw.IconTypeImageFile}
p := fmt.Sprintf("/Applications/ForkLift.app/Contents/Resources/Connection%s.icns", filename)
return &aw.Icon{Value: p, Type: aw.IconTypeImageFile}
}

// loadFavourites reads favourites from JSON file at path.
Expand Down Expand Up @@ -153,6 +170,10 @@ func loadFavourites(path string) ([]*Favourite, error) {
Type: f.Type,
}

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

// Ignore local favourites whose path doesn't exist
if fav.Type == "Local" && !aw.PathExists(fav.Path) {
continue
Expand All @@ -165,9 +186,13 @@ func loadFavourites(path string) ([]*Favourite, error) {
favourites = append(favourites, fav)
}
}

sort.Sort(ByName(favourites))

return favourites, nil
}

// parseArgs sets options based on CLI flags and environment variables.
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)
Expand All @@ -192,6 +217,13 @@ func parseArgs() error {
if args["<query>"] != nil {
query = args["<query>"].(string)
}

v := os.Getenv("IGNORE_LOCAL")
log.Printf("IGNORE_LOCAL=%v", v)
if v == "1" || v == "true" {
ignoreLocal = true
}

return nil
}

Expand All @@ -200,7 +232,7 @@ func run() {
// ================ Alternate actions ====================

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

if doLogfile == true {
Expand Down Expand Up @@ -249,7 +281,7 @@ func run() {
// Load favourites
faves, err := loadFavourites(favesFile)
if err != nil {
panic(fmt.Sprintf("couldn't load favourites: %s", err))
wf.FatalError(fmt.Errorf("couldn't load favourites: %s", err))
}
log.Printf("%d favourite(s)", len(faves))

Expand Down
7 changes: 6 additions & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@
<integer>40</integer>
</dict>
</dict>
<key>variables</key>
<dict>
<key>IGNORE_LOCAL</key>
<string>1</string>
</dict>
<key>version</key>
<string>0.1.1</string>
<string>0.1.2</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down

0 comments on commit 7f454f8

Please sign in to comment.