Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ensure parent directories in path helpers #446

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}