>> make individual file arguments pass same checks as recursive search

> rename variables in parseFileList
This commit is contained in:
Thomas Lindop 2025-05-24 11:56:29 +01:00
parent bea54603cb
commit c8f28ba537

34
main.go
View file

@ -93,27 +93,24 @@ func comparePaths(a, b string) int {
func (ctx *context) parseFileList() ([]string, error) { func (ctx *context) parseFileList() ([]string, error) {
errMsgs := []string{} errMsgs := []string{}
result := slices.Clone(ctx.FileList) result := []string{}
slices.SortFunc(result, comparePaths) addPath := func(path string) {
for _, path := range result { loc, isDup := slices.BinarySearchFunc(result, path, comparePaths)
if _, err := os.Stat(path); err != nil { if !isDup {
msg := fmt.Sprintf( "File <<%s>> does not exist", path) result = slices.Insert(result, loc, path)
errMsgs = append(errMsgs, msg)
} }
} }
addFiles := func(fullPath string, fileInfo fs.DirEntry, err error) error { addRecursively := func(path string, fileInfo fs.DirEntry, err error) error {
if err != nil { if err != nil {
// This can only happen with the path passed to WalkDir
msg := fmt.Sprintf( msg := fmt.Sprintf(
"Directory <<%s>> does not exist or is not searchable", "Directory <<%s>> does not exist or is not searchable",
fullPath, path,
) )
errMsgs = append(errMsgs,msg) errMsgs = append(errMsgs,msg)
return err return err
} }
loc, isDup := slices.BinarySearchFunc(result, fullPath, comparePaths) addPath(path)
if !isDup {
result = slices.Insert(result, loc, fullPath)
}
return nil return nil
} }
for _, dir := range ctx.RecurseDirs { for _, dir := range ctx.RecurseDirs {
@ -121,7 +118,18 @@ func (ctx *context) parseFileList() ([]string, error) {
if err != nil { if err != nil {
return result, ErrNoPwd return result, ErrNoPwd
} }
fpath.WalkDir(dir, addFiles) fpath.WalkDir(dir, addRecursively)
}
for _, path := range ctx.FileList {
if _, err := os.Stat(path); err != nil {
msg := fmt.Sprintf( "File <<%s>> does not exist", path)
errMsgs = append(errMsgs, msg)
}
path, err := fpath.Abs(path)
if err != nil {
return result, ErrNoPwd
}
addPath(path)
} }
if len(errMsgs) == 0 { if len(errMsgs) == 0 {
return result, nil return result, nil