Skip to content

Commit 0301fa9

Browse files
refactor: Simplify tool installation logic by removing temporary extraction directory
- Changed installation process to extract files directly to the installation directory. - Removed unnecessary creation and cleanup of a temporary extraction directory. - Added logic to ensure all binaries are executable after installation. - Updated error messages for clarity.
1 parent 7209462 commit 0301fa9

File tree

2 files changed

+18
-79
lines changed

2 files changed

+18
-79
lines changed

config/tools-installer.go

Lines changed: 11 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"codacy/cli-v2/plugins"
66
"codacy/cli-v2/utils"
77
"fmt"
8-
"io"
98
"log"
109
"os"
1110
"os/exec"
@@ -130,99 +129,32 @@ func installDownloadBasedTool(toolInfo *plugins.ToolInfo) error {
130129
}
131130
defer file.Close()
132131

133-
// Create a temporary extraction directory
134-
tempExtractDir := filepath.Join(Config.ToolsDirectory(), fmt.Sprintf("%s-%s-temp", toolInfo.Name, toolInfo.Version))
135-
136-
// Clean up any previous extraction attempt
137-
os.RemoveAll(tempExtractDir)
138-
139-
// Create the temporary extraction directory
140-
err = os.MkdirAll(tempExtractDir, 0755)
132+
// Create the installation directory
133+
err = os.MkdirAll(toolInfo.InstallDir, 0755)
141134
if err != nil {
142-
return fmt.Errorf("failed to create temporary extraction directory: %w", err)
135+
return fmt.Errorf("failed to create installation directory: %w", err)
143136
}
144137

145-
// Extract to the temporary directory first
138+
// Extract directly to the installation directory
146139
log.Printf("Extracting %s v%s...\n", toolInfo.Name, toolInfo.Version)
147140
if strings.HasSuffix(fileName, ".zip") {
148-
err = utils.ExtractZip(file.Name(), tempExtractDir)
141+
err = utils.ExtractZip(file.Name(), toolInfo.InstallDir)
149142
} else {
150-
err = utils.ExtractTarGz(file, tempExtractDir)
143+
err = utils.ExtractTarGz(file, toolInfo.InstallDir)
151144
}
152145

153146
if err != nil {
154147
return fmt.Errorf("failed to extract tool: %w", err)
155148
}
156149

157-
// Create the final installation directory
158-
err = os.MkdirAll(toolInfo.InstallDir, 0755)
159-
if err != nil {
160-
return fmt.Errorf("failed to create installation directory: %w", err)
161-
}
162-
163-
// Find and copy the tool binaries
164-
for binName, binPath := range toolInfo.Binaries {
165-
// Get the base name of the binary (without the path)
166-
binBaseName := filepath.Base(binPath)
167-
168-
// Try to find the binary in the extracted files
169-
foundPath := ""
170-
171-
// First check if it's at the expected location directly
172-
expectedPath := filepath.Join(tempExtractDir, binBaseName)
173-
if _, err := os.Stat(expectedPath); err == nil {
174-
foundPath = expectedPath
175-
} else {
176-
// Look for the binary anywhere in the extracted directory
177-
err := filepath.Walk(tempExtractDir, func(path string, info os.FileInfo, err error) error {
178-
if err != nil {
179-
return err
180-
}
181-
if !info.IsDir() && filepath.Base(path) == binBaseName {
182-
foundPath = path
183-
return io.EOF // Stop the walk
184-
}
185-
return nil
186-
})
187-
188-
// io.EOF is expected when we find the file and stop the walk
189-
if err != nil && err != io.EOF {
190-
return fmt.Errorf("error searching for %s binary: %w", binName, err)
191-
}
192-
}
193-
194-
if foundPath == "" {
195-
return fmt.Errorf("could not find %s binary in extracted files", binName)
196-
}
197-
198-
// Make sure the destination directory exists
199-
err = os.MkdirAll(filepath.Dir(binPath), 0755)
200-
if err != nil {
201-
return fmt.Errorf("failed to create directory for binary: %w", err)
202-
}
203-
204-
// Copy the binary to the installation directory
205-
input, err := os.Open(foundPath)
206-
if err != nil {
207-
return fmt.Errorf("failed to open %s binary: %w", binName, err)
208-
}
209-
defer input.Close()
210-
211-
output, err := os.OpenFile(binPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
212-
if err != nil {
213-
return fmt.Errorf("failed to create destination file for %s: %w", binName, err)
214-
}
215-
defer output.Close()
216-
217-
_, err = io.Copy(output, input)
218-
if err != nil {
219-
return fmt.Errorf("failed to copy %s binary: %w", binName, err)
150+
// Make sure all binaries are executable
151+
for _, binaryPath := range toolInfo.Binaries {
152+
err = os.Chmod(filepath.Join(toolInfo.InstallDir, filepath.Base(binaryPath)), 0755)
153+
if err != nil && !os.IsNotExist(err) {
154+
return fmt.Errorf("failed to make binary executable: %w", err)
220155
}
221156
}
222157

223-
// Clean up the temporary directory
224-
os.RemoveAll(tempExtractDir)
225-
226158
log.Printf("Successfully installed %s v%s\n", toolInfo.Name, toolInfo.Version)
227159
return nil
228160
}

plugins/tools/trivy/plugin.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ download:
1919
binaries:
2020
- name: trivy
2121
path: "trivy"
22+
formatters:
23+
- name: sarif
24+
flag: "--format sarif"
25+
output_options:
26+
file_flag: "-o"
27+
analysis_options:
28+
default_path: "."

0 commit comments

Comments
 (0)