Skip to content

Commit afe399e

Browse files
authored
Remove unused JSON progress logging mode (#3812)
## Changes The progress logger now only supports `append` mode. ## Why JSON mode was added in #276 (March 2023). The intent was to make progress events machine readable but this didn't materialize (we didn't end up using it in the VS Code extension). This functionality would print events to stderr if the user specified `--progress-format json`, or set the an equivalent environment variable. Because the flag is hidden, and there are no online references to the functionality, I believe it is safe to remove. If users take a dependency on JSON output, it should be enabled via the existing `--output json` flag and be written to stdout. After this change is merged, the remaining functionality can be moved into the core `cmdio` types, and the "progress logger" can be removed. Once there is a single type for all I/O, we have a better shot at improving it. Related change: #3811. ## Tests Tests pass.
1 parent a8100b3 commit afe399e

File tree

9 files changed

+9
-102
lines changed

9 files changed

+9
-102
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### CLI
88

99
* Remove `inplace` mode for the `--progress-format` flag. ([#3811](https://github.com/databricks/cli/pull/3811))
10+
* Remove `json` mode for the `--progress-format` flag. ([#3812](https://github.com/databricks/cli/pull/3812))
1011

1112
### Dependency updates
1213

cmd/bundle/destroy.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
"github.com/databricks/cli/bundle/phases"
1313
"github.com/databricks/cli/cmd/bundle/utils"
1414
"github.com/databricks/cli/cmd/root"
15-
"github.com/databricks/cli/libs/cmdio"
1615
"github.com/databricks/cli/libs/diag"
17-
"github.com/databricks/cli/libs/flags"
1816
"github.com/databricks/cli/libs/logdiag"
1917
"github.com/spf13/cobra"
2018
"golang.org/x/term"
@@ -68,15 +66,6 @@ Typical use cases:
6866
return errors.New("please specify --auto-approve to skip interactive confirmation checks for non tty consoles")
6967
}
7068

71-
// Check auto-approve is selected for json logging
72-
logger, ok := cmdio.FromContext(ctx)
73-
if !ok {
74-
return errors.New("progress logger not found")
75-
}
76-
if logger.Mode == flags.ModeJson && !autoApprove {
77-
return errors.New("please specify --auto-approve since selected logging format is json")
78-
}
79-
8069
phases.Initialize(ctx, b)
8170
if logdiag.HasError(ctx) {
8271
return root.ErrAlreadyPrinted

cmd/pipelines/destroy.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
"github.com/databricks/cli/bundle/phases"
1313
"github.com/databricks/cli/cmd/bundle/utils"
1414
"github.com/databricks/cli/cmd/root"
15-
"github.com/databricks/cli/libs/cmdio"
1615
"github.com/databricks/cli/libs/diag"
17-
"github.com/databricks/cli/libs/flags"
1816
"github.com/databricks/cli/libs/logdiag"
1917
"github.com/spf13/cobra"
2018
"golang.org/x/term"
@@ -56,15 +54,6 @@ func destroyCommand() *cobra.Command {
5654
return errors.New("please specify --auto-approve to skip interactive confirmation checks for non tty consoles")
5755
}
5856

59-
// Check auto-approve is selected for json logging
60-
logger, ok := cmdio.FromContext(ctx)
61-
if !ok {
62-
return errors.New("progress logger not found")
63-
}
64-
if logger.Mode == flags.ModeJson && !autoApprove {
65-
return errors.New("please specify --auto-approve since selected logging format is json")
66-
}
67-
6857
phases.Initialize(ctx, b)
6958
if logdiag.HasError(ctx) {
7059
return root.ErrAlreadyPrinted

cmd/pipelines/root/progress_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func initProgressLoggerFlag(cmd *cobra.Command, logFlags *logFlags) *progressLog
4444
}
4545

4646
flags := cmd.PersistentFlags()
47-
flags.Var(&f.ProgressLogFormat, "progress-format", "format for progress logs (append, json)")
47+
flags.Var(&f.ProgressLogFormat, "progress-format", "format for progress logs (append)")
4848
flags.MarkHidden("progress-format")
4949
cmd.RegisterFlagCompletionFunc("progress-format", f.Complete)
5050
return &f

cmd/root/progress_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func initProgressLoggerFlag(cmd *cobra.Command, logFlags *logFlags) *progressLog
4343
}
4444

4545
flags := cmd.PersistentFlags()
46-
flags.Var(&f.ProgressLogFormat, "progress-format", "format for progress logs (append, json)")
46+
flags.Var(&f.ProgressLogFormat, "progress-format", "format for progress logs (append)")
4747
flags.MarkHidden("progress-format")
4848
cmd.RegisterFlagCompletionFunc("progress-format", f.Complete)
4949
return &f

libs/cmdio/logger.go

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package cmdio
33
import (
44
"bufio"
55
"context"
6-
"encoding/json"
7-
"errors"
86
"fmt"
97
"io"
108
"os"
@@ -17,7 +15,7 @@ import (
1715

1816
// This is the interface for all io interactions with a user
1917
type Logger struct {
20-
// Mode for the logger. One of (append, json).
18+
// Mode for the logger. One of (append).
2119
Mode flags.ProgressLogFormat
2220

2321
// Input stream (eg. stdin). Answers to questions prompted using the Ask() method
@@ -121,10 +119,6 @@ func splitAtLastNewLine(s string) (string, string) {
121119
}
122120

123121
func (l *Logger) AskSelect(question string, choices []string) (string, error) {
124-
if l.Mode == flags.ModeJson {
125-
return "", errors.New("question prompts are not supported in json mode")
126-
}
127-
128122
// Promptui does not support multiline prompts. So we split the question.
129123
first, last := splitAtLastNewLine(question)
130124
_, err := l.Writer.Write([]byte(first))
@@ -150,10 +144,6 @@ func (l *Logger) AskSelect(question string, choices []string) (string, error) {
150144
}
151145

152146
func (l *Logger) Ask(question, defaultVal string) (string, error) {
153-
if l.Mode == flags.ModeJson {
154-
return "", errors.New("question prompts are not supported in json mode")
155-
}
156-
157147
// Add default value to question prompt.
158148
if defaultVal != "" {
159149
question += fmt.Sprintf(` [%s]`, defaultVal)
@@ -180,34 +170,9 @@ func (l *Logger) Ask(question, defaultVal string) (string, error) {
180170
return ans, nil
181171
}
182172

183-
func (l *Logger) writeJson(event Event) {
184-
b, err := json.MarshalIndent(event, "", " ")
185-
if err != nil {
186-
// we panic because there we cannot catch this in jobs.RunNowAndWait
187-
panic(err)
188-
}
189-
_, _ = l.Writer.Write(b)
190-
_, _ = l.Writer.Write([]byte("\n"))
191-
}
192-
193-
func (l *Logger) writeAppend(event Event) {
194-
_, _ = l.Writer.Write([]byte(event.String()))
195-
_, _ = l.Writer.Write([]byte("\n"))
196-
}
197-
198173
func (l *Logger) Log(event Event) {
199174
l.mutex.Lock()
200175
defer l.mutex.Unlock()
201-
switch l.Mode {
202-
case flags.ModeJson:
203-
l.writeJson(event)
204-
205-
case flags.ModeAppend:
206-
l.writeAppend(event)
207-
208-
default:
209-
// we panic because errors are not captured in some log sides like
210-
// jobs.RunNowAndWait
211-
panic("unknown progress logger mode: " + l.Mode.String())
212-
}
176+
_, _ = l.Writer.Write([]byte(event.String()))
177+
_, _ = l.Writer.Write([]byte("\n"))
213178
}

libs/cmdio/logger_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
package cmdio
22

33
import (
4-
"context"
54
"testing"
65

7-
"github.com/databricks/cli/libs/flags"
86
"github.com/stretchr/testify/assert"
97
)
108

11-
func TestAskFailedInJsonMode(t *testing.T) {
12-
l := NewLogger(flags.ModeJson)
13-
_, err := l.Ask("What is your spirit animal?", "")
14-
assert.ErrorContains(t, err, "question prompts are not supported in json mode")
15-
}
16-
17-
func TestAskChoiceFailsInJsonMode(t *testing.T) {
18-
l := NewLogger(flags.ModeJson)
19-
ctx := NewContext(context.Background(), l)
20-
21-
_, err := AskSelect(ctx, "what is a question?", []string{"b", "c", "a"})
22-
assert.EqualError(t, err, "question prompts are not supported in json mode")
23-
}
24-
259
func TestSplitAtLastNewLine(t *testing.T) {
2610
first, last := splitAtLastNewLine("hello\nworld")
2711
assert.Equal(t, "hello\n", first)

libs/flags/progress_format.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type ProgressLogFormat string
1111

1212
var (
1313
ModeAppend = ProgressLogFormat("append")
14-
ModeJson = ProgressLogFormat("json")
1514
ModeDefault = ProgressLogFormat("default")
1615
)
1716

@@ -28,19 +27,13 @@ func (p *ProgressLogFormat) Set(s string) error {
2827
switch lower {
2928
case ModeAppend.String():
3029
*p = ProgressLogFormat(ModeAppend.String())
31-
case ModeJson.String():
32-
*p = ProgressLogFormat(ModeJson.String())
3330
case ModeDefault.String():
3431
// We include ModeDefault here for symmetry reasons so this flag value
3532
// can be unset after test runs. We should not point this value in error
3633
// messages though since it's internal only
37-
*p = ProgressLogFormat(ModeJson.String())
34+
*p = ProgressLogFormat(ModeAppend.String())
3835
default:
39-
valid := []string{
40-
ModeAppend.String(),
41-
ModeJson.String(),
42-
}
43-
return fmt.Errorf("accepted arguments are [%s]", strings.Join(valid, ", "))
36+
return fmt.Errorf("accepted arguments are [%s]", ModeAppend.String())
4437
}
4538
return nil
4639
}
@@ -53,6 +46,5 @@ func (p *ProgressLogFormat) Type() string {
5346
func (p *ProgressLogFormat) Complete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5447
return []string{
5548
"append",
56-
"json",
5749
}, cobra.ShellCompDirectiveNoFileComp
5850
}

libs/flags/progress_format_test.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,7 @@ func TestProgressFormatSet(t *testing.T) {
1616

1717
// invalid arg
1818
err := p.Set("foo")
19-
assert.ErrorContains(t, err, "accepted arguments are [append, json]")
20-
21-
// set json
22-
err = p.Set("json")
23-
assert.NoError(t, err)
24-
assert.Equal(t, "json", p.String())
25-
26-
err = p.Set("JSON")
27-
assert.NoError(t, err)
28-
assert.Equal(t, "json", p.String())
29-
30-
err = p.Set("Json")
31-
assert.NoError(t, err)
32-
assert.Equal(t, "json", p.String())
19+
assert.ErrorContains(t, err, "accepted arguments are [append]")
3320

3421
// set append
3522
err = p.Set("append")

0 commit comments

Comments
 (0)