-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (101 loc) · 2.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"text/template"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
"github.com/go-semantic-release/semantic-release/v2/pkg/hooks"
"github.com/mattn/go-shellwords"
)
var version = "dev"
type Exec struct {
cmdOnSuccess *template.Template
cmdOnNoRelease *template.Template
log *log.Logger
}
func (e *Exec) Init(m map[string]string) error {
var err error
e.cmdOnSuccess, err = template.New("command").Parse(m["exec_on_success"])
if err != nil {
return fmt.Errorf("failed to parse exec_on_success: %w", err)
}
e.cmdOnNoRelease, err = template.New("command").Parse(m["exec_on_no_release"])
if err != nil {
return fmt.Errorf("failed to parse exec_on_no_release: %w", err)
}
return nil
}
func (e *Exec) Name() string {
return "exec"
}
func (e *Exec) Version() string {
return version
}
func (e *Exec) runCommand(cmdStr string) error {
e.log.Println("#", cmdStr)
cmdArgs, err := shellwords.Parse(cmdStr)
if err != nil {
return fmt.Errorf("failed to parse command: %w", err)
}
if len(cmdArgs) == 0 {
e.log.Println("warning: command is empty")
return nil
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Env = os.Environ()
logPipeRead, logPipeWrite := io.Pipe()
defer logPipeWrite.Close()
cmd.Stdout = logPipeWrite
cmd.Stderr = logPipeWrite
logLineScanner := bufio.NewScanner(logPipeRead)
go func() {
for logLineScanner.Scan() {
e.log.Println(logLineScanner.Text())
}
}()
return cmd.Run()
}
func executeTemplate(tpl *template.Template, data any) (string, error) {
buf := &bytes.Buffer{}
err := tpl.Execute(buf, data)
if err != nil {
return "", fmt.Errorf("failed to execute command template: %w", err)
}
return buf.String(), nil
}
func (e *Exec) Success(config *hooks.SuccessHookConfig) error {
cmdStr, err := executeTemplate(e.cmdOnSuccess, config)
if err != nil {
return err
}
if cmdStr == "" {
return nil
}
e.log.Println("executing success command:")
return e.runCommand(cmdStr)
}
func (e *Exec) NoRelease(config *hooks.NoReleaseConfig) error {
cmdStr, err := executeTemplate(e.cmdOnNoRelease, config)
if err != nil {
return err
}
if cmdStr == "" {
return nil
}
e.log.Println("executing no release command:")
return e.runCommand(cmdStr)
}
func main() {
plugin.Serve(&plugin.ServeOpts{
Hooks: func() hooks.Hooks {
return &Exec{
log: log.New(os.Stderr, "", 0),
}
},
})
}