|
5 | 5 | "codacy/cli-v2/plugins" |
6 | 6 | "codacy/cli-v2/utils" |
7 | 7 | "fmt" |
8 | | - "io" |
9 | 8 | "log" |
10 | 9 | "os" |
11 | 10 | "os/exec" |
@@ -130,99 +129,32 @@ func installDownloadBasedTool(toolInfo *plugins.ToolInfo) error { |
130 | 129 | } |
131 | 130 | defer file.Close() |
132 | 131 |
|
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) |
141 | 134 | 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) |
143 | 136 | } |
144 | 137 |
|
145 | | - // Extract to the temporary directory first |
| 138 | + // Extract directly to the installation directory |
146 | 139 | log.Printf("Extracting %s v%s...\n", toolInfo.Name, toolInfo.Version) |
147 | 140 | if strings.HasSuffix(fileName, ".zip") { |
148 | | - err = utils.ExtractZip(file.Name(), tempExtractDir) |
| 141 | + err = utils.ExtractZip(file.Name(), toolInfo.InstallDir) |
149 | 142 | } else { |
150 | | - err = utils.ExtractTarGz(file, tempExtractDir) |
| 143 | + err = utils.ExtractTarGz(file, toolInfo.InstallDir) |
151 | 144 | } |
152 | 145 |
|
153 | 146 | if err != nil { |
154 | 147 | return fmt.Errorf("failed to extract tool: %w", err) |
155 | 148 | } |
156 | 149 |
|
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) |
220 | 155 | } |
221 | 156 | } |
222 | 157 |
|
223 | | - // Clean up the temporary directory |
224 | | - os.RemoveAll(tempExtractDir) |
225 | | - |
226 | 158 | log.Printf("Successfully installed %s v%s\n", toolInfo.Name, toolInfo.Version) |
227 | 159 | return nil |
228 | 160 | } |
|
0 commit comments