summaryrefslogtreecommitdiffstats
path: root/src/go/pkg/multipath/multipath.go
blob: 6172def063f78853336e9ce190426df79425ad2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SPDX-License-Identifier: GPL-3.0-or-later

package multipath

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"slices"
	"strings"

	"github.com/mitchellh/go-homedir"
)

type ErrNotFound struct{ msg string }

func (e ErrNotFound) Error() string { return e.msg }

// IsNotFound returns a boolean indicating whether the error is ErrNotFound or not.
func IsNotFound(err error) bool {
	var errNotFound ErrNotFound
	return errors.As(err, &errNotFound)
}

// MultiPath multi-paths
type MultiPath []string

// New multi-paths
func New(paths ...string) MultiPath {
	set := map[string]bool{}
	mPath := make(MultiPath, 0)

	for _, dir := range paths {
		if dir == "" {
			continue
		}
		if d, err := homedir.Expand(dir); err != nil {
			dir = d
		}
		if !set[dir] {
			mPath = append(mPath, dir)
			set[dir] = true
		}
	}

	return mPath
}

// Find finds a file in given paths
func (p MultiPath) Find(filename string) (string, error) {
	for _, dir := range p {
		file := filepath.Join(dir, filename)
		if _, err := os.Stat(file); !os.IsNotExist(err) {
			return file, nil
		}
	}
	return "", ErrNotFound{msg: fmt.Sprintf("can't find '%s' in %v", filename, p)}
}

func (p MultiPath) FindFiles(suffixes ...string) ([]string, error) {
	set := make(map[string]bool)
	var files []string

	for _, dir := range p {
		entries, err := os.ReadDir(dir)
		if err != nil {
			continue
		}

		for _, e := range entries {
			if !e.Type().IsRegular() {
				continue
			}

			ext := filepath.Ext(e.Name())
			name := strings.TrimSuffix(e.Name(), ext)

			if (len(suffixes) != 0 && !slices.Contains(suffixes, ext)) || set[name] {
				continue
			}

			set[name] = true
			file := filepath.Join(dir, e.Name())
			files = append(files, file)
		}
	}

	return files, nil
}