Skip to content

Commit a9fae1b

Browse files
committed
refactor: Implement tool plugins with declarative configuration
This major refactoring replaces hardcoded tool configuration with a plugin-based system: - Created a descriptive plugin system for tools using YAML configuration files - Implemented configuration processing with plugins/tool-utils.go - Created tool installation and execution logic in config/tools-installer.go
1 parent 3607792 commit a9fae1b

File tree

14 files changed

+539
-126
lines changed

14 files changed

+539
-126
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ go.work
2222
go.work.sum
2323

2424
.idea/
25+
.vscode/
2526

26-
cli-v2
27+
cli-v2

cmd/analyze.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ var analyzeCmd = &cobra.Command{
205205
}
206206

207207
eslint := config.Config.Tools()["eslint"]
208-
eslintInstallationDirectory := eslint.Info()["installDir"]
208+
eslintInstallationDirectory := eslint.InstallDir
209209
nodeRuntime := config.Config.Runtimes()["node"]
210210
nodeBinary := nodeRuntime.Binaries["node"]
211211

@@ -220,4 +220,4 @@ var analyzeCmd = &cobra.Command{
220220

221221
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)
222222
},
223-
}
223+
}

cmd/install.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
cfg "codacy/cli-v2/config"
5-
"fmt"
65
"log"
76

87
"github.com/spf13/cobra"
@@ -33,18 +32,9 @@ func installRuntimes(config *cfg.ConfigType) {
3332
}
3433

3534
func installTools(config *cfg.ConfigType) {
36-
for _, tool := range config.Tools() {
37-
switch tool.Name() {
38-
case "eslint":
39-
// eslint needs node runtime
40-
nodeRuntime := config.Runtimes()["node"]
41-
err := cfg.InstallEslint(nodeRuntime, tool, registry)
42-
if err != nil {
43-
fmt.Println(err.Error())
44-
log.Fatal(err)
45-
}
46-
default:
47-
log.Fatal("Unknown tool:", tool.Name())
48-
}
35+
// Use the new tools-installer instead of manual installation
36+
err := cfg.InstallTools()
37+
if err != nil {
38+
log.Fatal(err)
4939
}
50-
}
40+
}

config-file/configFile.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,22 @@ func parseConfigFile(configContents []byte) error {
3636
return err
3737
}
3838

39+
// Convert the tool strings to ToolConfig objects
40+
toolConfigs := make([]plugins.ToolConfig, 0, len(configFile.TOOLS))
3941
for _, tl := range configFile.TOOLS {
4042
ct, err := parseConfigTool(tl)
4143
if err != nil {
4244
return err
4345
}
44-
config.Config.AddTool(config.NewRuntime(ct.name, ct.version))
46+
toolConfigs = append(toolConfigs, plugins.ToolConfig{
47+
Name: ct.name,
48+
Version: ct.version,
49+
})
50+
}
51+
52+
// Add all tools at once
53+
if err := config.Config.AddTools(toolConfigs); err != nil {
54+
return err
4555
}
4656

4757
return nil

config/config.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ConfigType struct {
1717
projectConfigFile string
1818

1919
runtimes map[string]*plugins.RuntimeInfo
20-
tools map[string]*Runtime
20+
tools map[string]*plugins.ToolInfo
2121
}
2222

2323
func (c *ConfigType) HomePath() string {
@@ -63,13 +63,23 @@ func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {
6363
return nil
6464
}
6565

66-
// TODO do inheritance with tool
67-
func (c *ConfigType) Tools() map[string]*Runtime {
66+
func (c *ConfigType) Tools() map[string]*plugins.ToolInfo {
6867
return c.tools
6968
}
7069

71-
func (c *ConfigType) AddTool(t *Runtime) {
72-
c.tools[t.Name()] = t
70+
func (c *ConfigType) AddTools(configs []plugins.ToolConfig) error {
71+
// Process the tool configurations using the plugins.ProcessTools function
72+
toolInfoMap, err := plugins.ProcessTools(configs, c.toolsDirectory)
73+
if err != nil {
74+
return err
75+
}
76+
77+
// Store the tool information in the config
78+
for name, info := range toolInfoMap {
79+
c.tools[name] = info
80+
}
81+
82+
return nil
7383
}
7484

7585
func (c *ConfigType) initCodacyDirs() {
@@ -117,7 +127,7 @@ func Init() {
117127
Config.initCodacyDirs()
118128

119129
Config.runtimes = make(map[string]*plugins.RuntimeInfo)
120-
Config.tools = make(map[string]*Runtime)
130+
Config.tools = make(map[string]*plugins.ToolInfo)
121131
}
122132

123133
// Global singleton config-file

config/eslint-utils.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

config/runtime.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)