asdf/hook/hook.go
Trevor Brown 65688915a5 feat(golang-rewrite): create versions.InstallOneVersion function
* Create Plugin.Exists method
* Create internal/versions package
* Add missing asdfrc for internal/resolve package tests
* Create versions.ParseString function
* Create versions.InstallOneVersion function
* Update hook package to allow hook output to be received
* Write tests
2024-12-18 11:32:01 -05:00

38 lines
934 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/config"
"asdf/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()
}