From dbe11d19b9ac13c081ab0a4d2724f0e198269878 Mon Sep 17 00:00:00 2001 From: Deepak Kaul Date: Sun, 7 May 2017 18:41:00 -0400 Subject: [PATCH] Adding support for offline mode. File is saved everything its pulled off of the web. If there is an error retrieving the alfred file from the web then it checks the /Users/dkaul/.alfred/offline///alfred.yml file --- alfred/alfred.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/alfred/alfred.go b/alfred/alfred.go index 5c93f30..53024c7 100644 --- a/alfred/alfred.go +++ b/alfred/alfred.go @@ -9,6 +9,7 @@ import ( "path" "path/filepath" "regexp" + "runtime" "sort" "strconv" "strings" @@ -361,17 +362,54 @@ func (a *Alfred) findRemote() bool { url = a.remote.URL(remote, module) } + // Setup for offline mode + homeDir := os.Getenv("HOME") + if runtime.GOOS == "windows" { + homeDir = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + } + alfredDir := "" + alfredFile := "" + if homeDir == "" { + say("ERROR", "home dir not set") + } else { + alfredDir = homeDir + + string(os.PathSeparator) + ".alfred" + + string(os.PathSeparator) + "offline" + + string(os.PathSeparator) + remote + + string(os.PathSeparator) + module + alfredFile = alfredDir + string(os.PathSeparator) + "alfred.yml" + } + // try to fetch the alfred file resp, err := http.Get(url) if err != nil || resp.StatusCode != 200 { - say("error", "Unknown module "+a.args[1]) - say("url", url) + if homeDir != "" { + body, err := ioutil.ReadFile(alfredFile) + if err != nil { + say("ERROR", "Unknown module "+a.args[1]) + say("url", url) + } else { + a.contents = body + a.location = alfredFile + } + } return true } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err == nil { + if homeDir != "" { + err := os.MkdirAll(alfredDir, 0777) + if err != nil { + say("ERROR", "Creating offline directories.") + } else { + err := ioutil.WriteFile(alfredFile, body, 0644) + if err != nil { + say("ERROR", "Saving offline alfred files") + } + } + } // We found something ... lets use it! //a.contents = append(append(a.contents, []byte("\n")...), body...) a.contents = body