diff --git a/ForkLift-Favourites-0.1.1.alfredworkflow b/ForkLift-Favourites-0.1.1.alfredworkflow deleted file mode 100644 index ecc6ce3..0000000 Binary files a/ForkLift-Favourites-0.1.1.alfredworkflow and /dev/null differ diff --git a/ForkLift-Favourites-0.1.2.alfredworkflow b/ForkLift-Favourites-0.1.2.alfredworkflow new file mode 100644 index 0000000..5ade1b9 Binary files /dev/null and b/ForkLift-Favourites-0.1.2.alfredworkflow differ diff --git a/forklift.go b/forklift.go index d55f8fa..b951197 100644 --- a/forklift.go +++ b/forklift.go @@ -21,6 +21,7 @@ import ( "log" "os" "path/filepath" + "sort" "strings" aw "git.deanishe.net/deanishe/awgo" @@ -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() { @@ -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"` } @@ -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 @@ -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. @@ -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 @@ -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) @@ -192,6 +217,13 @@ func parseArgs() error { if args[""] != nil { query = args[""].(string) } + + v := os.Getenv("IGNORE_LOCAL") + log.Printf("IGNORE_LOCAL=%v", v) + if v == "1" || v == "true" { + ignoreLocal = true + } + return nil } @@ -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 { @@ -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)) diff --git a/info.plist b/info.plist index 75093ee..a1ad4ae 100644 --- a/info.plist +++ b/info.plist @@ -205,8 +205,13 @@ 40 + variables + + IGNORE_LOCAL + 1 + version - 0.1.1 + 0.1.2 webaddress