Skip to content

Commit

Permalink
Merge pull request #45 from git-plm/cbrake/main
Browse files Browse the repository at this point in the history
Improve hooks processing
  • Loading branch information
cbrake authored Feb 2, 2024
2 parents 2e0cdb6 + 849538c commit 053e919
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ For more details or to discuss releases, please visit the

## [Unreleased]

## [[0.4.0] - 2024-02-02](https://github.com/git-plm/gitplm/releases/tag/v0.4.0)

- output hook stdout/err to gitplm stdout/err
- add IPN template variable in hooks

## [[0.3.0] - 2023-01-04](https://github.com/git-plm/gitplm/releases/tag/v0.3.0)

- support hooks, and required sections in release configuration (yml) file.
Expand Down
1 change: 1 addition & 0 deletions example/ASY-001.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ hooks:
- date -Iseconds > {{ .RelDir }}/timestamp.txt
- |
echo "processing {{ .SrcDir }}"
echo "IPN {{ .IPN }}"
echo "hi #1"
echo "hi #2"
required:
Expand Down
38 changes: 33 additions & 5 deletions rel-script.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"sort"
Expand Down Expand Up @@ -86,13 +88,15 @@ func (rs *relScript) copy(srcDir, destDir string) error {
return nil
}

func (rs *relScript) hooks(srcDir, destDir string) error {
func (rs *relScript) hooks(pn string, srcDir, destDir string) error {
data := struct {
SrcDir string
RelDir string
IPN string
}{
SrcDir: srcDir,
RelDir: destDir,
IPN: pn,
}

for _, h := range rs.Hooks {
Expand All @@ -108,12 +112,36 @@ func (rs *relScript) hooks(srcDir, destDir string) error {
return fmt.Errorf("Error parsing hook: %v: %v", h, err)
}

output, err := exec.Command("/bin/sh", "-c", out.String()).Output()
if len(output) > 0 {
log.Println(string(output))
cmd := exec.Command("/bin/sh", "-c", out.String())

stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("Error running %v: %v", out.String(), err)
log.Fatal(err)
}

// Start the command
if err := cmd.Start(); err != nil {
log.Fatal(err)
}

// Copy the command's stdout and stderr to the Go program's stdout
go func() {
_, _ = io.Copy(os.Stdout, stdout)
}()

go func() {
_, _ = io.Copy(os.Stderr, stderr)
}()

// Wait for the command to exit
if err := cmd.Wait(); err != nil {
log.Println("Error running hook: ", err)
log.Println("Hook contents: ")
fmt.Print(out.String())
}
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -99,7 +98,7 @@ func processRelease(relPn string, relLog *strings.Builder) (string, error) {
}

if ymlExists {
ymlBytes, err := ioutil.ReadFile(ymlFilePath)
ymlBytes, err := os.ReadFile(ymlFilePath)
if err != nil {
return sourceDir, fmt.Errorf("Error loading yml file: %v", err)
}
Expand All @@ -118,7 +117,7 @@ func processRelease(relPn string, relLog *strings.Builder) (string, error) {
}

// run hooks
err = rs.hooks(sourceDir, releaseDir)
err = rs.hooks(relPn, sourceDir, releaseDir)
if err != nil {
return sourceDir, fmt.Errorf("Error running hooks specified in YML: %v", err)
}
Expand Down

0 comments on commit 053e919

Please sign in to comment.