Skip to content

Commit

Permalink
Cache base path for util.RelPath() on first use.
Browse files Browse the repository at this point in the history
It isn't supposed to change (it would be a problem if it did).  More to
the point, flaky filesystems can make that fail with a panic, which is
tolerable on startup but not half-way through a pipeline.
  • Loading branch information
adam-azarchs committed Feb 16, 2019
1 parent 23c05ff commit ded5f1e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions martian/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,31 @@ import (
"github.com/martian-lang/docopt.go"
)

func RelPath(p string) string {
base := os.Getenv("MARTIAN_BASE")
if base != "" {
return path.Join(base, p)
func getExeBasePath() string {
if base := os.Getenv("MARTIAN_BASE"); base != "" {
return base
} else {
if exe, err := os.Executable(); err != nil {
panic(err)
} else {
if exe, err := filepath.EvalSymlinks(exe); err != nil {
panic(err)
} else {
return path.Join(path.Dir(exe), p)
return path.Dir(exe)
}
}
}
}

var exeBasePath string

func RelPath(p string) string {
if exeBasePath == "" {
exeBasePath = getExeBasePath()
}
return path.Join(exeBasePath, p)
}

func Mkdir(p string) error {
if err := os.Mkdir(p, 0777); err != nil {
if !os.IsExist(err) {
Expand Down

0 comments on commit ded5f1e

Please sign in to comment.