Skip to content

Commit 63f4904

Browse files
committed
Added Context.ExecStdout/ExecStderr to easy redirect output of executed commands
1 parent 6be83c9 commit 63f4904

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

i18n/i18n.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,45 @@ type Logger interface {
5252
Flush() string
5353
}
5454

55-
type LoggerToIoWriter struct {
56-
Writer io.Writer
55+
type LoggerToCustomStreams struct {
56+
Stdout io.Writer
57+
Stderr io.Writer
5758
}
5859

59-
func (s LoggerToIoWriter) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
60-
fmt.Fprintln(s.Writer, Format(format, a...))
60+
func (s LoggerToCustomStreams) Fprintln(w io.Writer, level string, format string, a ...interface{}) {
61+
target := s.Stdout
62+
if w == os.Stderr {
63+
target = s.Stderr
64+
}
65+
fmt.Fprintln(target, Format(format, a...))
6166
}
6267

63-
func (s LoggerToIoWriter) UnformattedFprintln(w io.Writer, str string) {
64-
fmt.Fprintln(s.Writer, str)
68+
func (s LoggerToCustomStreams) UnformattedFprintln(w io.Writer, str string) {
69+
target := s.Stdout
70+
if w == os.Stderr {
71+
target = s.Stderr
72+
}
73+
fmt.Fprintln(target, str)
6574
}
6675

67-
func (s LoggerToIoWriter) UnformattedWrite(w io.Writer, data []byte) {
68-
s.Writer.Write(data)
76+
func (s LoggerToCustomStreams) UnformattedWrite(w io.Writer, data []byte) {
77+
target := s.Stdout
78+
if w == os.Stderr {
79+
target = s.Stderr
80+
}
81+
target.Write(data)
6982
}
7083

71-
func (s LoggerToIoWriter) Println(level string, format string, a ...interface{}) {
84+
func (s LoggerToCustomStreams) Println(level string, format string, a ...interface{}) {
7285
s.Fprintln(nil, level, format, a...)
7386
}
7487

75-
func (s LoggerToIoWriter) Flush() string {
88+
func (s LoggerToCustomStreams) Flush() string {
7689
return ""
7790
}
7891

79-
func (s LoggerToIoWriter) Name() string {
80-
return "LoggerToIoWriter"
92+
func (s LoggerToCustomStreams) Name() string {
93+
return "LoggerToCustomStreams"
8194
}
8295

8396
type NoopLogger struct{}

types/context.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"io"
45
"strings"
56

67
"github.com/arduino/arduino-builder/i18n"
@@ -9,8 +10,8 @@ import (
910
"github.com/arduino/arduino-cli/arduino/libraries"
1011
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
1112
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
12-
"github.com/arduino/go-paths-helper"
13-
"github.com/arduino/go-properties-orderedmap"
13+
paths "github.com/arduino/go-paths-helper"
14+
properties "github.com/arduino/go-properties-orderedmap"
1415
)
1516

1617
type ProgressStruct struct {
@@ -110,6 +111,10 @@ type Context struct {
110111

111112
// Experimental: use arduino-preprocessor to create prototypes
112113
UseArduinoPreprocessor bool
114+
115+
// Out and Err stream to redirect all Exec commands
116+
ExecStdout io.Writer
117+
ExecStderr io.Writer
113118
}
114119

115120
func (ctx *Context) ExtractBuildOptions() *properties.Map {

utils/utils.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import (
4848
"github.com/arduino/arduino-builder/gohasissues"
4949
"github.com/arduino/arduino-builder/i18n"
5050
"github.com/arduino/arduino-builder/types"
51-
"github.com/arduino/go-paths-helper"
51+
paths "github.com/arduino/go-paths-helper"
5252
"golang.org/x/text/transform"
5353
"golang.org/x/text/unicode/norm"
5454
)
@@ -288,6 +288,13 @@ const (
288288
)
289289

290290
func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int) ([]byte, []byte, error) {
291+
if ctx.ExecStdout == nil {
292+
ctx.ExecStdout = os.Stdout
293+
}
294+
if ctx.ExecStderr == nil {
295+
ctx.ExecStderr = os.Stderr
296+
}
297+
291298
if ctx.Verbose {
292299
ctx.GetLogger().UnformattedFprintln(os.Stdout, PrintableCommand(command.Args))
293300
}
@@ -296,14 +303,14 @@ func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int)
296303
buffer := &bytes.Buffer{}
297304
command.Stdout = buffer
298305
} else if stdout == Show || stdout == ShowIfVerbose && ctx.Verbose {
299-
command.Stdout = os.Stdout
306+
command.Stdout = ctx.ExecStdout
300307
}
301308

302309
if stderr == Capture {
303310
buffer := &bytes.Buffer{}
304311
command.Stderr = buffer
305312
} else if stderr == Show || stderr == ShowIfVerbose && ctx.Verbose {
306-
command.Stderr = os.Stderr
313+
command.Stderr = ctx.ExecStderr
307314
}
308315

309316
err := command.Start()

0 commit comments

Comments
 (0)