Skip to content

Commit

Permalink
Feat/SQL-2474 add option to disable diagnostics (#2935)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavodemorais authored Nov 22, 2024
1 parent c9c4294 commit d438a66
Show file tree
Hide file tree
Showing 40 changed files with 299 additions and 166 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/confluentinc/cmf-sdk-go v0.0.2
github.com/confluentinc/confluent-kafka-go/v2 v2.6.0
github.com/confluentinc/go-editor v0.11.0
github.com/confluentinc/go-prompt v0.2.38
github.com/confluentinc/go-prompt v0.2.40
github.com/confluentinc/go-ps1 v1.0.2
github.com/confluentinc/kafka-rest-sdk-go/kafkarestv3 v0.3.18
github.com/confluentinc/mds-sdk-go-public/mdsv1 v0.0.0-20240923163156-b922b35891f9
Expand Down Expand Up @@ -87,6 +87,7 @@ require (
github.com/keybase/go-keychain v0.0.0-20230523030712-b5615109f100
github.com/linkedin/goavro/v2 v2.13.0
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-runewidth v0.0.15
github.com/olekukonko/tablewriter v0.0.5
github.com/opencontainers/image-spec v1.1.0
github.com/panta/machineid v1.0.2
Expand Down Expand Up @@ -175,7 +176,6 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-tty v0.0.4 // indirect
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ github.com/confluentinc/confluent-kafka-go/v2 v2.6.0 h1:VKnMT71Tl0dCp3lfGBp2D8eq
github.com/confluentinc/confluent-kafka-go/v2 v2.6.0/go.mod h1:hScqtFIGUI1wqHIgM3mjoqEou4VweGGGX7dMpcUKves=
github.com/confluentinc/go-editor v0.11.0 h1:fcEALYHj7xV/fRSp54/IHi2DS4GlZMJWVgrYvi/llvU=
github.com/confluentinc/go-editor v0.11.0/go.mod h1:nEjwqdqx8S7ZGjXsDvRgawsA04Fu2P/KAtA8fa5afMI=
github.com/confluentinc/go-prompt v0.2.38 h1:z9tSfrJYHbiZC81kZQjenW5nFx9w2Vzbs84aswwx3vE=
github.com/confluentinc/go-prompt v0.2.38/go.mod h1:Bc4kUldoYFpXx7frWasxAG0wpp+xOcu/Ehq930/FJBw=
github.com/confluentinc/go-prompt v0.2.40 h1:tveghQJ+FVOVvF0dgQaZEm7YZSQ3r3tyuLMHy7w4PK0=
github.com/confluentinc/go-prompt v0.2.40/go.mod h1:Bc4kUldoYFpXx7frWasxAG0wpp+xOcu/Ehq930/FJBw=
github.com/confluentinc/go-ps1 v1.0.2 h1:+4cKOzWs3AWmxL2s96oHu0QutZESDRXECnhFzm2ic4o=
github.com/confluentinc/go-ps1 v1.0.2/go.mod h1:qmgG9xQgFd4u7/CS6eA9nNP8eQqOtXuRHmZ4+w7Vprs=
github.com/confluentinc/kafka-rest-sdk-go/kafkarestv3 v0.3.18 h1:M9/yzpG+eOTp/peiHNHRQWax0If6+GAJIC/NcvbzAOI=
Expand Down
2 changes: 1 addition & 1 deletion pkg/flink/app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func StartApp(gatewayClient ccloudv2.GatewayClientInterface, tokenRefreshFunc fu
reportUsage: reportUsageFunc,
appOptions: appOptions,
}
components.PrintWelcomeHeader()
components.PrintWelcomeHeader(appOptions)
return app.readEvalPrintLoop()
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/flink/components/.snapshots/TestPrintCompletionsFalse
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Completions are now disabled.

3 changes: 3 additions & 0 deletions pkg/flink/components/.snapshots/TestPrintCompletionsTrue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Completions are now enabled.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Diagnostics are now disabled.

4 changes: 4 additions & 0 deletions pkg/flink/components/.snapshots/TestPrintDiagnosticsStateTrue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

You should now see the errors in your SQL statement highlighted as you type, if there are any.
Diagnostics are now enabled.

3 changes: 3 additions & 0 deletions pkg/flink/components/.snapshots/TestPrintSmartCompletionFalse
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Smart Completion is now disabled

3 changes: 3 additions & 0 deletions pkg/flink/components/.snapshots/TestPrintSmartCompletionTrue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Smart Completion is now enabled

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

You should now see the errors in your SQL statement highlighted as you type, if there are any.
Diagnostics are now enabled.

44 changes: 36 additions & 8 deletions pkg/flink/components/interactive_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import (
"strings"

fColor "github.com/fatih/color"
"github.com/mattn/go-runewidth"

"github.com/confluentinc/cli/v4/pkg/color"
"github.com/confluentinc/cli/v4/pkg/featureflags"
"github.com/confluentinc/cli/v4/pkg/flink/types"
"github.com/confluentinc/cli/v4/pkg/output"
)

func PrintSmartCompletionState(smartCompletion bool, maxCol int) {
msgPrefix := "Smart Completion is now "
PrintOptionState(msgPrefix, smartCompletion, maxCol)
func PrintCompletionsState(completionsEnabled bool, maxCol int) {
msgPrefix := "Completions are now "
PrintOptionState(msgPrefix, completionsEnabled, maxCol)
}

func PrintDiagnosticsState(diagnosticsEnabled bool, maxCol int) {
msg := "Diagnostics are now "
if diagnosticsEnabled {
msg = "You should now see the errors in your SQL statement highlighted as you type, if there are any.\n" + msg
}
PrintOptionState(msg, diagnosticsEnabled, maxCol)
}

func PrintOptionState(prefix string, isEnabled bool, maxCol int) {
Expand All @@ -20,18 +31,35 @@ func PrintOptionState(prefix string, isEnabled bool, maxCol int) {
stateMsg = "enabled"
}

output.Print(false, "\n"+prefix+fColor.CyanString(stateMsg))
lines := strings.Split(prefix, "\n")

output.Print(false, "\n")
for i, line := range lines {
if i == len(lines)-1 {
output.Print(false, line+fColor.CyanString(stateMsg)+".")
line = line + stateMsg + "."
} else {
output.Print(false, line)
}

// This prints to the console the exact amount of empty characters to fill the line might have autocompletions before
output.Println(false, strings.Repeat(" ", maxCol-len(prefix+stateMsg)))
// This prints to the console the exact amount of empty characters to fill the line might have autocompletions before
// This is to avoid the linter to complain about not using the
output.Println(false, strings.Repeat(" ", maxCol-runewidth.StringWidth(line)))
}
}

func PrintWelcomeHeader() {
func PrintWelcomeHeader(appOtions types.ApplicationOptions) {
// Print welcome message
output.Print(false, "Welcome! \n")
output.Print(false, "To exit, press Ctrl-Q or type \"exit\". \n\n")

// Print shortcuts
c := fColor.New(color.AccentColor)
output.Printf(false, "[Ctrl-Q] %s [Ctrl-S] %s \n", c.Sprint("Quit"), c.Sprint("Toggle Smart Completion"))

ldClient := featureflags.GetCcloudLaunchDarklyClient(appOtions.Context.PlatformName)
if featureflags.Manager.BoolVariation("flink.language_service.enable_diagnostics", appOtions.Context, ldClient, true, false) {
output.Printf(false, "[Ctrl-Q] %s [Ctrl-S] %s [Ctrl-G] %s \n", c.Sprint("Quit"), c.Sprint("Toggle Completions"), c.Sprint("Toggle Diagnostics"))
} else {
output.Printf(false, "[Ctrl-Q] %s [Ctrl-S] %s \n", c.Sprint("Quit"), c.Sprint("Toggle Completions"))
}
}
43 changes: 43 additions & 0 deletions pkg/flink/components/interactive_input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package components

import (
"testing"

"github.com/bradleyjkemp/cupaloy/v2"

"github.com/confluentinc/cli/v4/pkg/flink/test"
)

const maxCol = 100

func TestPrintCompletionsTrue(t *testing.T) {
actual := test.RunAndCaptureSTDOUT(t, func() {
PrintCompletionsState(true, maxCol)
})

cupaloy.SnapshotT(t, actual)
}

func TestPrintCompletionsFalse(t *testing.T) {
actual := test.RunAndCaptureSTDOUT(t, func() {
PrintCompletionsState(false, maxCol)
})

cupaloy.SnapshotT(t, actual)
}

func TestPrintDiagnosticsStateTrue(t *testing.T) {
actual := test.RunAndCaptureSTDOUT(t, func() {
PrintDiagnosticsState(true, maxCol)
})

cupaloy.SnapshotT(t, actual)
}

func TestPrintDiagnosticsStateFalse(t *testing.T) {
actual := test.RunAndCaptureSTDOUT(t, func() {
PrintDiagnosticsState(false, maxCol)
})

cupaloy.SnapshotT(t, actual)
}
16 changes: 8 additions & 8 deletions pkg/flink/internal/autocomplete/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"github.com/confluentinc/go-prompt"
)

func combineCompleters(getSmartCompletion func() bool, completers ...prompt.Completer) prompt.Completer {
func combineCompleters(getCompletionsEnabled func() bool, completers ...prompt.Completer) prompt.Completer {
return func(d prompt.Document) []prompt.Suggest {
suggestions := []prompt.Suggest{}

if !getSmartCompletion() {
if !getCompletionsEnabled() {
return suggestions
}

Expand All @@ -20,19 +20,19 @@ func combineCompleters(getSmartCompletion func() bool, completers ...prompt.Comp
}

type completerBuilder struct {
isSmartCompletionEnabled func() bool
completer prompt.Completer
completionsEnabled func() bool
completer prompt.Completer
}

func NewCompleterBuilder(isSmartCompletionEnabled func() bool) *completerBuilder {
return &completerBuilder{isSmartCompletionEnabled: isSmartCompletionEnabled}
func NewCompleterBuilder(completionsEnabled func() bool) *completerBuilder {
return &completerBuilder{completionsEnabled: completionsEnabled}
}

func (c *completerBuilder) AddCompleter(completer prompt.Completer) *completerBuilder {
if c.completer == nil {
c.completer = combineCompleters(c.isSmartCompletionEnabled, completer)
c.completer = combineCompleters(c.completionsEnabled, completer)
} else {
c.completer = combineCompleters(c.isSmartCompletionEnabled, c.completer, completer)
c.completer = combineCompleters(c.completionsEnabled, c.completer, completer)
}
return c
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/flink/internal/autocomplete/completer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/confluentinc/cli/v4/pkg/flink/test/generators"
)

func mockGetSmartCompletion() bool {
func mockCompletionsEnabled() bool {
return true
}

Expand All @@ -22,7 +22,7 @@ func TestBasicSelectAutoCompletion(t *testing.T) {
buffer.InsertText(input, false, true)

expected := prompt.Suggest{Text: "SELECT ", Description: "Select data from a database"}
completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
AddCompleter(SetCompleter).
AddCompleter(ShowCompleter).
Expand All @@ -38,7 +38,7 @@ func TestFailingBasicAutoCompletion(t *testing.T) {
buffer.InsertText(input, false, true)

expected := prompt.Suggest{Text: "SELECT", Description: "Select data from a database"}
completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
AddCompleter(SetCompleter).
AddCompleter(ShowCompleter).
Expand All @@ -62,7 +62,7 @@ func TestNoLineBreaksInAutocompletion(t *testing.T) {

// when
buffer.InsertText(randomStatement.Text, false, true)
completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
AddCompleter(SetCompleter).
AddCompleter(ShowCompleter).
Expand Down
14 changes: 7 additions & 7 deletions pkg/flink/internal/autocomplete/examples_completer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestSelectExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand All @@ -27,7 +27,7 @@ func TestAlterExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand All @@ -41,7 +41,7 @@ func TestInserttExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand All @@ -55,7 +55,7 @@ func TestDescribeExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand All @@ -69,7 +69,7 @@ func TestUseExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand All @@ -83,7 +83,7 @@ func TestSetExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand All @@ -97,7 +97,7 @@ func TestResetExampleAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ExamplesCompleter).
BuildCompleter()

Expand Down
2 changes: 1 addition & 1 deletion pkg/flink/internal/autocomplete/history_completer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestHistorySnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(GenerateHistoryCompleter(history)).
BuildCompleter()

Expand Down
2 changes: 1 addition & 1 deletion pkg/flink/internal/autocomplete/set_completer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestSetAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(SetCompleter).
BuildCompleter()

Expand Down
2 changes: 1 addition & 1 deletion pkg/flink/internal/autocomplete/show_completer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestShowAutoCompletionSnapshot(t *testing.T) {
buffer := prompt.NewBuffer()
buffer.InsertText(input, false, true)

completer := NewCompleterBuilder(mockGetSmartCompletion).
completer := NewCompleterBuilder(mockCompletionsEnabled).
AddCompleter(ShowCompleter).
BuildCompleter()

Expand Down
Loading

0 comments on commit d438a66

Please sign in to comment.