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

murex / TCR / 15156762416

21 May 2025 08:00AM UTC coverage: 90.473% (+0.04%) from 90.43%
15156762416

push

github

mengdaming
Update node dependencies

5242 of 5794 relevant lines covered (90.47%)

5610.16 hits per line

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

85.48
/src/toolchain/command/command.go
1
/*
2
Copyright (c) 2024 Murex
3

4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10

11
The above copyright notice and this permission notice shall be included in all
12
copies or substantial portions of the Software.
13

14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
SOFTWARE.
21
*/
22

23
package command
24

25
import (
26
        "errors"
27
        "runtime"
28
        "slices"
29
        "strings"
30
)
31

32
type (
33
        // OsName is the name of a supported operating system
34
        OsName string
35

36
        // ArchName is the name of a supported architecture
37
        ArchName string
38

39
        // Command is a command that can be run by a toolchain.
40
        // It contains 2 filters (Os and Arch) allowing to restrict it to specific OS(s)/Architecture(s).
41
        // - Path is the path to the command to be run.
42
        // - Arguments is the arguments to be passed to the command when executed.
43
        Command struct {
44
                Os        []OsName
45
                Arch      []ArchName
46
                Path      string
47
                Arguments []string
48
        }
49
)
50

51
// List of possible values for OsName
52
const (
53
        OsDarwin  OsName = "darwin"
54
        OsLinux   OsName = "linux"
55
        OsWindows OsName = "windows"
56
)
57

58
// List of possible values for ArchName
59
const (
60
        Arch386   ArchName = "386"
61
        ArchAmd64 ArchName = "amd64"
62
        ArchArm64 ArchName = "arm64"
63
)
64

65
// GetAllOsNames return the list of all supported OS Names
66
func GetAllOsNames() []OsName {
66✔
67
        return []OsName{OsDarwin, OsLinux, OsWindows}
66✔
68
}
66✔
69

70
// GetAllArchNames return the list of all supported OS Architectures
71
func GetAllArchNames() []ArchName {
66✔
72
        return []ArchName{Arch386, ArchAmd64, ArchArm64}
66✔
73
}
66✔
74

75
func (command Command) runsOnLocalMachine() bool {
×
76
        return command.runsOnPlatform(OsName(runtime.GOOS), ArchName(runtime.GOARCH))
×
77
}
×
78

79
func (command Command) runsOnPlatform(osName OsName, archName ArchName) bool {
33✔
80
        return command.runsWithOs(osName) && command.runsWithArch(archName)
33✔
81
}
33✔
82

83
func (command Command) runsWithOs(osName OsName) bool {
45✔
84
        return slices.Contains(command.Os, osName)
45✔
85
}
45✔
86

87
func (command Command) runsWithArch(archName ArchName) bool {
27✔
88
        return slices.Contains(command.Arch, archName)
27✔
89
}
27✔
90

91
func (command Command) check() error {
18✔
92
        if err := command.checkPath(); err != nil {
21✔
93
                return err
3✔
94
        }
3✔
95
        if err := command.checkOsTable(); err != nil {
21✔
96
                return err
6✔
97
        }
6✔
98
        return command.checkArchTable()
9✔
99
}
100

101
func (command Command) checkPath() error {
18✔
102
        if command.Path == "" {
21✔
103
                return errors.New("command path is empty")
3✔
104
        }
3✔
105
        return nil
15✔
106
}
107

108
func (command Command) checkOsTable() error {
15✔
109
        if command.Os == nil {
18✔
110
                return errors.New("command's OS list is empty")
3✔
111
        }
3✔
112
        if slices.Contains(command.Os, "") {
15✔
113
                return errors.New("a command OS name is empty")
3✔
114
        }
3✔
115
        return nil
9✔
116
}
117

118
func (command Command) checkArchTable() error {
9✔
119
        if command.Arch == nil {
12✔
120
                return errors.New("command's architecture list is empty")
3✔
121
        }
3✔
122
        if slices.Contains(command.Arch, "") {
9✔
123
                return errors.New("a command architecture name is empty")
3✔
124
        }
3✔
125
        return nil
3✔
126
}
127

128
// AsCommandLine returns a string formatted like if the command was run from the command line
129
func (command Command) AsCommandLine() string {
11✔
130
        return command.Path + " " + strings.Join(command.Arguments, " ")
11✔
131
}
11✔
132

133
// FindCommand retrieves out of a list of commands the first that is compatible with provided OS and Architecture
134
func FindCommand(commands []Command, osName OsName, archName ArchName) *Command {
12✔
135
        for _, cmd := range commands {
24✔
136
                if cmd.runsOnPlatform(osName, archName) {
15✔
137
                        return &cmd
3✔
138
                }
3✔
139
        }
140
        return nil
9✔
141
}
142

143
// FindCompatibleCommand retrieves out of a list of commands one that is compatible
144
// with local machine's OS and Architecture
145
func FindCompatibleCommand(commands []Command) *Command {
×
146
        for _, command := range commands {
×
147
                if command.runsOnLocalMachine() {
×
148
                        return &command
×
149
                }
×
150
        }
151
        return nil
×
152
}
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

© 2026 Coveralls, Inc