asdf/internal/hook/hook.go
Trevor Brown 620c0d87e8 feat(golang-rewrite): re-organize Go code
* move most Go packages to internal directory
* update import paths
2024-12-18 11:32:02 -05:00

38 lines
952 B
Go

// Package hook provides a simple interface for running hook commands that may
// be defined in the asdfrc file
package hook
import (
"io"
"os"
"asdf/internal/config"
"asdf/internal/execute"
)
// 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 {
hookCmd, err := config.GetHook(hookName)
if err != nil {
return err
}
if hookCmd == "" {
return nil
}
cmd := execute.NewExpression(hookCmd, arguments)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
return cmd.Run()
}