Skip to content

Commit ae7a213

Browse files
chore: update Trivy version and refactor plugin configuration handling
- Updated Trivy version from 0.49.1 to 0.50.0 in codacy.yaml - Refactored Trivy plugin configuration loading by introducing a new plugins package - Removed deprecated types and functions related to Trivy plugin configuration - Improved error handling in the plugin configuration loading process
1 parent 3448241 commit ae7a213

File tree

3 files changed

+58
-51
lines changed

3 files changed

+58
-51
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ runtimes:
22
33
tools:
44
5-
- trivy@0.49.1
5+
- trivy@0.50.0

config/trivy-utils.go

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

33
import (
4+
"codacy/cli-v2/plugins"
45
"fmt"
56
"io"
67
"log"
@@ -9,39 +10,8 @@ import (
910
"path/filepath"
1011
"runtime"
1112
"strings"
12-
13-
"gopkg.in/yaml.v3"
1413
)
1514

16-
// TrivyPluginConfig represents the structure of the trivy plugin.yaml file
17-
type TrivyPluginConfig struct {
18-
Name string `yaml:"name"`
19-
Description string `yaml:"description"`
20-
Downloads []TrivyDownloadConfig `yaml:"downloads"`
21-
ArchMapping map[string]string `yaml:"arch_mapping"`
22-
Binaries []TrivyBinaryConfig `yaml:"binaries"`
23-
}
24-
25-
// TrivyDownloadConfig represents the download configuration in plugin.yaml
26-
type TrivyDownloadConfig struct {
27-
OS []string `yaml:"os"`
28-
URLTemplate string `yaml:"url_template"`
29-
FileNameTemplate string `yaml:"file_name_template"`
30-
Extension TrivyExtensionConfig `yaml:"extension"`
31-
}
32-
33-
// TrivyExtensionConfig defines the file extension based on OS
34-
type TrivyExtensionConfig struct {
35-
Windows string `yaml:"windows"`
36-
Default string `yaml:"default"`
37-
}
38-
39-
// TrivyBinaryConfig represents a binary executable
40-
type TrivyBinaryConfig struct {
41-
Name string `yaml:"name"`
42-
Path string `yaml:"path"`
43-
}
44-
4515
/*
4616
* This installs Trivy based on the plugin.yaml configuration
4717
*/
@@ -60,13 +30,13 @@ func InstallTrivy(trivyConfig *Runtime, registry string) error {
6030

6131
// Load the plugin configuration
6232
pluginPath := filepath.Join("plugins", "tools", "trivy", "plugin.yaml")
63-
pluginConfig, err := loadTrivyPluginConfig(pluginPath)
33+
pluginConfig, err := plugins.LoadPluginConfig(pluginPath)
6434
if err != nil {
6535
return fmt.Errorf("failed to load Trivy plugin configuration: %w", err)
6636
}
6737

6838
// Find the download configuration for the current OS
69-
var downloadConfig *TrivyDownloadConfig
39+
var downloadConfig *plugins.DownloadConfig
7040
currentOS := runtime.GOOS
7141
if currentOS == "darwin" {
7242
currentOS = "macos"
@@ -162,7 +132,7 @@ func InstallTrivy(trivyConfig *Runtime, registry string) error {
162132
log.Printf("Extracted Trivy to: %s\n", extractDir)
163133

164134
// Find the binary from the plugin configuration
165-
var binaryConfig *TrivyBinaryConfig
135+
var binaryConfig *plugins.BinaryConfig
166136
if len(pluginConfig.Binaries) > 0 {
167137
binaryConfig = &pluginConfig.Binaries[0]
168138
} else {
@@ -222,22 +192,6 @@ func InstallTrivy(trivyConfig *Runtime, registry string) error {
222192
return nil
223193
}
224194

225-
// loadTrivyPluginConfig loads the Trivy plugin configuration from the plugin.yaml file
226-
func loadTrivyPluginConfig(path string) (*TrivyPluginConfig, error) {
227-
data, err := os.ReadFile(path)
228-
if err != nil {
229-
return nil, fmt.Errorf("error reading plugin config file: %w", err)
230-
}
231-
232-
var config TrivyPluginConfig
233-
err = yaml.Unmarshal(data, &config)
234-
if err != nil {
235-
return nil, fmt.Errorf("error parsing plugin config file: %w", err)
236-
}
237-
238-
return &config, nil
239-
}
240-
241195
// isTrivyInstalled checks if Trivy is already installed
242196
func isTrivyInstalled(trivyConfig *Runtime) bool {
243197
trivyFolder := fmt.Sprintf("%s@%s", trivyConfig.Name(), trivyConfig.Version())

plugins/plugin-config.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package plugins
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
// PluginConfig represents the structure of plugin.yaml files
11+
type PluginConfig struct {
12+
Name string `yaml:"name"`
13+
Description string `yaml:"description"`
14+
Downloads []DownloadConfig `yaml:"downloads"`
15+
ArchMapping map[string]string `yaml:"arch_mapping"`
16+
Binaries []BinaryConfig `yaml:"binaries"`
17+
}
18+
19+
// DownloadConfig represents the download configuration in plugin.yaml
20+
type DownloadConfig struct {
21+
OS []string `yaml:"os"`
22+
URLTemplate string `yaml:"url_template"`
23+
FileNameTemplate string `yaml:"file_name_template"`
24+
Extension ExtensionConfig `yaml:"extension"`
25+
}
26+
27+
// ExtensionConfig defines the file extension based on OS
28+
type ExtensionConfig struct {
29+
Windows string `yaml:"windows"`
30+
Default string `yaml:"default"`
31+
}
32+
33+
// BinaryConfig represents a binary executable
34+
type BinaryConfig struct {
35+
Name string `yaml:"name"`
36+
Path string `yaml:"path"`
37+
}
38+
39+
// LoadPluginConfig loads a plugin configuration from the given path
40+
func LoadPluginConfig(path string) (*PluginConfig, error) {
41+
data, err := os.ReadFile(path)
42+
if err != nil {
43+
return nil, fmt.Errorf("error reading plugin config file: %w", err)
44+
}
45+
46+
var config PluginConfig
47+
err = yaml.Unmarshal(data, &config)
48+
if err != nil {
49+
return nil, fmt.Errorf("error parsing plugin config file: %w", err)
50+
}
51+
52+
return &config, nil
53+
}

0 commit comments

Comments
 (0)