Skip to content

Commit 04c5b10

Browse files
committed
Merge branch 'main' into deployment
2 parents 4e04787 + 99ece25 commit 04c5b10

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

cli/cliui/prompt.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cliui
22

33
import (
44
"bufio"
5+
"encoding/json"
56
"fmt"
7+
"io"
68
"os"
79
"os/signal"
810
"strings"
@@ -45,12 +47,22 @@ func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
4547
} else {
4648
reader := bufio.NewReader(cmd.InOrStdin())
4749
line, err = reader.ReadString('\n')
48-
// Multiline with single quotes!
49-
if err == nil && strings.HasPrefix(line, "'") {
50-
rest, err := reader.ReadString('\'')
50+
51+
// Check if the first line beings with JSON object or array chars.
52+
// This enables multiline JSON to be pasted into an input, and have
53+
// it parse properly.
54+
if err == nil && (strings.HasPrefix(line, "{") || strings.HasPrefix(line, "[")) {
55+
pipeReader, pipeWriter := io.Pipe()
56+
defer pipeWriter.Close()
57+
defer pipeReader.Close()
58+
go func() {
59+
_, _ = pipeWriter.Write([]byte(line))
60+
_, _ = reader.WriteTo(pipeWriter)
61+
}()
62+
var rawMessage json.RawMessage
63+
err := json.NewDecoder(pipeReader).Decode(&rawMessage)
5164
if err == nil {
52-
line += rest
53-
line = strings.Trim(line, "'")
65+
line = string(rawMessage)
5466
}
5567
}
5668
}

cli/cliui/prompt_test.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cliui_test
22

33
import (
44
"context"
5-
"runtime"
65
"testing"
76

87
"github.com/spf13/cobra"
@@ -47,7 +46,7 @@ func TestPrompt(t *testing.T) {
4746
require.Equal(t, "yes", <-doneChan)
4847
})
4948

50-
t.Run("Multiline", func(t *testing.T) {
49+
t.Run("JSON", func(t *testing.T) {
5150
t.Parallel()
5251
ptty := ptytest.New(t)
5352
doneChan := make(chan string)
@@ -59,13 +58,44 @@ func TestPrompt(t *testing.T) {
5958
doneChan <- resp
6059
}()
6160
ptty.ExpectMatch("Example")
62-
ptty.WriteLine("'this is a")
63-
ptty.WriteLine("test'")
64-
newline := "\n"
65-
if runtime.GOOS == "windows" {
66-
newline = "\r\n"
67-
}
68-
require.Equal(t, "this is a"+newline+"test", <-doneChan)
61+
ptty.WriteLine("{}")
62+
require.Equal(t, "{}", <-doneChan)
63+
})
64+
65+
t.Run("BadJSON", func(t *testing.T) {
66+
t.Parallel()
67+
ptty := ptytest.New(t)
68+
doneChan := make(chan string)
69+
go func() {
70+
resp, err := newPrompt(ptty, cliui.PromptOptions{
71+
Text: "Example",
72+
})
73+
require.NoError(t, err)
74+
doneChan <- resp
75+
}()
76+
ptty.ExpectMatch("Example")
77+
ptty.WriteLine("{a")
78+
require.Equal(t, "{a", <-doneChan)
79+
})
80+
81+
t.Run("MultilineJSON", func(t *testing.T) {
82+
t.Parallel()
83+
ptty := ptytest.New(t)
84+
doneChan := make(chan string)
85+
go func() {
86+
resp, err := newPrompt(ptty, cliui.PromptOptions{
87+
Text: "Example",
88+
})
89+
require.NoError(t, err)
90+
doneChan <- resp
91+
}()
92+
ptty.ExpectMatch("Example")
93+
ptty.WriteLine(`{
94+
"test": "wow"
95+
}`)
96+
require.Equal(t, `{
97+
"test": "wow"
98+
}`, <-doneChan)
6999
})
70100
}
71101

0 commit comments

Comments
 (0)