Skip to content

Commit

Permalink
feat: ensure parent directories in path helpers
Browse files Browse the repository at this point in the history
Path helpers are typically used in conjuction with writing a file, this
simplifies workflows related to generating build files.

Parent directories are only created automatically by helpers leading
into ignored directories (build, tools, bin).
  • Loading branch information
odsod committed Jul 18, 2023
1 parent 7ba36c6 commit dcca273
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions sg/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,31 @@ func FromSageDir(pathElems ...string) string {
}

// FromToolsDir returns the path relative to where tools are downloaded and installed.
// Parent directories of the returned path will be automatically created.
func FromToolsDir(pathElems ...string) string {
return FromSageDir(append([]string{toolsDir}, pathElems...)...)
path := FromSageDir(append([]string{toolsDir}, pathElems...)...)
ensureParentDir(path)
return path
}

// FromBinDir returns the path relative to where tool binaries are installed.
// Parent directories of the returned path will be automatically created.
func FromBinDir(pathElems ...string) string {
return FromSageDir(append([]string{binDir}, pathElems...)...)
path := FromSageDir(append([]string{binDir}, pathElems...)...)
ensureParentDir(path)
return path
}

// FromBuildDir returns the path relative to where generated build files are installed.
// Parent directories of the returned path will be automatically created.
func FromBuildDir(pathElems ...string) string {
return FromSageDir(append([]string{buildDir}, pathElems...)...)
path := FromSageDir(append([]string{buildDir}, pathElems...)...)
ensureParentDir(path)
return path
}

func ensureParentDir(path string) {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
panic(err)
}
}

0 comments on commit dcca273

Please sign in to comment.