This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load resources either from the dir of the executable, or from the resolved go path, preferring the dir of the executable.
- Loading branch information
Showing
3 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"go/build" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"bitbucket.org/kardianos/osext" | ||
) | ||
|
||
// exists returns whether the given file or directory exists or not | ||
func exists(path string) bool { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true | ||
} | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
return false | ||
} | ||
|
||
func resourcePaths() (staticPath string, dataPath string) { | ||
base, err := osext.ExecutableFolder() | ||
if err != nil { | ||
log.Fatal("Could not read base dir") | ||
} | ||
|
||
staticPath = filepath.Join(base, "static") | ||
dataPath = filepath.Join(base, "data") | ||
if exists(dataPath) && exists(staticPath) { | ||
return | ||
} | ||
|
||
p, err := build.Default.Import(basePkg, "", build.FindOnly) | ||
if err != nil { | ||
log.Fatal("Could not find package dir") | ||
} | ||
|
||
staticPath = filepath.Join(p.Dir, "static") | ||
dataPath = filepath.Join(p.Dir, "data") | ||
return | ||
} |