Skip to content

Commit

Permalink
feat: adds script hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed May 13, 2023
1 parent f3378b7 commit b0e8464
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type Hook struct {
Command string `json:"command"`
Args []string `json:"args"`
Script string `json:"script"`
}

type SinceConfig struct {
Expand Down
46 changes: 41 additions & 5 deletions hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ const (
After HookType = "after"
)

var scriptInterpreter string

func init() {
scriptInterpreter = os.Getenv("SINCE_HOOK_SCRIPT_INTERPRETER")
if scriptInterpreter == "" {
scriptInterpreter = "/bin/bash"
}
}

// ExecuteHooks executes all hooks of the given type
func ExecuteHooks(config cfg.SinceConfig, hookType HookType, metadata vcs.ReleaseMetadata) error {
var hooks []cfg.Hook
Expand All @@ -58,9 +67,36 @@ func ExecuteHooks(config cfg.SinceConfig, hookType HookType, metadata vcs.Releas

// executeHook executes a hook command with the given arguments
func executeHook(hook cfg.Hook, metadata vcs.ReleaseMetadata) error {
logrus.Debugf("executing hook '%s %s'", hook.Command, strings.Join(hook.Args, " "))
var command string
var args []string

if hook.Script != "" {
if hook.Command != "" {
return fmt.Errorf("hook cannot specify both a command and a script")
}

script, err := os.CreateTemp(os.TempDir(), "since-hook*.sh")
if err != nil {
return fmt.Errorf("error creating temporary script file: %v", err)
}
defer os.Remove(script.Name())
err = os.WriteFile(script.Name(), []byte(hook.Script), 0755)
if err != nil {
return err
}
command = scriptInterpreter
args = []string{script.Name()}
} else {
command = hook.Command
args = hook.Args
}
return execCommand(command, args, metadata)
}

func execCommand(command string, args []string, metadata vcs.ReleaseMetadata) error {
logrus.Debugf("executing hook '%s %s'", command, strings.Join(args, " "))

cmd := exec.Command(hook.Command, hook.Args...)
cmd := exec.Command(command, args...)
cmd.Dir = metadata.RepoPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -72,12 +108,12 @@ func executeHook(hook cfg.Hook, metadata vcs.ReleaseMetadata) error {

err := cmd.Run()
if err != nil {
return fmt.Errorf("error executing hook '%s %s': %v", hook.Command, strings.Join(hook.Args, " "), err)
return fmt.Errorf("error executing hook '%s %s': %v", command, strings.Join(args, " "), err)
}
if cmd.ProcessState.Success() {
logrus.Debugf("hook '%s %s' executed successfully", hook.Command, strings.Join(hook.Args, " "))
logrus.Debugf("hook '%s %s' executed successfully", command, strings.Join(args, " "))
} else {
logrus.Warnf("hook '%s %s' executed with errors", hook.Command, strings.Join(hook.Args, " "))
logrus.Warnf("hook '%s %s' executed with errors", command, strings.Join(args, " "))
}
return nil
}
17 changes: 16 additions & 1 deletion hooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestExecuteHooks(t *testing.T) {
wantErr: false,
},
{
name: "successful hook",
name: "successful command hook",
args: args{
config: cfg.SinceConfig{
Before: []cfg.Hook{
Expand All @@ -54,6 +54,21 @@ func TestExecuteHooks(t *testing.T) {
},
wantErr: false,
},
{
name: "successful script hook",
args: args{
config: cfg.SinceConfig{
Before: []cfg.Hook{
{
Script: `echo "hello world"`,
},
},
},
metadata: vcs.ReleaseMetadata{},
hookType: Before,
},
wantErr: false,
},
{
name: "failing hook",
args: args{
Expand Down

0 comments on commit b0e8464

Please sign in to comment.