Skip to content

Commit

Permalink
Various Improvements (#216)
Browse files Browse the repository at this point in the history
* More consistent success messages when a list doesn't have any items.
* Fixed a race in shell command.
* Fixed SQL querying
* Various test fixes
  • Loading branch information
yuce authored Apr 18, 2023
1 parent 17f4a64 commit fa0631d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion base/commands/config/config_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (cm ListCmd) Exec(ctx context.Context, ec plug.ExecContext) error {
ec.Logger().Warn("Cannot access configs directory at: %s: %s", cd, err.Error())
}
if len(cs) == 0 {
ec.PrintlnUnnecessary("No configuration was found.")
ec.PrintlnUnnecessary("No configurations found.")
return nil
}
var rows []output.Row
Expand Down
2 changes: 1 addition & 1 deletion base/commands/map/map_entry_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (mc *MapEntrySetCommand) Exec(ctx context.Context, ec plug.ExecContext) err
return ec.AddOutputRows(ctx, rows...)
}
if !ec.Props().GetBool(clc.PropertyQuiet) {
I2(fmt.Fprintln(ec.Stdout(), "No entries found"))
I2(fmt.Fprintln(ec.Stdout(), "No entries found."))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion base/commands/map/map_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func size_InteractiveTest(t *testing.T) {
tcx.WithReset(func() {
check.Must(m.Set(ctx, "foo", "bar"))
tcx.WriteStdin([]byte(fmt.Sprintf("\\map -n %s size\n", m.Name())))
tcx.AssertStderrEquals("")
tcx.AssertStderrContains("OK")
tcx.AssertStdoutDollarWithPath("testdata/map_size_1.txt")
})
})
Expand Down
9 changes: 8 additions & 1 deletion base/commands/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"strings"
"sync"

"github.com/google/shlex"

Expand Down Expand Up @@ -36,6 +37,7 @@ var errHelp = errors.New("interactive help")

type ShellCommand struct {
shortcuts map[string]struct{}
mu sync.RWMutex
}

func (cm *ShellCommand) Init(cc plug.InitContext) error {
Expand All @@ -44,11 +46,13 @@ func (cm *ShellCommand) Init(cc plug.InitContext) error {
cc.SetCommandHelp(help, help)
cc.SetPositionalArgCount(0, 0)
cc.Hide()
cm.mu.Lock()
cm.shortcuts = map[string]struct{}{
`\dm`: {},
`\dm+`: {},
`\exit`: {},
}
cm.mu.Unlock()
return nil
}

Expand Down Expand Up @@ -102,7 +106,10 @@ func (cm *ShellCommand) ExecInteractive(ctx context.Context, ec plug.ExecContext
textFn := func(ctx context.Context, stdout io.Writer, text string) error {
if strings.HasPrefix(strings.TrimSpace(text), shell.CmdPrefix) {
parts := strings.Fields(text)
if _, ok := cm.shortcuts[parts[0]]; !ok {
cm.mu.RLock()
_, ok := cm.shortcuts[parts[0]]
cm.mu.RUnlock()
if !ok {
// this is a CLC command
text = strings.TrimSpace(text)
text = strings.TrimPrefix(text, shell.CmdPrefix)
Expand Down
12 changes: 7 additions & 5 deletions base/commands/sql/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ func UpdateOutput(ctx context.Context, ec plug.ExecContext, res sql.Result, verb
return err
}
rowCh := make(chan output.Row, 1)
errCh := make(chan error)
errCh := make(chan error, 1)
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, os.Kill)
defer stop()
go func() {
var row sql.Row
var err error
for it.HasNext() {
row, err := it.Next()
row, err = it.Next()
if err != nil {
errCh <- err
break
}
// TODO: move the following 2 lines out of the loop --YT
// have to create a new output row
// since it is processed by another goroutine
cols := row.Metadata().Columns()
orow := make(output.Row, len(cols))
for i, col := range cols {
Expand All @@ -62,7 +64,7 @@ func UpdateOutput(ctx context.Context, ec plug.ExecContext, res sql.Result, verb
}
}
close(rowCh)
errCh <- nil
errCh <- err
}()
_ = ec.AddOutputStream(ctx, rowCh)
select {
Expand Down
2 changes: 1 addition & 1 deletion base/commands/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (cm *SQLCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
if err != nil {
return err
}
stop()
defer stop()
// TODO: keep it or remove it?
verbose := ec.Props().GetBool(clc.PropertyVerbose)
return UpdateOutput(ctx, ec, res, verbose)
Expand Down
14 changes: 14 additions & 0 deletions base/commands/sql/sql_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestSQL(t *testing.T) {
{name: "SQL_ShellCommand", f: sql_shellCommandTest},
{name: "SQL_Interactive", f: sql_InteractiveTest},
{name: "SQL_NonInteractive", f: sql_NonInteractiveTest},
{name: "SQL_NonInteractiveStreaming", f: sql_NonInteractiveStreamingTest},
{name: "SQL_Suggestion_Interactive", f: sqlSuggestion_Interactive},
{name: "SQL_Suggestion_NonInteractive", f: sqlSuggestion_NonInteractive},
}
Expand Down Expand Up @@ -197,6 +198,19 @@ func sqlOutput_NonInteractiveTest(t *testing.T) {
}
}

func sql_NonInteractiveStreamingTest(t *testing.T) {
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
ctx := context.Background()
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.CLCExecute(ctx, "sql", "select * from table(generate_stream(1)) limit 3")
tcx.AssertStdoutEquals("0\n1\n2\n")
})
})
})
}

func sqlOutput_JSONTest(t *testing.T) {
formats := []string{"delimited", "json", "csv", "table"}
for _, f := range formats {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/alecthomas/chroma v0.10.0
github.com/gohxs/readline v0.0.0-20171011095936-a780388e6e7c
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hazelcast/hazelcast-go-client v1.4.1-0.20230414163103-81c510723182
github.com/hazelcast/hazelcast-go-client v1.4.1-0.20230418181131-132516f96baa
github.com/mattn/go-runewidth v0.0.14
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20210105204122-a87d9f614b9d
github.com/spf13/cobra v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ github.com/gohxs/readline v0.0.0-20171011095936-a780388e6e7c/go.mod h1:9S/fKAutQ
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/hazelcast/hazelcast-go-client v1.4.1-0.20230414163103-81c510723182 h1:PSn6/nZ5DbqSDMc3MHurSHSFR477f9sH6ghWv9gxR4s=
github.com/hazelcast/hazelcast-go-client v1.4.1-0.20230414163103-81c510723182/go.mod h1:PJ38lqXJ18S0YpkrRznPDlUH8GnnMAQCx3jpQtBPZ6Q=
github.com/hazelcast/hazelcast-go-client v1.4.1-0.20230418181131-132516f96baa h1:KIjQJzRBL++zy7d/8qWFfrXn8ApgVKdIdWXCDihsXlE=
github.com/hazelcast/hazelcast-go-client v1.4.1-0.20230418181131-132516f96baa/go.mod h1:PJ38lqXJ18S0YpkrRznPDlUH8GnnMAQCx3jpQtBPZ6Q=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down
18 changes: 12 additions & 6 deletions internal/it/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,7 @@ func (tcx TestContext) Tester(f func(tcx TestContext)) {
d, _ := filepath.Split(p)
check.Must(os.MkdirAll(d, 0700))
home.WithFile(p, bytesConfig, func(_ string) {
fp, err := config.NewFileProvider(tcx.ConfigPath)
if err != nil {
panic(err)
}
tcx.main = check.MustValue(cmd.NewMain("clc", tcx.ConfigPath, fp, tcx.LogPath, tcx.LogLevel, tcx.IO()))
tcx.main = check.MustValue(tcx.createMain())
tcx.T.Logf("created CLC main")
defer func() {
check.Must(tcx.main.Exit())
Expand Down Expand Up @@ -255,6 +251,7 @@ func (tcx TestContext) AssertStdoutEqualsWithPath(path string) {
}

func (tcx TestContext) WithReset(f func()) {
time.Sleep(100 * time.Millisecond)
tcx.ExpectStdout.Reset()
tcx.ExpectStderr.Reset()
tcx.stdout.Reset()
Expand All @@ -265,7 +262,8 @@ func (tcx TestContext) WithReset(f func()) {
func (tcx TestContext) CLCExecute(ctx context.Context, args ...string) {
a := []string{"-c", tcx.ConfigPath}
a = append(a, args...)
check.Must(tcx.CLC().Execute(ctx, a...))
main := check.MustValue(tcx.createMain())
check.Must(main.Execute(ctx, a...))
}

func (tcx TestContext) WithShell(ctx context.Context, f func(tcx TestContext)) {
Expand All @@ -281,6 +279,14 @@ func (tcx TestContext) WithShell(ctx context.Context, f func(tcx TestContext)) {
})
}

func (tcx TestContext) createMain() (*cmd.Main, error) {
fp, err := config.NewFileProvider(tcx.ConfigPath)
if err != nil {
panic(err)
}
return cmd.NewMain("clc", tcx.ConfigPath, fp, tcx.LogPath, tcx.LogLevel, tcx.IO())
}

func WithEnv(name, value string, f func()) {
b, ok := os.LookupEnv(name)
if ok {
Expand Down

0 comments on commit fa0631d

Please sign in to comment.