Skip to content

Commit b2d1358

Browse files
better0fdeadpsergee
authored andcommitted
tt: add env module
Added module that adds 'tt' and 'tarantool' (installed using 'tt') to the current environment. Closes #338
1 parent 24ecbdf commit b2d1358

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111

1212
- Make cartridge app dependencies less strict.
1313

14+
### Added
15+
16+
- `tt env`: add current environment binaries location to the PATH variable.
17+
1418
## [2.0.0] - 2023-11-13
1519

1620
### Changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Tarantool-based applications.
3434
* [Working with application templates](#Working-with-application-templates)
3535
* [Working with tt daemon (experimental)](#working-with-tt-daemon-experimental)
3636
* [Setting Tarantool configuration parameters via environment variables](#setting-tarantool-configuration-parameters-via-environment-variables)
37+
* [Add current environment binaries location to the PATH variable](#add-current-environment-binaries-location-to-the-path-variable)
3738
* [Migration from older TT versions](doc/migration_from_older_versions.md)
3839
* [Commands](#commands)
3940

@@ -578,6 +579,16 @@ support it. The name of a variable should have the following pattern:
578579
[box.cfg](https://www.tarantool.io/en/doc/latest/reference/configuration/#box-cfg-params-ref)
579580
parameter.
580581

582+
### Add current environment binaries location to the PATH variable.
583+
584+
You can add current environment binaries location to the PATH variable:
585+
586+
``` console
587+
. <(tt env)
588+
```
589+
590+
Also TARANTOOL_DIR variable is set.
591+
581592
## Commands
582593

583594
Common description. For a detailed description, use `tt help command` .
@@ -611,6 +622,7 @@ Common description. For a detailed description, use `tt help command` .
611622
- `instances` - show enabled applications.
612623
- `binaries` - show a list of installed binaries and their versions.
613624
- `cluster` - manage cluster configuration.
625+
- `env` - add current environment binaries location to the PATH variable.
614626

615627
[godoc-badge]: https://pkg.go.dev/badge/github.com/tarantool/tt.svg
616628
[godoc-url]: https://pkg.go.dev/github.com/tarantool/tt

cli/cmd/env.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/tarantool/tt/cli/cmdcontext"
8+
"github.com/tarantool/tt/cli/env"
9+
"github.com/tarantool/tt/cli/modules"
10+
)
11+
12+
// NewEnvCmd creates env command.
13+
func NewEnvCmd() *cobra.Command {
14+
var envCmd = &cobra.Command{
15+
Use: "env",
16+
Short: "Add current environment binaries location to the PATH variable",
17+
Long: "Add current environment binaries location to the PATH variable.\n" +
18+
"Also sets TARANTOOL_DIR variable.",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
21+
internalEnvModule, args)
22+
handleCmdErr(cmd, err)
23+
},
24+
}
25+
26+
return envCmd
27+
}
28+
29+
// internalEnvModule is a default env module.
30+
func internalEnvModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
31+
var err error
32+
_, err = fmt.Print(env.CreateEnvString(cliOpts))
33+
return err
34+
}

cli/cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func NewCmdRoot() *cobra.Command {
163163
NewCfgCmd(),
164164
NewInstancesCmd(),
165165
NewBinariesCmd(),
166+
NewEnvCmd(),
166167
)
167168
if err := injectCmds(rootCmd); err != nil {
168169
panic(err.Error())

cli/env/env.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package env
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/tarantool/tt/cli/config"
10+
)
11+
12+
// CreateEnvString generates environment variables for 'tarantool' and 'tt' installed using 'tt'.
13+
func CreateEnvString(cliOpts *config.CliOpts) string {
14+
binDir := cliOpts.Env.BinDir
15+
path := os.Getenv("PATH")
16+
if !strings.Contains(path, binDir) {
17+
path = binDir + ":" + path
18+
}
19+
20+
tarantoolDir := filepath.Join(cliOpts.Env.IncludeDir, "include")
21+
22+
return fmt.Sprintf("export %s=%s\nexport %s=%s\n", "PATH", path, "TARANTOOL_DIR", tarantoolDir)
23+
}

cli/env/env_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package env
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/tarantool/tt/cli/configure"
8+
)
9+
10+
func Test_CreateEnvString(t *testing.T) {
11+
cliOpts := configure.GetDefaultCliOpts()
12+
cliOpts.Env.BinDir = "foo/bin/"
13+
cliOpts.Env.IncludeDir = "bar/include/"
14+
assert.Contains(t, CreateEnvString(cliOpts),
15+
"\nexport TARANTOOL_DIR=bar/include/include\n")
16+
assert.Contains(t, CreateEnvString(cliOpts),
17+
"export PATH=foo/bin/:")
18+
}

test/integration/env/test_env.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import re
3+
import subprocess
4+
5+
from utils import config_name
6+
7+
8+
def test_env_output(tt_cmd, tmpdir):
9+
configPath = os.path.join(tmpdir, config_name)
10+
# Create test config
11+
with open(configPath, 'w') as f:
12+
f.write('env:\n bin_dir:\n inc_dir:\n')
13+
binDir = str(tmpdir + "/bin")
14+
tarantoolDir = "TARANTOOL_DIR=" + str(tmpdir + "/include/include")
15+
16+
env_cmd = [tt_cmd, "env"]
17+
instance_process = subprocess.Popen(
18+
env_cmd,
19+
cwd=tmpdir,
20+
stderr=subprocess.STDOUT,
21+
stdout=subprocess.PIPE,
22+
text=True
23+
)
24+
25+
# Check that the process shutdowned correctly.
26+
instance_process_rc = instance_process.wait()
27+
assert instance_process_rc == 0
28+
29+
# Check output
30+
output = instance_process.stdout.read()
31+
assert re.search(tarantoolDir, output)
32+
assert re.search(binDir, output)

0 commit comments

Comments
 (0)