Skip to content

Commit

Permalink
Update the map command output (hazelcast#374)
Browse files Browse the repository at this point in the history
Updated the map command
  • Loading branch information
yuce authored Sep 8, 2023
1 parent ab852ba commit 7883e39
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 325 deletions.
14 changes: 14 additions & 0 deletions base/commands/map/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
package _map

import (
"context"
"fmt"
"strings"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/cmd"
"github.com/hazelcast/hazelcast-commandline-client/internal"
"github.com/hazelcast/hazelcast-commandline-client/internal/mk"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
Expand Down Expand Up @@ -58,3 +62,13 @@ func makeKeyValueData(ec plug.ExecContext, ci *hazelcast.ClientInternal, keyStr,
}
return kd, vd, nil
}

func getMap(ctx context.Context, ec plug.ExecContext, sp clc.Spinner) (*hazelcast.Map, error) {
name := ec.Props().GetString(base.FlagName)
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Getting map %s", name))
return ci.Client().GetMap(ctx, name)
}
47 changes: 5 additions & 42 deletions base/commands/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@ package _map

import (
"context"
"fmt"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/paths"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
)

const (
mapFlagName = "name"
mapFlagShowType = "show-type"
mapPropertyName = "map"
)

type MapCommand struct {
}
type MapCommand struct{}

func (mc *MapCommand) Init(cc plug.InitContext) error {
cc.SetCommandUsage("map")
Expand All @@ -30,8 +21,8 @@ func (mc *MapCommand) Init(cc plug.InitContext) error {
cc.SetCommandGroup(clc.GroupDDSID)
help := "Map operations"
cc.SetCommandHelp(help, help)
cc.AddStringFlag(mapFlagName, "n", defaultMapName, false, "map name")
cc.AddBoolFlag(mapFlagShowType, "", false, false, "add the type names to the output")
cc.AddStringFlag(base.FlagName, "n", defaultMapName, false, "map name")
cc.AddBoolFlag(base.FlagShowType, "", false, false, "add the type names to the output")
if !cc.Interactive() {
cc.AddStringFlag(clc.PropertySchemaDir, "", paths.Schemas(), false, "set the schema directory")
}
Expand All @@ -42,34 +33,6 @@ func (mc *MapCommand) Exec(context.Context, plug.ExecContext) error {
return nil
}

func (mc *MapCommand) Augment(ec plug.ExecContext, props *plug.Properties) error {
ctx := context.TODO()
props.SetBlocking(mapPropertyName, func() (any, error) {
mapName := ec.Props().GetString(mapFlagName)
// empty map name is allowed
ci, err := ec.ClientInternal(ctx)
if err != nil {
return nil, err
}
mv, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
sp.SetText(fmt.Sprintf("Getting map %s", mapName))
m, err := ci.Client().GetMap(ctx, mapName)
if err != nil {
return nil, err
}
return m, nil
})
if err != nil {
return nil, err
}
stop()
return mv.(*hazelcast.Map), nil
})
return nil
}

func init() {
cmd := &MapCommand{}
Must(plug.Registry.RegisterCommand("map", cmd))
plug.Registry.RegisterAugmentor("20-map", cmd)
Must(plug.Registry.RegisterCommand("map", &MapCommand{}))
}
19 changes: 10 additions & 9 deletions base/commands/map/map_clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"context"
"fmt"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/errors"
"github.com/hazelcast/hazelcast-commandline-client/internal/prompt"

Expand All @@ -19,6 +17,8 @@ import (

type MapClearCommand struct{}

func (mc *MapClearCommand) Unwrappable() {}

func (mc *MapClearCommand) Init(cc plug.InitContext) error {
cc.SetCommandUsage("clear")
help := "Delete all entries of a Map"
Expand All @@ -28,10 +28,6 @@ func (mc *MapClearCommand) Init(cc plug.InitContext) error {
}

func (mc *MapClearCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
mv, err := ec.Props().GetBlocking(mapPropertyName)
if err != nil {
return err
}
autoYes := ec.Props().GetBool(clc.FlagAutoYes)
if !autoYes {
p := prompt.New(ec.Stdin(), ec.Stdout())
Expand All @@ -44,18 +40,23 @@ func (mc *MapClearCommand) Exec(ctx context.Context, ec plug.ExecContext) error
return errors.ErrUserCancelled
}
}
m := mv.(*hazelcast.Map)
_, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
name, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
m, err := getMap(ctx, ec, sp)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Clearing map %s", m.Name()))
if err := m.Clear(ctx); err != nil {
return nil, err
}
return nil, nil
return m.Name(), nil
})
if err != nil {
return err
}
stop()
msg := fmt.Sprintf("OK Cleared map: %s.", name)
ec.PrintlnUnnecessary(msg)
return nil
}

Expand Down
19 changes: 10 additions & 9 deletions base/commands/map/map_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"context"
"fmt"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/errors"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
Expand All @@ -17,6 +15,8 @@ import (

type MapDestroyCommand struct{}

func (mc *MapDestroyCommand) Unwrappable() {}

func (mc *MapDestroyCommand) Init(cc plug.InitContext) error {
cc.SetCommandUsage("destroy")
long := `Destroy a Map
Expand All @@ -29,10 +29,6 @@ This command will delete the Map and the data in it will not be available anymor
}

func (mc *MapDestroyCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
mv, err := ec.Props().GetBlocking(mapPropertyName)
if err != nil {
return err
}
autoYes := ec.Props().GetBool(clc.FlagAutoYes)
if !autoYes {
p := prompt.New(ec.Stdin(), ec.Stdout())
Expand All @@ -45,18 +41,23 @@ func (mc *MapDestroyCommand) Exec(ctx context.Context, ec plug.ExecContext) erro
return errors.ErrUserCancelled
}
}
m := mv.(*hazelcast.Map)
_, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
name, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
m, err := getMap(ctx, ec, sp)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Destroying map %s", m.Name()))
if err := m.Destroy(ctx); err != nil {
return nil, err
}
return nil, nil
return m.Name(), nil
})
if err != nil {
return err
}
stop()
msg := fmt.Sprintf("OK Destroyed map %s.", name)
ec.PrintlnUnnecessary(msg)
return nil
}

Expand Down
41 changes: 24 additions & 17 deletions base/commands/map/map_entry_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"context"
"fmt"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/cmd"
"github.com/hazelcast/hazelcast-commandline-client/internal/output"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
"github.com/hazelcast/hazelcast-commandline-client/internal/proto/codec"
Expand All @@ -18,6 +18,8 @@ import (

type MapEntrySetCommand struct{}

func (mc *MapEntrySetCommand) Unwrappable() {}

func (mc *MapEntrySetCommand) Init(cc plug.InitContext) error {
cc.SetCommandUsage("entry-set")
help := "Get all entries of a Map"
Expand All @@ -26,28 +28,33 @@ func (mc *MapEntrySetCommand) Init(cc plug.InitContext) error {
}

func (mc *MapEntrySetCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
mapName := ec.Props().GetString(mapFlagName)
showType := ec.Props().GetBool(mapFlagShowType)
ci, err := ec.ClientInternal(ctx)
if err != nil {
return err
}
req := codec.EncodeMapEntrySetRequest(mapName)
rv, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
mapName := ec.Props().GetString(base.FlagName)
showType := ec.Props().GetBool(base.FlagShowType)
rowsV, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
req := codec.EncodeMapEntrySetRequest(mapName)
sp.SetText(fmt.Sprintf("Getting entries of %s", mapName))
return ci.InvokeOnRandomTarget(ctx, req, nil)
resp, err := ci.InvokeOnRandomTarget(ctx, req, nil)
if err != nil {
return nil, err
}
pairs := codec.DecodeMapEntrySetResponse(resp)
rows := output.DecodePairs(ci, pairs, showType)
return rows, nil
})
if err != nil {
return err
}
stop()
pairs := codec.DecodeMapEntrySetResponse(rv.(*hazelcast.ClientMessage))
rows := output.DecodePairs(ci, pairs, showType)
if len(rows) > 0 {
return ec.AddOutputRows(ctx, rows...)
rows := rowsV.([]output.Row)
if len(rows) == 0 {
ec.PrintlnUnnecessary("OK No entries found.")
return nil
}
ec.PrintlnUnnecessary("No entries found.")
return nil
return ec.AddOutputRows(ctx, rows...)
}

func init() {
Expand Down
79 changes: 43 additions & 36 deletions base/commands/map/map_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"context"
"fmt"

"github.com/hazelcast/hazelcast-go-client"

"github.com/hazelcast/hazelcast-commandline-client/base"
"github.com/hazelcast/hazelcast-commandline-client/clc"
"github.com/hazelcast/hazelcast-commandline-client/clc/cmd"
. "github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/output"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
Expand All @@ -18,6 +18,8 @@ import (

type MapGetCommand struct{}

func (mc *MapGetCommand) Unwrappable() {}

func (mc *MapGetCommand) Init(cc plug.InitContext) error {
cc.SetCommandUsage("get")
addKeyTypeFlag(cc)
Expand All @@ -28,47 +30,52 @@ func (mc *MapGetCommand) Init(cc plug.InitContext) error {
}

func (mc *MapGetCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
mapName := ec.Props().GetString(mapFlagName)
ci, err := ec.ClientInternal(ctx)
if err != nil {
return err
}
mapName := ec.Props().GetString(base.FlagName)
keyStr := ec.GetStringArg(argKey)
keyData, err := makeKeyData(ec, ci, keyStr)
if err != nil {
return err
}
req := codec.EncodeMapGetRequest(mapName, keyData, 0)
rv, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
showType := ec.Props().GetBool(base.FlagShowType)
rowV, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
ci, err := cmd.ClientInternal(ctx, ec, sp)
if err != nil {
return nil, err
}
sp.SetText(fmt.Sprintf("Getting from map %s", mapName))
return ci.InvokeOnKey(ctx, req, keyData, nil)
keyData, err := makeKeyData(ec, ci, keyStr)
if err != nil {
return nil, err
}
req := codec.EncodeMapGetRequest(mapName, keyData, 0)
resp, err := ci.InvokeOnKey(ctx, req, keyData, nil)
if err != nil {
return nil, err
}
data := codec.DecodeMapGetResponse(resp)
vt := data.Type()
value, err := ci.DecodeData(data)
if err != nil {
ec.Logger().Info("The value for %s was not decoded, due to error: %s", keyStr, err.Error())
value = serialization.NondecodedType(serialization.TypeToLabel(vt))
}
row := output.Row{
output.Column{
Name: output.NameValue,
Type: vt,
Value: value,
},
}
if showType {
row = append(row, output.Column{
Name: output.NameValueType,
Type: serialization.TypeString,
Value: serialization.TypeToLabel(vt),
})
}
return row, nil
})
if err != nil {
return err
}
stop()
raw := codec.DecodeMapGetResponse(rv.(*hazelcast.ClientMessage))
vt := raw.Type()
value, err := ci.DecodeData(raw)
if err != nil {
ec.Logger().Info("The value for %s was not decoded, due to error: %s", keyStr, err.Error())
value = serialization.NondecodedType(serialization.TypeToLabel(vt))
}
row := output.Row{
output.Column{
Name: output.NameValue,
Type: vt,
Value: value,
},
}
if ec.Props().GetBool(mapFlagShowType) {
row = append(row, output.Column{
Name: output.NameValueType,
Type: serialization.TypeString,
Value: serialization.TypeToLabel(vt),
})
}
return ec.AddOutputRows(ctx, row)
return ec.AddOutputRows(ctx, rowV.(output.Row))
}

func init() {
Expand Down
Loading

0 comments on commit 7883e39

Please sign in to comment.