Skip to content

Commit 2be2228

Browse files
committed
Removed direct access to stdio streams in monitor command
1 parent a246a46 commit 2be2228

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

cli/feedback/terminal.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,38 @@ import (
1919
"bufio"
2020
"errors"
2121
"fmt"
22+
"io"
2223
"os"
2324

2425
"golang.org/x/term"
2526
)
2627

28+
// InteractiveStreams returns the underlying io.Reader and io.Writer to directly stream to
29+
// stdin and stdout. It errors if the selected output format is not Text or the terminal is
30+
// not interactive.
31+
func InteractiveStreams() (io.Reader, io.Writer, error) {
32+
if !formatSelected {
33+
panic("output format not yet selected")
34+
}
35+
if format != Text {
36+
return nil, nil, errors.New(tr("interactive terminal not supported for the '%s' output format", format))
37+
}
38+
if !isTerminal() {
39+
return nil, nil, errors.New(tr("not running in a terminal"))
40+
}
41+
return os.Stdin, stdOut, nil
42+
}
43+
44+
func isTerminal() bool {
45+
return term.IsTerminal(int(os.Stdin.Fd()))
46+
}
47+
2748
// InputUserField prompts the user to input the provided user field.
2849
func InputUserField(prompt string, secret bool) (string, error) {
2950
if format != Text {
3051
return "", errors.New(tr("user input not supported for the '%s' output format", format))
3152
}
32-
if !term.IsTerminal(int(os.Stdin.Fd())) {
53+
if !isTerminal() {
3354
return "", errors.New(tr("user input not supported in non interactive mode"))
3455
}
3556

cli/monitor/term.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,36 @@
1616
package monitor
1717

1818
import (
19-
"os"
19+
"io"
20+
21+
"github.com/arduino/arduino-cli/cli/feedback"
2022
)
2123

2224
type stdInOut struct {
25+
in io.Reader
26+
out io.Writer
2327
}
2428

2529
func newStdInOutTerminal() (*stdInOut, error) {
26-
return &stdInOut{}, nil
30+
in, out, err := feedback.InteractiveStreams()
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return &stdInOut{
36+
in: in,
37+
out: out,
38+
}, nil
2739
}
2840

2941
func (n *stdInOut) Close() error {
3042
return nil
3143
}
3244

3345
func (n *stdInOut) Read(buff []byte) (int, error) {
34-
return os.Stdin.Read(buff)
46+
return n.in.Read(buff)
3547
}
3648

3749
func (n *stdInOut) Write(buff []byte) (int, error) {
38-
return os.Stdout.Write(buff)
50+
return n.out.Write(buff)
3951
}

0 commit comments

Comments
 (0)