Skip to content

Commit

Permalink
Adding support for offline mode. File is saved everything its pulled …
Browse files Browse the repository at this point in the history
…off of the web. If there is an error retrieving the alfred file from the web then it checks the /Users/dkaul/.alfred/offline/<remote>/<module>/alfred.yml file
  • Loading branch information
Deepak Kaul authored and Deepak Kaul committed May 7, 2017
1 parent e2e4235 commit dbe11d1
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions alfred/alfred.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dbe11d1

Please sign in to comment.