@@ -7,13 +7,16 @@ import (
7
7
"path/filepath"
8
8
"strings"
9
9
10
+ "github.com/apex/log"
10
11
"github.com/spf13/cobra"
11
12
"github.com/tarantool/tt/cli/cmdcontext"
12
13
"github.com/tarantool/tt/cli/config"
14
+ "github.com/tarantool/tt/cli/util"
13
15
)
14
16
15
17
const (
16
- helpModuleName = "help"
18
+ manifestFileName = "manifest"
19
+ mainEntryPoint = "main"
17
20
)
18
21
19
22
// ModuleInfo stores information about Tarantool CLI module.
@@ -27,6 +30,19 @@ type ModuleInfo struct {
27
30
// ModulesInfo stores information about all CLI modules.
28
31
type ModulesInfo map [string ]* ModuleInfo
29
32
33
+ // modulesEntries keeps detected entry points while scan modules.
34
+ type modulesEntries struct {
35
+ // Modules location path.
36
+ Directory string
37
+ // Path to manifest.yaml file.
38
+ Manifest string
39
+ // Path to `main` executable file.
40
+ Main string
41
+ }
42
+
43
+ // posibleModules map module name with found its entry points.
44
+ type posibleModules map [string ]modulesEntries
45
+
30
46
// fillSubCommandsInfo collects information about subcommands.
31
47
func fillSubCommandsInfo (cmd * cobra.Command , modulesInfo * ModulesInfo ) {
32
48
for _ , subCmd := range cmd .Commands () {
@@ -57,21 +73,23 @@ func GetModulesInfo(cmdCtx *cmdcontext.CmdCtx, rootCmd *cobra.Command,
57
73
}
58
74
modulesDirs = append (modulesDirs , modulesEnvDirs ... )
59
75
60
- externalModules , err := getExternalModules (modulesDirs )
76
+ // FIXME: working with modules list at https://github.com/tarantool/tt/issues/1016
77
+ /* externalModules */
78
+ _ , err = getExternalModules (modulesDirs )
61
79
if err != nil {
62
80
return nil , fmt .Errorf (
63
81
"failed to get available external modules information: %s" , err )
64
82
}
65
83
66
84
// External modules have a higher priority than internal.
67
85
modulesInfo := ModulesInfo {}
68
- for name , path := range externalModules {
69
- commandPath := rootCmd .Name () + " " + name
70
- modulesInfo [commandPath ] = & ModuleInfo {
71
- IsInternal : false ,
72
- ExternalPath : path ,
73
- }
74
- }
86
+ // for name, path := range externalModules {
87
+ // commandPath := rootCmd.Name() + " " + name
88
+ // modulesInfo[commandPath] = &ModuleInfo{
89
+ // IsInternal: false,
90
+ // ExternalPath: path,
91
+ // }
92
+ // }
75
93
76
94
fillSubCommandsInfo (rootCmd , & modulesInfo )
77
95
@@ -132,38 +150,45 @@ func getEnvironmentModulesDirs() ([]string, error) {
132
150
return collectDirectoriesList (paths )
133
151
}
134
152
135
- // getExternalModules returns map of available modules by
136
- // parsing the contents of the path folder.
137
- func getExternalModules (paths []string ) (map [string ]string , error ) {
138
- modules := make (map [string ]string )
139
- if len (paths ) == 0 {
140
- return modules , nil
153
+ // isPossibleModule checks is exists any manifest or executable `main` file inside dir.
154
+ func isPossibleModule (dir string ) (modulesEntries , bool ) {
155
+ is_module := false
156
+ entries := modulesEntries {Directory : dir }
157
+ manifest , _ := util .GetYamlFileName (filepath .Join (dir , manifestFileName ), false )
158
+ if manifest != "" {
159
+ entries .Manifest = manifest
160
+ is_module = true
141
161
}
142
- // FIXME: Work with list at https://github.com/tarantool/tt/issues/1014
143
- path := paths [0 ]
162
+ if main , err := exec .LookPath (filepath .Join (dir , mainEntryPoint )); err == nil {
163
+ entries .Main = main
164
+ is_module = true
165
+ }
166
+ return entries , is_module
167
+ }
144
168
145
- // If the directory doesn't exist, it is not an error.
146
- // TODO: Add warning in next patches, discussion
147
- // what if the file exists, but access is denied, etc.
148
- if _ , err := os .Stat (path ); err != nil {
149
- if ! os .IsNotExist (err ) {
169
+ // getExternalModules returns map[name] = directory of available modules by
170
+ // parsing the contents of the list folders.
171
+ func getExternalModules (paths []string ) (posibleModules , error ) {
172
+ modules := posibleModules {}
173
+ for _ , path := range paths {
174
+ entries , err := os .ReadDir (path )
175
+ if err != nil {
150
176
return nil , fmt .Errorf (`failed to read "%s" directory: %s` , path , err )
151
177
}
152
-
153
- return nil , nil
154
- }
155
-
156
- files , err := os . ReadDir ( path )
157
- if err != nil {
158
- return nil , fmt . Errorf ( `failed to read "%s" directory: %s` , path , err )
159
- }
160
-
161
- for _ , f := range files {
162
- // Ignore non executable files.
163
- if path , err := exec . LookPath ( filepath . Join ( path , f . Name ())); err == nil {
164
- modules [ strings . Split ( f . Name (), "." )[ 0 ]] = path
178
+ cnt_modules := 0
179
+ for _ , e := range entries {
180
+ mod_path := filepath . Join ( path , e . Name ())
181
+ if ! e . IsDir () {
182
+ continue
183
+ }
184
+ if mod_entry , is_module := isPossibleModule ( mod_path ); is_module {
185
+ modules [ e . Name ()] = mod_entry
186
+ cnt_modules += 1
187
+ }
188
+ }
189
+ if cnt_modules == 0 {
190
+ log . Warnf ( "Directory %q does not have any module" , path )
165
191
}
166
192
}
167
-
168
193
return modules , nil
169
194
}
0 commit comments