feat: use exec.LookPath to find formatter executable

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2023-12-26 11:01:35 +00:00
parent 26c2ae1cda
commit d8d666a132
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0

View File

@ -25,8 +25,9 @@ type Formatter struct {
// Excludes is an optional list of glob patterns used to exclude certain files from this Formatter.
Excludes []string
name string
log *log.Logger
name string
log *log.Logger
executable string // path to the executable described by Command
// internal compiled versions of Includes and Excludes.
includes []glob.Glob
@ -41,14 +42,24 @@ type Formatter struct {
batchSize int
}
// Executable returns the path to the executable defined by Command
func (f *Formatter) Executable() string {
return f.executable
}
// Init is used to initialise internal state before this Formatter is ready to accept paths.
func (f *Formatter) Init(name string) error {
// capture the name from the config file
f.name = name
// test if the formatter is available
if err := exec.Command(f.Command, "--help").Run(); err != nil {
executable, err := exec.LookPath(f.Command)
if errors.Is(err, exec.ErrNotFound) {
return ErrFormatterNotFound
} else if err != nil {
return err
}
f.executable = executable
// initialise internal state
f.log = log.WithPrefix("format | " + name)