• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-artifact / 1886872200

24 Jun 2025 02:27PM UTC coverage: 76.671% (+0.07%) from 76.598%
1886872200

Pull #717

gitlab-ci

elkoniu
chore: Look for binaries in brew specific path

When brew installs binary it will create symlink directory
in location like `/usr/local/opt/<binary>/bin`. This location
is not part of the system PATH by default.

When mender-artifact evaluates commands location it need to check
this brew specific directories apart of default ones.

ticket: MEN-8471
changelog: Extend tools search locations with brew specific ones
Signed-off-by: Paweł Poławski <pawel.polawski@northern.tech>
Pull Request #717: chore: Look for binaries in brew specific path

11 of 15 new or added lines in 1 file covered. (73.33%)

249 existing lines in 2 files now uncovered.

5932 of 7737 relevant lines covered (76.67%)

132.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/utils/binpath.go
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package utils
16

17
import (
18
        "fmt"
19
        "os"
20
        "os/exec"
21
        "path"
22
        "runtime"
23
        "slices"
24

25
        "github.com/pkg/errors"
26
)
27

28
var (
29
        ExternalBinaryPaths = []string{"/usr/sbin", "/sbin", "/usr/local/sbin"}
30
)
31

32
var (
33
        BrewSpecificPaths = []string{"/usr/local/opt"}
34
)
35

36
var unsupportedBinariesDarwin = []string{
37
        "parted",
38
        "fsck.ext4",
39
        "fsck.vfat",
40
}
41

42
const errorUnsupportedDarwin = "Operations that use %q are unfortunately not available on Mac OS."
43

44
func GetBinaryPath(command string) (string, error) {
1,370✔
45
        // first check if command exists in PATH
1,370✔
46
        p, err := exec.LookPath(command)
1,370✔
47
        if err == nil {
2,734✔
48
                return p, nil
1,364✔
49
        }
1,364✔
50

51
        // maybe sbin isn't included in PATH, check there explicitly.
52
        for _, p = range ExternalBinaryPaths {
12✔
53
                p, err = exec.LookPath(path.Join(p, command))
6✔
54
                if err == nil {
6✔
55
                        return p, nil
×
56
                }
×
57
        }
58

59
        // look for the binaries in brew symlink directories
60
        // example: /usr/local/opt/e2fsprogs/bin, /usr/local/opt/mtools/bin etc.
61
        for _, p = range BrewSpecificPaths {
8✔
62
                items, err := os.ReadDir(p)
2✔
63
                if err != nil {
3✔
64
                        break
1✔
65
                }
66
                for _, d := range items {
75✔
67
                        // normal files and symbolic links will be processed too
74✔
68
                        // and just result in error when looking for binary
74✔
69
                        k, err := exec.LookPath(path.Join(p, d.Name(), "bin", command))
74✔
70
                        if err == nil {
74✔
NEW
71
                                return k, nil
×
NEW
72
                        }
×
73
                        k, err = exec.LookPath(path.Join(p, d.Name(), "sbin", command))
74✔
74
                        if err == nil {
74✔
NEW
75
                                return k, nil
×
NEW
76
                        }
×
77
                }
78
        }
79

80
        // not found, but oh well...
81
        if runtime.GOOS == "darwin" {
7✔
82
                base := path.Base(command)
1✔
83
                if slices.Contains(unsupportedBinariesDarwin, base) {
1✔
84
                        return command, errors.Wrap(err, fmt.Sprintf(errorUnsupportedDarwin, base))
×
85
                }
×
86
        }
87

88
        return command, err
6✔
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc