Skip to content

Commit

Permalink
make aliases runtime variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanmetin committed Sep 1, 2023
1 parent 41038ee commit 67178c6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 106 deletions.
27 changes: 26 additions & 1 deletion base/commands/alias/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ package alias

import (
"context"
"os"
"path/filepath"
"strings"
"sync"

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

var Aliases sync.Map

const AliasFileName = "shell.clc"

type AliasCommand struct {
Expand All @@ -31,5 +38,23 @@ func (a AliasCommand) Exec(context.Context, plug.ExecContext) error {
}

func init() {
Must(plug.Registry.RegisterCommand("alias", AliasCommand{}))
Must(plug.Registry.RegisterCommand("alias", AliasCommand{}, plug.OnlyInteractive{}))
Must(persistentAliases())
}

func persistentAliases() error {
p := filepath.Join(paths.Home(), AliasFileName)
data, err := os.ReadFile(p)
if err != nil && !os.IsNotExist(err) {
return err
}
lines := strings.Split(string(data), "\n")
for _, l := range lines {
if l == "" {
continue
}
parts := strings.SplitN(l, "=", 2)
Aliases.Store(parts[0], parts[1])
}
return nil
}
42 changes: 3 additions & 39 deletions base/commands/alias/alias_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import (
"context"
"fmt"
"math"
"os"
"path/filepath"
"strings"

"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"
)
Expand All @@ -34,7 +31,8 @@ func (a AliasAddCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
cmd := strings.Join(ec.Args()[1:], " ")
_, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
sp.SetText(fmt.Sprintf("Saving alias %s", name))
return nil, CreateAlias(name, cmd)
Aliases.Store(name, cmd)
return nil, nil
})
if err != nil {
return err
Expand All @@ -43,40 +41,6 @@ func (a AliasAddCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
return nil
}

func CreateAlias(key, value string) error {
p := filepath.Join(paths.Home(), AliasFileName)
data, err := os.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
_, err = os.Create(p)
if err != nil {
return err
}
} else {
return err
}
}
lines := strings.Split(string(data), "\n")
var newData []string
updated := false
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
if parts[0] == key {
newData = append(newData, fmt.Sprintf("%s=%s", key, value))
updated = true
} else {
newData = append(newData, line)
}
}
}
if !updated {
newData = append(newData, fmt.Sprintf("%s=%s", key, value))
}
c := strings.Join(newData, "\n")
return os.WriteFile(filepath.Join(paths.Home(), AliasFileName), []byte(c), 0644)
}

func init() {
Must(plug.Registry.RegisterCommand("alias:add", &AliasAddCommand{}))
Must(plug.Registry.RegisterCommand("alias:add", &AliasAddCommand{}, plug.OnlyInteractive{}))
}
21 changes: 9 additions & 12 deletions base/commands/alias/alias_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@ package alias_test
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

_ "github.com/hazelcast/hazelcast-commandline-client/base"
_ "github.com/hazelcast/hazelcast-commandline-client/base/commands"
"github.com/hazelcast/hazelcast-commandline-client/base/commands/alias"
"github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/it"
"github.com/stretchr/testify/require"
)

func TestAlias(t *testing.T) {
testCases := []struct {
name string
f func(t *testing.T)
}{
{name: "Execute_Interactive", f: Execute_InteractiveTest},
{name: "Add_Interactive", f: Add_InteractiveTest},
{name: "Remove_Interactive", f: Remove_InteractiveTest},
{name: "List_Interactive", f: List_InteractiveTest},
{name: "Execute_Interactive", f: Execute_InteractiveTest},
}
for _, tc := range testCases {
t.Run(tc.name, tc.f)
Expand All @@ -36,7 +32,7 @@ func Execute_InteractiveTest(t *testing.T) {
ctx := context.TODO()
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
check.Must(alias.CreateAlias("mapAlias", "map set 1 1"))
alias.Aliases.Store("mapAlias", "map set 1 1")
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.WriteStdinString("@mapAlias\n")
Expand All @@ -54,37 +50,38 @@ func Add_InteractiveTest(t *testing.T) {
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.WriteStdinString(fmt.Sprintf("\\alias add mapAlias %s\n", `map set 1 1`))
tcx.WriteStdinString("\\alias list\n")
tcx.AssertStdoutContains("map set 1 1")
})
})
c := check.MustValue(os.ReadFile(filepath.Join(tcx.HomePath(), alias.AliasFileName)))
require.Equal(t, "mapAlias=map set 1 1", string(c))
})
}

func Remove_InteractiveTest(t *testing.T) {
ctx := context.TODO()
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
check.Must(alias.CreateAlias("mapAlias", "map set 1 1"))
alias.Aliases.Store("mapAlias", "map set 1 1")
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.WriteStdinString("\\alias remove mapAlias\n")
tcx.WriteStdinString("\\alias list\n")
tcx.AssertStdoutNotContains("map set 1 1")
})
})
c := check.MustValue(os.ReadFile(filepath.Join(tcx.HomePath(), alias.AliasFileName)))
require.Equal(t, 0, len(c))
})
}

func List_InteractiveTest(t *testing.T) {
ctx := context.TODO()
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
check.Must(alias.CreateAlias("mapAlias", "map set 1 1"))
alias.Aliases.Store("mapAlias", "map set 1 1")
tcx.WithShell(ctx, func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.WriteStdinString("\\alias list\n")
tcx.AssertStdoutContains("mapAlias")
tcx.AssertStdoutContains("map set 1 1")
})
})
})
Expand Down
43 changes: 12 additions & 31 deletions base/commands/alias/alias_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package alias

import (
"context"
"os"
"path/filepath"
"strings"

"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/output"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
Expand All @@ -32,14 +28,22 @@ type alias struct {
func (a AliasListCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
sv, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
sp.SetText("Listing aliases")
return listAliases()
var all []alias
Aliases.Range(func(key, value any) bool {
all = append(all, alias{
name: key.(string),
value: value.(string),
})
return true
})
return all, nil
})
if err != nil {
return err
}
stop()
aliases := sv.([]alias)
if len(aliases) == 0 {
all := sv.([]alias)
if len(all) == 0 {
ec.PrintlnUnnecessary("No aliases found.")
return nil
}
Expand All @@ -61,29 +65,6 @@ func (a AliasListCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
return ec.AddOutputRows(ctx, rows...)
}

func listAliases() ([]alias, error) {
var aliases []alias
data, err := os.ReadFile(filepath.Join(paths.Home(), AliasFileName))
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
lines := strings.Split(string(data), "\n")
for _, l := range lines {
if l == "" {
continue
}
parts := strings.SplitN(l, "=", 2)
aliases = append(aliases, alias{
name: parts[0],
value: parts[1],
})
}
return aliases, nil
}

func init() {
Must(plug.Registry.RegisterCommand("alias:list", &AliasListCommand{}))
Must(plug.Registry.RegisterCommand("alias:list", &AliasListCommand{}, plug.OnlyInteractive{}))
}
26 changes: 3 additions & 23 deletions base/commands/alias/alias_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package alias
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"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"
)
Expand All @@ -27,7 +23,8 @@ func (a AliasRemoveCommand) Exec(ctx context.Context, ec plug.ExecContext) error
name := ec.Args()[0]
_, stop, err := ec.ExecuteBlocking(ctx, func(ctx context.Context, sp clc.Spinner) (any, error) {
sp.SetText(fmt.Sprintf("Removing alias %s", name))
return nil, removeAlias(name)
Aliases.Delete(name)
return nil, nil
})
if err != nil {
return err
Expand All @@ -36,23 +33,6 @@ func (a AliasRemoveCommand) Exec(ctx context.Context, ec plug.ExecContext) error
return nil
}

func removeAlias(name string) error {
data, err := os.ReadFile(filepath.Join(paths.Home(), AliasFileName))
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
var newData []string
for _, l := range lines {
parts := strings.SplitN(l, "=", 2)
if parts[0] != name {
newData = append(newData, l)
}
}
newContent := strings.Join(newData, "\n")
return os.WriteFile(filepath.Join(paths.Home(), AliasFileName), []byte(newContent), 0600)
}

func init() {
Must(plug.Registry.RegisterCommand("alias:remove", &AliasRemoveCommand{}))
Must(plug.Registry.RegisterCommand("alias:remove", &AliasRemoveCommand{}, plug.OnlyInteractive{}))
}
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/user-defined-aliases.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Alias command are a group of alias operations.

They are runtime variables. If you want to persist them you need to save them in `$CLC_HOME/shell.clc` in `<name>=<command>` format. Variables in `shell.clc` are loaded everytime CLC starts.

Usage:

[source,bash]
Expand Down

0 comments on commit 67178c6

Please sign in to comment.