mirror of
https://github.com/EnumeratedDev/bpm.git
synced 2026-09-16 10:36:12 +00:00
Add 'GetPathOwners' function
This commit is contained in:
+9
-45
@@ -152,7 +152,7 @@ func main() {
|
||||
currentFlagSet.StringP("root", "R", "/", "Operate on specified root directory")
|
||||
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s <options>", subcommand), "Show what packages own the specified paths", os.Args[2:])
|
||||
|
||||
getFileOwner()
|
||||
getPathOwners()
|
||||
case "c", "compile":
|
||||
// Setup flags and help
|
||||
currentFlagSet = flag.NewFlagSet("compile", flag.ExitOnError)
|
||||
@@ -1134,7 +1134,7 @@ func updatePackages() {
|
||||
}
|
||||
}
|
||||
|
||||
func getFileOwner() {
|
||||
func getPathOwners() {
|
||||
// Get flags
|
||||
rootDir, _ := currentFlagSet.GetString("root")
|
||||
|
||||
@@ -1157,7 +1157,7 @@ func getFileOwner() {
|
||||
// Ensure file exists
|
||||
stat, err := os.Lstat(path)
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("Error: file (%s) does not exist!\n", path)
|
||||
log.Printf("Error: %s", err)
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
@@ -1170,57 +1170,21 @@ func getFileOwner() {
|
||||
pathType = "Symlink"
|
||||
}
|
||||
|
||||
// Get absolte path to path
|
||||
absPath, err := filepath.Abs(path)
|
||||
pathOwners, err := bpmlib.GetPathOwners(path, rootDir)
|
||||
if err != nil {
|
||||
log.Printf("Error: could not get absolute path of file (%s)\n", path)
|
||||
log.Printf("Error: %s", err)
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
|
||||
// Get path relative to rootDir
|
||||
if !strings.HasPrefix(absPath, rootDir) {
|
||||
log.Printf("Error: could not get path of file (%s) relative to root path", absPath)
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
absPath, err = filepath.Rel(rootDir, absPath)
|
||||
if err != nil {
|
||||
log.Printf("Error: could not get path of file (%s) relative to root path", absPath)
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
|
||||
// Trim leading and trailing slashes
|
||||
absPath = strings.TrimLeft(absPath, "/")
|
||||
absPath = strings.TrimRight(absPath, "/")
|
||||
|
||||
// Get installed packages
|
||||
pkgs, err := bpmlib.GetInstalledPackages(rootDir)
|
||||
if err != nil {
|
||||
log.Printf("Error: could not get installed packages: %s\n", err.Error())
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
|
||||
// Add packages that own path to list
|
||||
var pkgList []string
|
||||
for _, pkg := range pkgs {
|
||||
if slices.ContainsFunc(bpmlib.GetPackage(pkg, rootDir).PkgFiles, func(entry *bpmlib.PackageFileEntry) bool {
|
||||
return entry.Path == absPath
|
||||
}) {
|
||||
pkgList = append(pkgList, pkg)
|
||||
}
|
||||
}
|
||||
|
||||
// Print packages
|
||||
if len(pkgList) == 0 {
|
||||
fmt.Printf("%s (%s) is not owned by any packages!\n", absPath, pathType)
|
||||
if len(pathOwners) == 0 {
|
||||
fmt.Printf("%s (%s) is not owned by any packages!\n", path, pathType)
|
||||
exitCode = 1
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("%s (%s) is owned by the following packages:\n", absPath, pathType)
|
||||
for _, pkg := range pkgList {
|
||||
fmt.Printf("%s (%s) is owned by the following packages:\n", path, pathType)
|
||||
for _, pkg := range pathOwners {
|
||||
fmt.Println("- " + pkg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -103,6 +104,41 @@ func GetInstalledPackages(rootDir string) (ret []string, err error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func GetPathOwners(path, rootDir string) (ret []string, err error) {
|
||||
// Get absolte path to path
|
||||
path, err = filepath.Abs(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
path, err = filepath.Rel(rootDir, path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Trim leading and trailing slashes
|
||||
path = strings.TrimLeft(path, "/")
|
||||
path = strings.TrimRight(path, "/")
|
||||
|
||||
// Get installed packages
|
||||
pkgs, err := GetInstalledPackages(rootDir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Add packages that own path to list
|
||||
for _, pkg := range pkgs {
|
||||
pkgFiles := getPackageFiles(pkg, rootDir)
|
||||
if slices.ContainsFunc(pkgFiles, func(entry *PackageFileEntry) bool {
|
||||
return entry.Path == path
|
||||
}) {
|
||||
ret = append(ret, pkg)
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func IsPackageInstalled(pkg, rootDir string) bool {
|
||||
// Initialize local package information
|
||||
err := InitializeLocalPackageInformation(rootDir)
|
||||
|
||||
Reference in New Issue
Block a user