Skip to content

Commit 9716dfa

Browse files
feat: add plugin logging
1 parent dc91568 commit 9716dfa

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

pkg/plugin/client.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package plugin
22

33
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"log"
8+
"os"
49
"os/exec"
10+
"strings"
511
"sync"
612

713
"github.com/go-semantic-release/semantic-release/v2/pkg/analyzer"
@@ -15,8 +21,9 @@ import (
1521
)
1622

1723
type PluginOpts struct {
18-
Type string
19-
Cmd *exec.Cmd
24+
Type string
25+
PluginName string
26+
Cmd *exec.Cmd
2027
}
2128

2229
var runningClientsMx sync.Mutex
@@ -33,6 +40,19 @@ func KillAllPlugins() {
3340
func startPlugin(opts *PluginOpts) (interface{}, error) {
3441
runningClientsMx.Lock()
3542
defer runningClientsMx.Unlock()
43+
logR, logW := io.Pipe()
44+
pluginLogger := log.New(os.Stderr, fmt.Sprintf("[%s]: ", opts.PluginName), 0)
45+
go func() {
46+
logLineScanner := bufio.NewScanner(logR)
47+
for logLineScanner.Scan() {
48+
line := logLineScanner.Text()
49+
// skip JSON logging
50+
if strings.HasPrefix(line, "{") {
51+
continue
52+
}
53+
pluginLogger.Println(line)
54+
}
55+
}()
3656
client := plugin.NewClient(&plugin.ClientConfig{
3757
HandshakeConfig: Handshake,
3858
VersionedPlugins: map[int]plugin.PluginSet{
@@ -45,6 +65,7 @@ func startPlugin(opts *PluginOpts) (interface{}, error) {
4565
Cmd: opts.Cmd,
4666
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
4767
Logger: hclog.NewNullLogger(),
68+
Stderr: logW,
4869
})
4970

5071
rpcClient, err := client.Client()

pkg/plugin/discovery/discovery.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ func FindPlugin(t, name string) (*plugin.PluginOpts, error) {
262262
}
263263

264264
return &plugin.PluginOpts{
265-
Type: t,
266-
Cmd: exec.Command(binPath),
265+
Type: t,
266+
PluginName: pName,
267+
Cmd: exec.Command(binPath),
267268
}, nil
268269
}

0 commit comments

Comments
 (0)