Add FileWorkspaceInfo
This commit is contained in:
30
file.go
30
file.go
@@ -2,8 +2,9 @@ package commons
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"os"
|
||||
"strings"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// FileDeepScan will scan dir recursively and append any files found to files.
|
||||
@@ -22,4 +23,27 @@ func FileDeepScan(dir string, files *[]string) {
|
||||
*files = append(*files, filepath.Join(dir, entry.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type WorkspaceInfo struct {
|
||||
ParentDir string
|
||||
}
|
||||
|
||||
// FileWorkspaceInfo given a file will return relevant information about
|
||||
// its location, such as its parent directory.
|
||||
func FileWorkspaceInfo(file string) *WorkspaceInfo {
|
||||
// 1. break down file
|
||||
clean := filepath.Clean(file)
|
||||
parts := strings.Split(clean, string(filepath.Separator))
|
||||
|
||||
l := len(parts)
|
||||
|
||||
if l > 3 {
|
||||
parentDir := parts[l - 2]
|
||||
return &WorkspaceInfo {
|
||||
ParentDir: parentDir,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user