diff options
Diffstat (limited to 'stddirs.go')
-rw-r--r-- | stddirs.go | 53 |
1 files changed, 30 insertions, 23 deletions
@@ -40,10 +40,13 @@ const ( DEFAULT_PATH_TESTS="tests" ) -type dirConfig struct { - src string - config string - build string +type DirConfig struct { + src string + config string + build string + + // Config falls back to the config found in the parent directory if the env variable hasn't been overridden + pdFallback string } func dieDirAbsent(name, path string) { @@ -63,22 +66,22 @@ func dirFromEnv(name, env string) *string { return nil } -func (c *dirConfig) SetSrcDir(srcDir string) { +func (c *DirConfig) SetSrcDir(srcDir string) { dieDirAbsent("src", srcDir) c.src = srcDir } -func (c *dirConfig) SetConfigDir(configDir string) { +func (c *DirConfig) SetConfigDir(configDir string) { dieDirAbsent("planr (config)", configDir) c.config = configDir } -func (c *dirConfig) SetBuildDir(buildDir string) { +func (c *DirConfig) SetBuildDir(buildDir string) { dieDirAbsent("build", buildDir) c.build = buildDir } -func (c *dirConfig) SetConfigDirFromTree(cdir string) { +func (c *DirConfig) SetConfigDirFromTree(cdir string) { var configDir string found := traverseUp(cdir, func (path string) bool { @@ -94,28 +97,32 @@ func (c *dirConfig) SetConfigDirFromTree(cdir string) { return false }); - if !found { - log.Fatal("Could not find planr directory"); - } - c.config = configDir + if found { + c.pdFallback = configDir + } } -func (c dirConfig) ConfigDir() string { +func (c DirConfig) ConfigDir() string { if c.config != "" { return c.config } - dir := dirFromEnv("config", ENV_CONFIG_DIR) - if dir == nil { - log.Fatal("Could not find directory") + + if dir := dirFromEnv("config", ENV_CONFIG_DIR); dir != nil { + c.config = *dir + return c.config + } + + if c.pdFallback == "" { + log.Fatal("Could not find planr directory"); } - c.config = *dir + c.config = c.pdFallback; return c.config } -func (c dirConfig) SrcDir() string { +func (c DirConfig) SrcDir() string { if c.src != "" { return c.src } @@ -130,7 +137,7 @@ func (c dirConfig) SrcDir() string { return path.Join(dir, DEFAULT_PATH_SRC) } -func (c dirConfig) BuildDir() string { +func (c DirConfig) BuildDir() string { if c.src != "" { return c.src } @@ -144,7 +151,7 @@ func (c dirConfig) BuildDir() string { return path.Join(dir, DEFAULT_PATH_BUILD) } -func (c dirConfig) CleanBuildDir() { +func (c DirConfig) CleanBuildDir() { build := c.BuildDir() if err := os.RemoveAll(build); err != nil { @@ -156,7 +163,7 @@ func (c dirConfig) CleanBuildDir() { } } -func (c dirConfig) MkBuildDir() { +func (c DirConfig) MkBuildDir() { build := c.BuildDir() if err := os.Mkdir(build, 0755); err != nil { @@ -164,13 +171,13 @@ func (c dirConfig) MkBuildDir() { } } -func (c dirConfig) RubricDir() string { +func (c DirConfig) RubricDir() string { rubric := path.Join(c.ConfigDir(), "rubric") dieDirAbsent("rubric", rubric) return rubric } -func (c dirConfig) TestsDir() string { +func (c DirConfig) TestsDir() string { tests := path.Join(c.ConfigDir(), "tests") dieDirAbsent("tests", tests) return tests |