Skip to content

Commit

Permalink
fix: Parse prompt input JSON using object or array chars (#538)
Browse files Browse the repository at this point in the history
Fixes #492. There is no more single-quote parsing, and instead we use a JSON decoder for multiline values. This is a much better UX!
  • Loading branch information
kylecarbs committed Mar 24, 2022
1 parent 305b67c commit 99ece25
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
22 changes: 17 additions & 5 deletions cli/cliui/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cliui

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -45,12 +47,22 @@ func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
} else {
reader := bufio.NewReader(cmd.InOrStdin())
line, err = reader.ReadString('\n')
// Multiline with single quotes!
if err == nil && strings.HasPrefix(line, "'") {
rest, err := reader.ReadString('\'')

// Check if the first line beings with JSON object or array chars.
// This enables multiline JSON to be pasted into an input, and have
// it parse properly.
if err == nil && (strings.HasPrefix(line, "{") || strings.HasPrefix(line, "[")) {
pipeReader, pipeWriter := io.Pipe()
defer pipeWriter.Close()
defer pipeReader.Close()
go func() {
_, _ = pipeWriter.Write([]byte(line))
_, _ = reader.WriteTo(pipeWriter)
}()
var rawMessage json.RawMessage
err := json.NewDecoder(pipeReader).Decode(&rawMessage)
if err == nil {
line += rest
line = strings.Trim(line, "'")
line = string(rawMessage)
}
}
}
Expand Down
48 changes: 39 additions & 9 deletions cli/cliui/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cliui_test

import (
"context"
"runtime"
"testing"

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

t.Run("Multiline", func(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
t.Parallel()
ptty := ptytest.New(t)
doneChan := make(chan string)
Expand All @@ -59,13 +58,44 @@ func TestPrompt(t *testing.T) {
doneChan <- resp
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("'this is a")
ptty.WriteLine("test'")
newline := "\n"
if runtime.GOOS == "windows" {
newline = "\r\n"
}
require.Equal(t, "this is a"+newline+"test", <-doneChan)
ptty.WriteLine("{}")
require.Equal(t, "{}", <-doneChan)
})

t.Run("BadJSON", func(t *testing.T) {
t.Parallel()
ptty := ptytest.New(t)
doneChan := make(chan string)
go func() {
resp, err := newPrompt(ptty, cliui.PromptOptions{
Text: "Example",
})
require.NoError(t, err)
doneChan <- resp
}()
ptty.ExpectMatch("Example")
ptty.WriteLine("{a")
require.Equal(t, "{a", <-doneChan)
})

t.Run("MultilineJSON", func(t *testing.T) {
t.Parallel()
ptty := ptytest.New(t)
doneChan := make(chan string)
go func() {
resp, err := newPrompt(ptty, cliui.PromptOptions{
Text: "Example",
})
require.NoError(t, err)
doneChan <- resp
}()
ptty.ExpectMatch("Example")
ptty.WriteLine(`{
"test": "wow"
}`)
require.Equal(t, `{
"test": "wow"
}`, <-doneChan)
})
}

Expand Down
3 changes: 2 additions & 1 deletion cli/workspacecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cli_test
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
"github.com/stretchr/testify/require"
)

func TestWorkspaceCreate(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"sync"
"time"

"github.com/coder/coder/cryptorand"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"golang.org/x/xerrors"

"github.com/coder/coder/cryptorand"
)

// Required to prevent port collision during container creation.
Expand Down

0 comments on commit 99ece25

Please sign in to comment.