11package config
22
33import (
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
242196func isTrivyInstalled (trivyConfig * Runtime ) bool {
243197 trivyFolder := fmt .Sprintf ("%s@%s" , trivyConfig .Name (), trivyConfig .Version ())
0 commit comments