Skip to content

Commit

Permalink
feat: one more simplify for getProjectDir
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed May 5, 2024
1 parent 755074a commit 046a161
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,29 @@ func normalize(repo string) string {
// The project directory path is returned as a string.
func getProjectDir(repository string) string {
gitProjectDir := os.Getenv("GIT_PROJECT_DIR")

if strings.HasPrefix(gitProjectDir, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("%s", err)
log.Fatal(err)
}
gitProjectDir = strings.TrimPrefix(gitProjectDir, "~")
gitProjectDir = filepath.Join(homeDir, gitProjectDir)
gitProjectDir = filepath.Join(homeDir, gitProjectDir[1:])
}

return filepath.Join(gitProjectDir, normalize(repository))
}

// isDirectoryNotEmpty checks if the specified directory is not empty.
// It opens the directory and reads its contents. If there are any files or directories present,
// it returns true. Otherwise, it returns false.
// It uses the Readdirnames function to get the directory contents without loading full FileInfo
// structures for each entry. If there are any entries, it returns true. Otherwise, it returns false.
func isDirectoryNotEmpty(name string) bool {
f, err := os.Open(name)
if err != nil {
return false
}
defer f.Close()

files, _ := os.ReadDir(name)

return len(files) != 0
_, err = f.Readdirnames(1)
return err == nil
}

// Usage prints the usage of the program.
Expand Down

0 comments on commit 046a161

Please sign in to comment.