Skip to content

Commit b4bfe46

Browse files
fixed problem with initializition due to tools processing
1 parent b3932f5 commit b4bfe46

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-14
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ runtimes:
22
33
44
tools:
5-
- eslint@8.57.0
5+
- eslint@9.3.0
66
77
88

cmd/init.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ var initCmd = &cobra.Command{
4242
Short: "Bootstraps project configuration",
4343
Long: "Bootstraps project configuration, creates codacy configuration file",
4444
Run: func(cmd *cobra.Command, args []string) {
45-
config.Config.CreateLocalCodacyDir()
45+
// Create local codacy directory first
46+
if err := config.Config.CreateLocalCodacyDir(); err != nil {
47+
log.Fatalf("Failed to create local codacy directory: %v", err)
48+
}
49+
50+
// Create tools-configs directory
51+
toolsConfigDir := config.Config.ToolsConfigDirectory()
52+
if err := os.MkdirAll(toolsConfigDir, 0777); err != nil {
53+
log.Fatalf("Failed to create tools-configs directory: %v", err)
54+
}
4655

4756
cliLocalMode := len(initFlags.apiToken) == 0
4857

@@ -93,25 +102,27 @@ cli-config.yaml
93102

94103
func createConfigurationFiles(tools []tools.Tool, cliLocalMode bool) error {
95104
configFile, err := os.Create(config.Config.ProjectConfigFile())
96-
defer configFile.Close()
97105
if err != nil {
98-
log.Fatal(err)
106+
return fmt.Errorf("failed to create project config file: %w", err)
99107
}
108+
defer configFile.Close()
100109

101-
_, err = configFile.WriteString(configFileTemplate(tools))
110+
configContent := configFileTemplate(tools)
111+
_, err = configFile.WriteString(configContent)
102112
if err != nil {
103-
log.Fatal(err)
113+
return fmt.Errorf("failed to write project config file: %w", err)
104114
}
105115

106116
cliConfigFile, err := os.Create(config.Config.CliConfigFile())
107-
defer cliConfigFile.Close()
108117
if err != nil {
109-
log.Fatal(err)
118+
return fmt.Errorf("failed to create CLI config file: %w", err)
110119
}
120+
defer cliConfigFile.Close()
111121

112-
_, err = cliConfigFile.WriteString(cliConfigFileTemplate(cliLocalMode))
122+
cliConfigContent := cliConfigFileTemplate(cliLocalMode)
123+
_, err = cliConfigFile.WriteString(cliConfigContent)
113124
if err != nil {
114-
log.Fatal(err)
125+
return fmt.Errorf("failed to write CLI config file: %w", err)
115126
}
116127

117128
return nil
@@ -162,7 +173,7 @@ func cliConfigFileTemplate(cliLocalMode bool) string {
162173
}
163174

164175
func buildRepositoryConfigurationFiles(token string) error {
165-
fmt.Println("Fetching repository configuration from codacy using api token ...")
176+
fmt.Println("Fetching repository configuration from codacy ...")
166177

167178
toolsConfigDir := config.Config.ToolsConfigDirectory()
168179

plugins/tool-utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,34 @@ func getDownloadURL(urlTemplate string, fileName string, version string, mappedA
294294

295295
return buf.String()
296296
}
297+
298+
// GetSupportedTools returns a map of supported tool names based on the tools folder
299+
func GetSupportedTools() (map[string]struct{}, error) {
300+
supportedTools := make(map[string]struct{})
301+
302+
// Read all directories in the tools folder
303+
entries, err := toolsFS.ReadDir("tools")
304+
if err != nil {
305+
return nil, fmt.Errorf("failed to read tools directory: %w", err)
306+
}
307+
308+
// For each directory, check if it has a plugin.yaml file
309+
for _, entry := range entries {
310+
if !entry.IsDir() {
311+
continue
312+
}
313+
314+
toolName := entry.Name()
315+
pluginPath := filepath.Join("tools", toolName, "plugin.yaml")
316+
317+
// Check if plugin.yaml exists
318+
_, err := toolsFS.ReadFile(pluginPath)
319+
if err != nil {
320+
continue // Skip if no plugin.yaml
321+
}
322+
323+
supportedTools[toolName] = struct{}{}
324+
}
325+
326+
return supportedTools, nil
327+
}

plugins/tool-utils_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,45 @@ func TestProcessToolsWithDownload(t *testing.T) {
152152
}
153153
assert.Contains(t, trivyInfo.DownloadURL, expectedArch)
154154
}
155+
156+
func TestGetSupportedTools(t *testing.T) {
157+
tests := []struct {
158+
name string
159+
expectedTools []string
160+
expectedError bool
161+
}{
162+
{
163+
name: "should return supported tools",
164+
expectedTools: []string{
165+
"eslint",
166+
"pmd",
167+
"pylint",
168+
"trivy",
169+
},
170+
expectedError: false,
171+
},
172+
}
173+
174+
for _, tt := range tests {
175+
t.Run(tt.name, func(t *testing.T) {
176+
supportedTools, err := GetSupportedTools()
177+
178+
if tt.expectedError {
179+
assert.Error(t, err)
180+
return
181+
}
182+
183+
assert.NoError(t, err)
184+
assert.NotNil(t, supportedTools)
185+
186+
// Check that all expected tools are supported
187+
for _, expectedTool := range tt.expectedTools {
188+
_, exists := supportedTools[expectedTool]
189+
assert.True(t, exists, "tool %s should be supported", expectedTool)
190+
}
191+
192+
// Check that we have exactly the expected number of tools
193+
assert.Equal(t, len(tt.expectedTools), len(supportedTools), "number of supported tools should match")
194+
})
195+
}
196+
}

tools/getTools.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tools
22

33
import (
4-
"codacy/cli-v2/config"
4+
"codacy/cli-v2/plugins"
55
"encoding/json"
66
"errors"
77
"fmt"
@@ -107,13 +107,16 @@ func GetRepositoryTools(codacyBase string, apiToken string, provider string, org
107107
return nil, err
108108
}
109109

110-
cliSupportedTools := config.Config.Tools()
110+
supportedTools, err := plugins.GetSupportedTools()
111+
if err != nil {
112+
return nil, err
113+
}
111114

112115
// Filter enabled tools
113116
var enabledTools []Tool
114117
for _, tool := range response.Data {
115118
if tool.Settings.Enabled {
116-
if _, exists := cliSupportedTools[strings.ToLower(tool.Name)]; exists {
119+
if _, exists := supportedTools[strings.ToLower(tool.Name)]; exists {
117120
enabledTools = append(enabledTools, tool)
118121
}
119122
}

0 commit comments

Comments
 (0)