2024-06-23 13:35:02 -07:00
|
|
|
// Package hook provides a simple interface for running hook commands that may
|
|
|
|
// be defined in the asdfrc file
|
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
2024-08-09 10:35:28 -07:00
|
|
|
"io"
|
2024-06-23 13:35:02 -07:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"asdf/config"
|
|
|
|
"asdf/execute"
|
|
|
|
)
|
|
|
|
|
2024-08-09 10:35:28 -07:00
|
|
|
// Run gets a hook command from config and runs it with the provided arguments.
|
|
|
|
// Output is sent to STDOUT and STDERR
|
|
|
|
func Run(conf config.Config, hookName string, arguments []string) error {
|
|
|
|
return RunWithOutput(conf, hookName, arguments, os.Stdout, os.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithOutput gets a hook command from config and runs it with the provided
|
|
|
|
// arguments. Output is sent to the provided io.Writers.
|
|
|
|
func RunWithOutput(config config.Config, hookName string, arguments []string, stdOut io.Writer, stdErr io.Writer) error {
|
2024-06-23 13:35:02 -07:00
|
|
|
hookCmd, err := config.GetHook(hookName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hookCmd == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
cmd := execute.NewExpression(hookCmd, arguments)
|
2024-06-23 13:35:02 -07:00
|
|
|
|
2024-08-09 10:35:28 -07:00
|
|
|
cmd.Stdout = stdOut
|
|
|
|
cmd.Stderr = stdErr
|
2024-06-23 13:35:02 -07:00
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|