From ddc02374687385578cdd329db4b0e134c892c3a4 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Tue, 18 Apr 2023 17:13:49 +0200 Subject: [PATCH] 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 ``` --- libs/cmdio/logger.go | 5 +++++ libs/cmdio/logger_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 libs/cmdio/logger_test.go diff --git a/libs/cmdio/logger.go b/libs/cmdio/logger.go index 00755b78..8904ddd7 100644 --- a/libs/cmdio/logger.go +++ b/libs/cmdio/logger.go @@ -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') diff --git a/libs/cmdio/logger_test.go b/libs/cmdio/logger_test.go new file mode 100644 index 00000000..cdf8087d --- /dev/null +++ b/libs/cmdio/logger_test.go @@ -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") +}