Skip to content

Commit c117489

Browse files
committed
Use direct streams where appropiate
1 parent 472c33e commit c117489

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

internal/cli/completion/completion.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ func NewCommand() *cobra.Command {
4747
}
4848

4949
func runCompletionCommand(cmd *cobra.Command, args []string) {
50-
stdOut, _, res := feedback.OutputStreams()
5150
logrus.Info("Executing `arduino-cli completion`")
51+
stdOut, _, err := feedback.DirectStreams()
52+
if err != nil {
53+
feedback.Fatal(err.Error(), feedback.ErrGeneric)
54+
}
5255
if completionNoDesc && (args[0] == "powershell") {
5356
feedback.Fatal(tr("Error: command description is not supported by %v", args[0]), feedback.ErrGeneric)
5457
}
@@ -66,5 +69,4 @@ func runCompletionCommand(cmd *cobra.Command, args []string) {
6669
case "powershell":
6770
cmd.Root().GenPowerShellCompletion(stdOut)
6871
}
69-
feedback.PrintResult(res())
7072
}

internal/cli/daemon/daemon.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
8989
defer f.Close()
9090
debugStdOut = f
9191
} else {
92-
// Attach to os.Stdout only if we are in Text mode
93-
if feedback.GetFormat() != feedback.Text {
94-
feedback.Fatal(tr("Debug log is only available in text format"), feedback.ErrBadArgument)
92+
if out, _, err := feedback.DirectStreams(); err != nil {
93+
feedback.Fatal(tr("Can't write debug log: %s", err), feedback.ErrBadArgument)
94+
} else {
95+
debugStdOut = out
9596
}
96-
out, _, _ := feedback.OutputStreams()
97-
debugStdOut = out
9897
}
9998
gRPCOptions = append(gRPCOptions,
10099
grpc.UnaryInterceptor(unaryLoggerInterceptor),

internal/cli/feedback/stdio.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,37 @@
1616
package feedback
1717

1818
import (
19+
"errors"
1920
"io"
2021
)
2122

22-
// OutputStreams returns the underlying io.Writer to directly stream to
23+
// DirectStreams returns the underlying io.Writer to directly stream to
2324
// stdout and stderr.
24-
// If the selected output format is not Text, the returned writers will
25-
// accumulate the output until command execution is completed.
25+
// If the selected output format is not Text, the function will error.
26+
// Using this function will allow usage of the PrintResult anymore.
27+
func DirectStreams() (io.Writer, io.Writer, error) {
28+
if !formatSelected {
29+
panic("output format not yet selected")
30+
}
31+
if format != Text {
32+
return nil, nil, errors.New(tr("available only in text format"))
33+
}
34+
return stdOut, stdErr, nil
35+
}
36+
37+
// OutputStreams returns a pair of io.Writer to write the command output.
38+
// The returned writers will accumulate the output until the command
39+
// execution is completed, so they are not suitable for printing an unbounded
40+
// stream like a debug logger or an event watcher (use DirectStreams for
41+
// that purpose).
42+
//
43+
// If the output format is Text the output will be directly streamed to the
44+
// underlying stdio streams in real time.
45+
//
2646
// This function returns also a callback that must be called when the
27-
// command execution is completed, it will return a *OutputStreamsResult
28-
// object that can be used as a Result or to retrieve the output to embed
29-
// it in another object.
47+
// command execution is completed, it will return an *OutputStreamsResult
48+
// object that can be used as a Result or to retrieve the accumulated output
49+
// to embed it in another object.
3050
func OutputStreams() (io.Writer, io.Writer, func() *OutputStreamsResult) {
3151
if !formatSelected {
3252
panic("output format not yet selected")

0 commit comments

Comments
 (0)