Error out if question prompts are used in json mode (#340)

## Changes
This PR disallows questions in json mode

## Tests
Manually and unit test
```
shreyas.goenka@THW32HFW6T job-output % bricks bundle destroy --progress-format=json
The following resources will be removed:
{
  "resource_type": "databricks_job",
  "action": "delete",
  "resource_name": "foo"
}
Error: question prompts are not supported in json mode
```
This commit is contained in:
shreyas-goenka 2023-04-18 17:13:49 +02:00 committed by GitHub
parent 598ad62688
commit ddc0237468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
@ -72,6 +73,10 @@ func Ask(ctx context.Context, question string) (bool, error) {
}
func (l *Logger) Ask(question string) (bool, error) {
if l.Mode == flags.ModeJson {
return false, fmt.Errorf("question prompts are not supported in json mode")
}
l.Writer.Write([]byte(question))
ans, err := l.Reader.ReadString('\n')

14
libs/cmdio/logger_test.go Normal file
View File

@ -0,0 +1,14 @@
package cmdio
import (
"testing"
"github.com/databricks/bricks/libs/flags"
"github.com/stretchr/testify/assert"
)
func TestAskFailedInJsonMode(t *testing.T) {
l := NewLogger(flags.ModeJson)
_, err := l.Ask("What is your spirit animal?")
assert.ErrorContains(t, err, "question prompts are not supported in json mode")
}