some clean up

This commit is contained in:
Lilian Jónsdóttir 2024-08-06 09:31:45 -07:00
parent d8ec25b36d
commit 50f4dbda8a
5 changed files with 22 additions and 34 deletions

View file

@ -20,7 +20,7 @@ var (
pwd, _ = os.Getwd()
)
// UnExpand returns dir after expanding some directory shortcuts
// UnExpand returns dir after unexpanding some directory shortcuts
//
// $HOME -> ~
//
@ -64,13 +64,11 @@ func PercentEncode(input string) (output string) {
return
}
func cleanDir(dir, pwd string) (out string) {
func cleanDir(dir, pwd string) string {
if strings.HasPrefix(dir, ".") {
out = filepath.Clean(dir)
return filepath.Clean(dir)
} else if !strings.HasPrefix(dir, sep) {
out = filepath.Join(pwd, dir)
} else {
out = dir
return filepath.Join(pwd, dir)
}
return
return dir
}

View file

@ -67,7 +67,7 @@ func NewDisk(path string) (DiskFile, error) {
}, nil
}
func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
var files Files
dir = filepath.Clean(dir)
if dir == "." || dir == "" {
@ -91,7 +91,7 @@ func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
files = append(files, readDir(dir, fltr)...)
}
return files, nil
return files
}
// isInHiddenDir checks `path` and parent directories
@ -143,7 +143,7 @@ func walkDir(dir string, fltr *filter.Filter) Files {
})
if err != nil {
log.Errorf("error walking directory %s: %s", dir, err)
return Files{}
return nil
}
return files
}
@ -152,7 +152,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
var files Files
fs, err := os.ReadDir(dir)
if err != nil {
return Files{}
return nil
}
for _, file := range fs {
name := file.Name()
@ -163,7 +163,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
info, err := file.Info()
if err != nil {
return Files{}
return nil
}
path := filepath.Dir(filepath.Join(dir, name))

View file

@ -67,7 +67,7 @@ func (t TrashInfo) String() string {
return t.name + t.path + t.ogpath + t.trashinfo
}
func FindInAllTrashes(ogdir string, fltr *filter.Filter) (Files, error) {
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
var files Files
personalTrash := filepath.Join(xdg.DataHome, "Trash")
@ -79,12 +79,13 @@ func FindInAllTrashes(ogdir string, fltr *filter.Filter) (Files, error) {
for _, trash := range getAllTrashes() {
fls, err := findTrash(trash, ogdir, fltr)
if err != nil {
log.Errorf("error reading trash dir '%s': %s", trash, err)
continue
}
files = append(files, fls...)
}
return files, nil
return files
}
func ConfirmRestore(confirm bool, fs Files) error {
@ -143,7 +144,7 @@ func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
infodir := filepath.Join(trashdir, "info")
dirs, err := os.ReadDir(infodir)
if err != nil {
return Files{}, err
return nil, err
}
for _, dir := range dirs {
@ -425,7 +426,8 @@ func getAllTrashes() []string {
})
if err != nil {
return []string{}
log.Errorf("error reading mounts: %s", err)
return nil
}
return trashes

View file

@ -86,7 +86,7 @@ type model struct {
fltrfiles files.Files
}
func newModel(fls []files.File, selectall, readonly, once bool, workdir string, mode modes.Mode) model {
func newModel(fls files.Files, selectall, readonly, once bool, workdir string, mode modes.Mode) model {
m := model{
keys: defaultKeyMap(),
readonly: readonly,

22
main.go
View file

@ -102,10 +102,7 @@ var (
err error
)
infiles, err = files.FindInAllTrashes(ogdir, fltr)
if err != nil {
return err
}
infiles = files.FindInAllTrashes(ogdir, fltr)
if len(infiles) <= 0 {
var msg string
if fltr.Blank() {
@ -206,10 +203,7 @@ var (
// if none of the args were files, then find files based on filter
if len(filesToTrash) == 0 {
fls, err := files.FindDisk(workdir, recursive, fltr)
if err != nil {
return err
}
fls := files.FindDisk(workdir, recursive, fltr)
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to trash")
return nil
@ -237,7 +231,7 @@ var (
Flags: slices.Concat(listFlags, trashedFlags, filterFlags),
Before: beforeCommands,
Action: func(_ *cli.Context) error {
fls, err := files.FindInAllTrashes(ogdir, fltr)
fls := files.FindInAllTrashes(ogdir, fltr)
var msg string
log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir)
@ -250,8 +244,6 @@ var (
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, msg)
return nil
} else if err != nil {
return err
}
return interactive.Show(fls, noInterArg, workdir)
@ -266,12 +258,10 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands,
Action: func(_ *cli.Context) error {
fls, err := files.FindInAllTrashes(ogdir, fltr)
fls := files.FindInAllTrashes(ogdir, fltr)
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to restore")
return nil
} else if err != nil {
return err
}
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring)
@ -295,12 +285,10 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands,
Action: func(_ *cli.Context) error {
fls, err := files.FindInAllTrashes(ogdir, fltr)
fls := files.FindInAllTrashes(ogdir, fltr)
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to clean")
return nil
} else if err != nil {
return err
}
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)