Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: autorequire frequently used modules #10014

Merged

Conversation

Totktonada
Copy link
Member

@Totktonada Totktonada commented May 15, 2024

There are built-in modules that are frequently used for administration or debugging purposes. It is convenient to have them accessible in the interactive console without extra actions.

They're accessible now without a manual require call if the console_session_scope_vars compat option is set to new (see also tarantool/doc#4191).

The list of the autorequired modules is below.

  • clock
  • compat
  • config
  • datetime
  • decimal
  • ffi
  • fiber
  • fio
  • fun
  • json
  • log
  • msgpack
  • popen
  • uuid
  • varbinary
  • yaml

See #9986 for motivation behind this feature.

This list forms so called initial environment for an interactive console session. The default initial environment may be adjusted by an application, for example, to include application specific administrator functions.

Two public functions are added for this purpose: console.initial_env() and console.set_initial_env(env).

Example 1 (keep autorequired modules, but add one more variable):

local console = require('console')

-- Add myapp_info function.
local initial_env = console.initial_env()
initial_env.myapp_info = function()
    <...>
end

Example 2 (replace the whole initial environment):

local console = require('console')

-- Add myapp_info function, discard the autorequired modules.
console.set_initial_env({
    myapp_info = function()
        <...>
    end,
})

The console.set_initial_env() call without an argument or with a nil argument drops the initial environment to its default.

A modification of the initial environment doesn't affect existing console sessions. It affects console sessions that are created after the modification.

Fixes #9986

@Totktonada Totktonada requested a review from a team as a code owner May 15, 2024 17:21
@coveralls
Copy link

coveralls commented May 15, 2024

Coverage Status

coverage: 87.089% (+0.003%) from 87.086%
when pulling e29c4e8 on Totktonada:gh-9986-console-autorequire-modules
into 1c4605b
on tarantool:master
.

Copy link
Collaborator

@Buristan Buristan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Alexander!
Thanks for the patchset!
I'll proceed with the review per-patch now.

[PATCH 1/2] console: define module table earlier
LGTM, as trivial.

Copy link
Collaborator

@Buristan Buristan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[PATCH 2/2] console: autorequire frequently used modules
Hi, Alexnader!
Thanks for the patch.
Generally, LGTM, but have some ignorable nits below.

src/box/lua/console.lua Show resolved Hide resolved
test/app-luatest/console_autorequire_test.lua Outdated Show resolved Hide resolved
test/app-luatest/console_autorequire_test.lua Show resolved Hide resolved
src/box/lua/console.lua Outdated Show resolved Hide resolved
I need to capture the module table inside a module function in a next
commit.

NO_DOC=refactoring, no behavior changes
NO_CHANGELOG=see NO_DOC
NO_TEST=see NO_DOC
@Totktonada Totktonada force-pushed the gh-9986-console-autorequire-modules branch from f7ecd85 to 81c6606 Compare May 16, 2024 09:43
changelogs/unreleased/console-autorequire.md Outdated Show resolved Hide resolved
Copy link
Contributor

@sergos sergos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!
LGTM

Fixes tarantool#9986

@TarantoolBot document
Title: Interactive console now autorequires a couple of built-in modules

There are built-in modules that are frequently used for administration
or debugging purposes. It is convenient to have them accessible in the
interactive console without extra actions.

They're accessible now without a manual `require` call if the
`console_session_scope_vars` compat option is set to `new` (see also
tarantool/doc#4191).

The list of the autorequired modules is below.

* clock
* compat
* config
* datetime
* decimal
* ffi
* fiber
* fio
* fun
* json
* log
* msgpack
* popen
* uuid
* varbinary
* yaml

See tarantool#9986 for motivation behind this feature.

This list forms so called initial environment for an interactive console
session. The default initial environment may be adjusted by an
application, for example, to include application specific administrator
functions.

Two public functions are added for this purpose: `console.initial_env()`
and `console.set_initial_env(env)`.

Example 1 (keep autorequired modules, but add one more variable):

```lua
local console = require('console')

-- Add myapp_info function.
local initial_env = console.initial_env()
initial_env.myapp_info = function()
    <...>
end
```

Example 2 (replace the whole initial environment):

```lua
local console = require('console')

-- Add myapp_info function, discard the autorequired modules.
console.set_initial_env({
    myapp_info = function()
        <...>
    end,
})
```

The `console.set_initial_env()` call without an argument or with a `nil`
argument drops the initial environment to its default.

A modification of the initial environment doesn't affect existing
console sessions. It affects console sessions that are created
after the modification.

Please, adjust the `console_session_scope_vars` compat option
description and extend the built-in `console` module reference with the
new functions.
@Totktonada Totktonada force-pushed the gh-9986-console-autorequire-modules branch from 81c6606 to e29c4e8 Compare May 17, 2024 07:04
@Totktonada Totktonada added the full-ci Enables all tests for a pull request label May 17, 2024
@Totktonada Totktonada merged commit 8c9965a into tarantool:master May 17, 2024
93 checks passed
@Totktonada Totktonada deleted the gh-9986-console-autorequire-modules branch May 17, 2024 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full-ci Enables all tests for a pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

console: autorequire modules suitable for administration and debugging
5 participants