feat(hooks): add pre and post release hooks

This commit is contained in:
Nightapes
2020-01-05 18:42:07 +01:00
parent 42fc522a43
commit 8ea92efb90
2 changed files with 174 additions and 0 deletions

71
internal/hooks/hooks.go Normal file
View File

@@ -0,0 +1,71 @@
package hooks
import (
"bufio"
"os/exec"
"strings"
"github.com/Nightapes/go-semantic-release/internal/shared"
"github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus"
)
//Hooks struct
type Hooks struct {
version *shared.ReleaseVersion
config *config.ReleaseConfig
}
// New hooks struct
func New(config *config.ReleaseConfig, version *shared.ReleaseVersion) *Hooks {
return &Hooks{
config: config,
version: version,
}
}
// PreRelease runs before creating release
func (h *Hooks) PreRelease() error {
log.Infof("Run pre release hooks")
for _, cmd := range h.config.Hooks.PreRelease {
log.Debugf("Run %s", cmd)
err := h.runCommand(cmd)
if err != nil {
return err
}
}
return nil
}
// PostRelease runs after creating release
func (h *Hooks) PostRelease() error {
log.Infof("Run post release hooks")
for _, cmd := range h.config.Hooks.PostRelease {
err := h.runCommand(cmd)
if err != nil {
return err
}
}
return nil
}
func (h *Hooks) runCommand(command string) error {
splittedCmd := strings.Split(strings.ReplaceAll(command, "$RELEASE_VERSION", h.version.Next.Version.String()), " ")
cmd := exec.Command(splittedCmd[0], splittedCmd[1:]...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.WithField("cmd", splittedCmd[0]).Infof("%s\n", scanner.Text())
}
}()
return cmd.Run()
}