-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontract_standalone_executable.go
More file actions
46 lines (39 loc) · 1.18 KB
/
contract_standalone_executable.go
File metadata and controls
46 lines (39 loc) · 1.18 KB
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
package exoskeleton
import (
"io/fs"
"path/filepath"
)
// StandaloneExecutableContract handles executable files that respond to --summary and --help.
//
// Note: This contract should be ordered AFTER ScriptCommandContract and ExecutableModuleContract
// in the contract list, as it matches all executable files.
type StandaloneExecutableContract struct{}
func (c *StandaloneExecutableContract) BuildCommand(path string, info fs.DirEntry, parent Command, d DiscoveryContext) (Command, error) {
// Only applies to files
if info.IsDir() {
return nil, ErrNotApplicable
}
// Must be executable
if ok, err := isExecutable(info); err != nil {
return nil, err
} else if !ok {
return nil, ErrNotApplicable
}
cmd := &executableCommand{
parent: parent,
path: path,
name: filepath.Base(path),
discoveredIn: filepath.Dir(path),
executor: d.Executor(),
cache: d.Cache(),
}
// Only applies to executables that define a summary
summary, err := d.Cache().Fetch(cmd, "summary", func() (string, error) {
return readSummaryFromExecutable(cmd)
})
if err != nil || summary == "" {
return nil, ErrNotApplicable
}
cmd.summary = &summary
return cmd, nil
}