Skip to content

Commit 7d17211

Browse files
committed
modules: find module with TT_CLI_MODULES_PATH
Closes #1013 @TarantoolBot document Title: Use environment variable to find modules. The TT_CLI_MODULES_PATH environment variable specifies a list of module's paths separated by a colon “:”. It can be set externally from tt and must contain a list of folders where modules are located. The logic of working with this variable is similar to the PATH system variable. Lists of modules are combined into one common list: - The modules declared in the configuration file tt.yaml come first - then the modules defined via the environment variable are added. Any duplicates must be removed, while maintaining the original order of module declarations. **Usage example** Set variable: ```sh export TT_CLI_MODULES_PATH=/ext/path/modules:${TT_CLI_MODULES_PATH} ```
1 parent bf53381 commit 7d17211

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111

1212
- `tt.yaml`: allows to specify a list of modules directories.
13+
- Environment variable TT_CLI_MODULES_PATH can be used to specify
14+
an extra path with modules.
1315

1416
### Changed
1517

cli/modules/modules.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ func fillSubCommandsInfo(cmd *cobra.Command, modulesInfo *ModulesInfo) {
4646
// GetModulesInfo collects information about available modules (both external and internal).
4747
func GetModulesInfo(cmdCtx *cmdcontext.CmdCtx, rootCmd *cobra.Command,
4848
cliOpts *config.CliOpts) (ModulesInfo, error) {
49-
modulesDir, err := getExternalModulesDir(cmdCtx, cliOpts)
49+
modulesDirs, err := getConfigModulesDirs(cmdCtx, cliOpts)
5050
if err != nil {
5151
return nil, err
5252
}
5353

54-
externalModules, err := getExternalModules(modulesDir)
54+
modulesEnvDirs, err := getEnvironmentModulesDirs()
55+
if err != nil {
56+
return nil, err
57+
}
58+
modulesDirs = append(modulesDirs, modulesEnvDirs...)
59+
60+
externalModules, err := getExternalModules(modulesDirs)
5561
if err != nil {
5662
return nil, fmt.Errorf(
5763
"failed to get available external modules information: %s", err)
@@ -72,45 +78,69 @@ func GetModulesInfo(cmdCtx *cmdcontext.CmdCtx, rootCmd *cobra.Command,
7278
return modulesInfo, nil
7379
}
7480

75-
// getExternalModulesDir returns the directory where external modules are located.
76-
func getExternalModulesDir(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts) (string, error) {
81+
// collectDirectoriesList checks list to ensure that all items is directories.
82+
func collectDirectoriesList(paths []string) ([]string, error) {
83+
dirs := make([]string, 0, len(paths))
84+
// We return an error only if the following conditions are met:
85+
// 1. If a directory field is specified;
86+
// 2. Specified path exists;
87+
// 3. Path points to not a directory.
88+
for _, dir := range paths {
89+
if info, err := os.Stat(dir); err == nil {
90+
// TODO: Add warning in next patches, discussion
91+
// what if the file exists, but access is denied, etc.
92+
// FIXME: resolve this question while prepare list:
93+
// https://github.com/tarantool/tt/issues/1014
94+
if !info.IsDir() {
95+
return dirs, fmt.Errorf("specified path in configuration file is not a directory")
96+
}
97+
dirs = append(dirs, dir)
98+
}
99+
}
100+
101+
return dirs, nil
102+
}
103+
104+
// getConfigModulesDirs returns from configuration the list of directories,
105+
// where external modules are located.
106+
func getConfigModulesDirs(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts) ([]string, error) {
77107
// Configuration file not detected - ignore and work on.
78108
// TODO: Add warning in next patches, discussion
79109
// what if the file exists, but access is denied, etc.
80110
if _, err := os.Stat(cmdCtx.Cli.ConfigPath); err != nil {
81111
if !os.IsNotExist(err) {
82-
return "", fmt.Errorf("failed to get access to configuration file: %s", err)
112+
return []string{}, fmt.Errorf("failed to get access to configuration file: %s", err)
83113
}
84114

85-
return "", nil
115+
return []string{}, nil
86116
}
87117

88118
// Unspecified `modules` field is not considered an error.
89119
if cliOpts.Modules == nil {
90-
return "", nil
120+
return []string{}, nil
91121
}
92122

93-
// We return an error only if the following conditions are met:
94-
// 1. If a directory field is specified;
95-
// 2. Specified path exists;
96-
// 3. Path points to not a directory.
97-
// FIXME: Add working with a list https://github.com/tarantool/tt/issues/1014
98-
modulesDir := cliOpts.Modules.Directories[0]
99-
if info, err := os.Stat(modulesDir); err == nil {
100-
// TODO: Add warning in next patches, discussion
101-
// what if the file exists, but access is denied, etc.
102-
if !info.IsDir() {
103-
return "", fmt.Errorf("specified path in configuration file is not a directory")
104-
}
105-
}
123+
return collectDirectoriesList(cliOpts.Modules.Directories)
124+
}
106125

107-
return modulesDir, nil
126+
func getEnvironmentModulesDirs() ([]string, error) {
127+
env_var := os.Getenv("TT_CLI_MODULES_PATH")
128+
if env_var == "" {
129+
return []string{}, nil
130+
}
131+
paths := strings.Split(env_var, ":")
132+
return collectDirectoriesList(paths)
108133
}
109134

110135
// getExternalModules returns map of available modules by
111136
// parsing the contents of the path folder.
112-
func getExternalModules(path string) (map[string]string, error) {
137+
func getExternalModules(paths []string) (map[string]string, error) {
113138
modules := make(map[string]string)
139+
if len(paths) == 0 {
140+
return modules, nil
141+
}
142+
// FIXME: Work with list at https://github.com/tarantool/tt/issues/1014
143+
path := paths[0]
114144

115145
// If the directory doesn't exist, it is not an error.
116146
// TODO: Add warning in next patches, discussion

cli/modules/modules_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME: Create new tests https://github.com/tarantool/tt/issues/1039
2+
//go:build exclude
3+
14
package modules
25

36
import (

0 commit comments

Comments
 (0)