Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

- `tt connect`: \history command.

### Changed

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ env:
inc_dir: path/to/inc_dir
restart_on_failure: bool
tarantoolctl_layout: bool
ouptut_history_max: 15
modules:
directory: path/to/modules/dir
app:
Expand Down Expand Up @@ -341,6 +342,8 @@ templates:
compatible mode for artifact files: control socket, pid, log files.
Data files (wal, vinyl, snapshots) and multi-instance applications
are not affected by this option.
- `ouptut_history_max` (int) - the maximum number of commands output
by the "\history" command. Default vaule is 10

#### modules

Expand Down
4 changes: 4 additions & 0 deletions cli/cfg/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ env:
instances_enabled: %[1]s
restart_on_failure: false
tarantoolctl_layout: false
ouptut_history_max: 0
modules:
directory: /root/modules
app:
Expand Down Expand Up @@ -128,6 +129,7 @@ env:
instances_enabled: %[1]s
restart_on_failure: false
tarantoolctl_layout: false
ouptut_history_max: 0
modules:
directory:
- /root/modules
Expand Down Expand Up @@ -166,6 +168,7 @@ env:
instances_enabled: %[1]s/instances.enabled
restart_on_failure: false
tarantoolctl_layout: false
ouptut_history_max: 0
modules:
directory: %[1]s/my_modules
app:
Expand Down Expand Up @@ -203,6 +206,7 @@ env:
instances_enabled: .
restart_on_failure: false
tarantoolctl_layout: false
ouptut_history_max: 0
modules:
directory: /root/modules
app:
Expand Down
8 changes: 8 additions & 0 deletions cli/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func makeConnOpts(network, address string, connCtx connect.ConnectCtx) connector
}
}

func getMaxHistoryOutputLen(cliOpts *config.CliOpts) int {
if cliOpts.Env == nil || cliOpts.Env.OutputHistoryMax <= 0 {
return connect.DefaultOutputHistorySize
}
return cliOpts.Env.OutputHistoryMax
}

// resolveConnectOpts tries to resolve target argument to replace it
// with a control socket or as a URI with/without credentials.
// It returns connection options and the remaining args.
Expand Down Expand Up @@ -183,6 +190,7 @@ func resolveConnectOpts(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts,
if connectCtx.ConnectTarget == "" {
connectCtx.ConnectTarget = target
}
connOpts.MaxOutputHistoryLen = getMaxHistoryOutputLen(cliOpts)
return connOpts, err
}

Expand Down
27 changes: 27 additions & 0 deletions cli/cmd/connect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tarantool/tt/cli/config"
"github.com/tarantool/tt/cli/connect"
)

func TestGenMaxHistoryOption(t *testing.T) {
t.Run("not configured", func(t *testing.T) {
opts := config.CliOpts{}
actual := getMaxHistoryOutputLen(&opts)
assert.Equal(t, connect.DefaultOutputHistorySize, actual)
})
t.Run("invalid configuration", func(t *testing.T) {
opts := config.CliOpts{Env: &config.TtEnvOpts{OutputHistoryMax: 0}}
actual := getMaxHistoryOutputLen(&opts)
assert.Equal(t, connect.DefaultOutputHistorySize, actual)
})
t.Run("correct configuration", func(t *testing.T) {
opts := config.CliOpts{Env: &config.TtEnvOpts{OutputHistoryMax: 2}}
actual := getMaxHistoryOutputLen(&opts)
assert.Equal(t, 2, actual)
})
}
3 changes: 3 additions & 0 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type TtEnvOpts struct {
// application sub-directories are not created for runtime artifacts like
// control socket, pid files and logs.
TarantoolctlLayout bool `mapstructure:"tarantoolctl_layout" yaml:"tarantoolctl_layout"`
// The maximum number of commands output by the "\history" command.
// Default vaule is 10
OutputHistoryMax int `mapstructure:"ouptut_history_max" yaml:"ouptut_history_max"`
}

// TemplateOpts contains configuration for applications templates.
Expand Down
19 changes: 19 additions & 0 deletions cli/connect/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ func getShortcutsFunc(console *Console, cmd string, args []string) (string, erro
return shortcutListText, nil
}

// getHistoryFunc returns last {maxHistorySize} executed commands
func getHistoryFunc(console *Console, _ string, _ []string) (string, error) {
if len(console.history.commands) == 0 {
return "", nil
}
outputData := console.history.commands
if len(console.history.commands) > console.connOpts.MaxOutputHistoryLen {
outputData = outputData[len(outputData)-console.connOpts.MaxOutputHistoryLen:]
}
return strings.Join(outputData, "\n-----\n"), nil
}

// setQuitFunc sets the quit flag for the console.
func setQuitFunc(console *Console, cmd string, arg []string) (string, error) {
console.quit = true
Expand Down Expand Up @@ -558,6 +570,13 @@ var cmdInfos = []cmdInfo{
newBaseCmd([]string{getShortcutsList}, getShortcutsFunc),
),
},
{
Short: getHistoryList,
Long: "show history of executed commands",
Cmd: newNoArgsCmdDecorator(
newBaseCmd([]string{getHistoryList}, getHistoryFunc),
),
},
// The Tarantool console has `\quit` command, but it requires execute
// access.
{
Expand Down
52 changes: 52 additions & 0 deletions cli/connect/commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package connect

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tarantool/tt/cli/connector"
)

func TestConsoleHistoryOutput(t *testing.T) {
t.Run("empty command list", func(t *testing.T) {
history, err := newCommandHistory("test", 100)
assert.NoError(t, err)
console := Console{history: history}
actual, err := getHistoryFunc(&console, "", nil)
assert.NoError(t, err)
assert.Equal(t, "", actual)
})
t.Run("history is less than max len", func(t *testing.T) {
history, err := newCommandHistory("test", 100)
history.appendCommand("test1")
history.appendCommand("test2")
assert.NoError(t, err)
console := Console{history: history, connOpts: connector.ConnectOpts{MaxOutputHistoryLen: 5}}
actual, err := getHistoryFunc(&console, "", nil)
assert.NoError(t, err)
assert.Equal(t, "test1\n-----\ntest2", actual)
})
t.Run("history is equal max len", func(t *testing.T) {
history, err := newCommandHistory("test", 100)
history.appendCommand("test1")
history.appendCommand("test2")
history.appendCommand("test3")
assert.NoError(t, err)
console := Console{history: history, connOpts: connector.ConnectOpts{MaxOutputHistoryLen: 3}}
actual, err := getHistoryFunc(&console, "", nil)
assert.NoError(t, err)
assert.Equal(t, "test1\n-----\ntest2\n-----\ntest3", actual)
})
t.Run("history is more max len", func(t *testing.T) {
history, err := newCommandHistory("test", 100)
history.appendCommand("test1")
history.appendCommand("test2")
history.appendCommand("test3")
history.appendCommand("test4")
assert.NoError(t, err)
console := Console{history: history, connOpts: connector.ConnectOpts{MaxOutputHistoryLen: 3}}
actual, err := getHistoryFunc(&console, "", nil)
assert.NoError(t, err)
assert.Equal(t, "test2\n-----\ntest3\n-----\ntest4", actual)
})
}
6 changes: 6 additions & 0 deletions cli/connect/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ var setQuit = []string{"\\quit", "\\q"}
// getShortcutsList is a command to get shortcuts and a list of hotkeys.
const getShortcutsList = "\\shortcuts"

// getHistoryList is a command to get history of executed commands.
const getHistoryList = "\\history"

// getHelpCmd is a command to get a help message.
var getHelp = []string{"\\help", "?"}

Expand Down Expand Up @@ -82,3 +85,6 @@ const shortcutListText = `---
Alt + B -- Move backwards one word
Alt + F -- Move forwards one word
...`

// Default maximal count of commands to output in \history command.
const DefaultOutputHistorySize = 10
2 changes: 2 additions & 0 deletions cli/connector/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type ConnectOpts struct {
Password string
// Ssl options for a connection.
Ssl SslOpts
// maximal count of commands to output in \history command.
MaxOutputHistoryLen int
}

// SslOpts is a way to configure SSL connection.
Expand Down
1 change: 1 addition & 0 deletions test/integration/connect/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def test_connect_and_get_commands_outputs(tt_cmd, tmpdir_with_cfg):
\\x[l,t,T,y] -- set output format lua, table, ttable or yaml
\\x[g,G] -- disables/enables pseudographics for table modes
\\shortcuts -- show available hotkeys and shortcuts
\\history -- show history of executed commands
\\quit, \\q -- quit from the console

"""
Expand Down
Loading