@@ -4,13 +4,15 @@ import (
44 "codacy/cli-v2/config"
55 "codacy/cli-v2/domain"
66 "codacy/cli-v2/tools"
7+ "codacy/cli-v2/utils"
78 "encoding/json"
89 "errors"
910 "fmt"
1011 "io"
1112 "log"
1213 "net/http"
1314 "os"
15+ "path/filepath"
1416 "time"
1517
1618 "github.com/spf13/cobra"
@@ -53,6 +55,7 @@ var initCmd = &cobra.Command{
5355 if err != nil {
5456 log .Fatal (err )
5557 }
58+ createGitIgnoreFile ()
5659 }
5760 fmt .Println ()
5861 fmt .Println ("✅ Successfully initialized Codacy configuration!" )
@@ -64,6 +67,26 @@ var initCmd = &cobra.Command{
6467 },
6568}
6669
70+ func createGitIgnoreFile () error {
71+ gitIgnorePath := filepath .Join (config .Config .LocalCodacyDirectory (), ".gitignore" )
72+ gitIgnoreFile , err := os .Create (gitIgnorePath )
73+ if err != nil {
74+ return fmt .Errorf ("failed to create .gitignore file: %w" , err )
75+ }
76+ defer gitIgnoreFile .Close ()
77+
78+ content := `# Codacy CLI
79+ tools-configs/
80+ .gitignore
81+ cli-config.yaml
82+ `
83+ if _ , err := gitIgnoreFile .WriteString (content ); err != nil {
84+ return fmt .Errorf ("failed to write to .gitignore file: %w" , err )
85+ }
86+
87+ return nil
88+ }
89+
6790func createConfigurationFiles (tools []tools.Tool , cliLocalMode bool ) error {
6891 configFile , err := os .Create (config .Config .ProjectConfigFile ())
6992 defer configFile .Close ()
@@ -137,6 +160,13 @@ func cliConfigFileTemplate(cliLocalMode bool) string {
137160func buildRepositoryConfigurationFiles (token string ) error {
138161 fmt .Println ("Fetching repository configuration from codacy using api token ..." )
139162
163+ toolsConfigDir := config .Config .ToolsConfigDirectory ()
164+
165+ // Create tools-configs directory if it doesn't exist
166+ if err := os .MkdirAll (toolsConfigDir , utils .DefaultDirPerms ); err != nil {
167+ return fmt .Errorf ("failed to create tools-configs directory: %w" , err )
168+ }
169+
140170 client := & http.Client {
141171 Timeout : 10 * time .Second ,
142172 }
@@ -212,12 +242,13 @@ func buildRepositoryConfigurationFiles(token string) error {
212242
213243// map tool uuid to tool name
214244func createToolFileConfigurations (tool tools.Tool , patternConfiguration []domain.PatternConfiguration ) error {
245+ toolsConfigDir := config .Config .ToolsConfigDirectory ()
215246 switch tool .Uuid {
216247 case ESLint :
217248 if len (patternConfiguration ) > 0 {
218249 eslintConfigurationString := tools .CreateEslintConfig (patternConfiguration )
219250
220- eslintConfigFile , err := os .Create ("eslint.config.mjs" )
251+ eslintConfigFile , err := os .Create (filepath . Join ( toolsConfigDir , "eslint.config.mjs" ) )
221252 if err != nil {
222253 return fmt .Errorf ("failed to create eslint config file: %v" , err )
223254 }
@@ -229,67 +260,79 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
229260 }
230261 fmt .Println ("ESLint configuration created based on Codacy settings" )
231262 } else {
232- err := createDefaultEslintConfigFile ()
263+ err := createDefaultEslintConfigFile (toolsConfigDir )
233264 if err != nil {
234265 return fmt .Errorf ("failed to create default ESLint config: %v" , err )
235266 }
236267 fmt .Println ("Default ESLint configuration created" )
237268 }
238269 case Trivy :
239270 if len (patternConfiguration ) > 0 {
240- createTrivyConfigFile (patternConfiguration )
271+ err := createTrivyConfigFile (patternConfiguration , toolsConfigDir )
272+ if err != nil {
273+ return fmt .Errorf ("failed to create Trivy config: %v" , err )
274+ }
241275 } else {
242- createDefaultTrivyConfigFile ()
276+ err := createDefaultTrivyConfigFile (toolsConfigDir )
277+ if err != nil {
278+ return fmt .Errorf ("failed to create default Trivy config: %v" , err )
279+ }
243280 }
244281 fmt .Println ("Trivy configuration created based on Codacy settings" )
245282 case PMD :
246283 if len (patternConfiguration ) > 0 {
247- createPMDConfigFile (patternConfiguration )
284+ err := createPMDConfigFile (patternConfiguration , toolsConfigDir )
285+ if err != nil {
286+ return fmt .Errorf ("failed to create PMD config: %v" , err )
287+ }
248288 } else {
249- createDefaultPMDConfigFile ()
289+ err := createDefaultPMDConfigFile (toolsConfigDir )
290+ if err != nil {
291+ return fmt .Errorf ("failed to create default PMD config: %v" , err )
292+ }
250293 }
251294 fmt .Println ("PMD configuration created based on Codacy settings" )
252295 }
253296 return nil
254297}
255298
256- func createPMDConfigFile (config []domain.PatternConfiguration ) error {
299+ func createPMDConfigFile (config []domain.PatternConfiguration , toolsConfigDir string ) error {
257300 pmdConfigurationString := tools .CreatePmdConfig (config )
258- return os .WriteFile ("pmd-ruleset.xml" , []byte (pmdConfigurationString ), 0644 )
301+ return os .WriteFile (filepath . Join ( toolsConfigDir , "pmd-ruleset.xml" ) , []byte (pmdConfigurationString ), utils . DefaultRW )
259302}
260303
261- func createDefaultPMDConfigFile () error {
304+ func createDefaultPMDConfigFile (toolsConfigDir string ) error {
262305 content := tools .CreatePmdConfig ([]domain.PatternConfiguration {})
263- return os .WriteFile ("pmd-ruleset.xml" , []byte (content ), 0644 )
306+ return os .WriteFile (filepath . Join ( toolsConfigDir , "pmd-ruleset.xml" ) , []byte (content ), utils . DefaultRW )
264307}
265308
266309// createTrivyConfigFile creates a trivy.yaml configuration file based on the API configuration
267- func createTrivyConfigFile (config []domain.PatternConfiguration ) error {
310+ func createTrivyConfigFile (config []domain.PatternConfiguration , toolsConfigDir string ) error {
268311
269312 trivyConfigurationString := tools .CreateTrivyConfig (config )
270313
271314 // Write to file
272- return os .WriteFile ("trivy.yaml" , []byte (trivyConfigurationString ), 0644 )
315+ return os .WriteFile (filepath . Join ( toolsConfigDir , "trivy.yaml" ) , []byte (trivyConfigurationString ), utils . DefaultRW )
273316}
274317
275318// createDefaultTrivyConfigFile creates a default trivy.yaml configuration file
276- func createDefaultTrivyConfigFile () error {
319+ func createDefaultTrivyConfigFile (toolsConfigDir string ) error {
277320 // Use empty tool configuration to get default settings
278321 emptyConfig := []domain.PatternConfiguration {}
279322 content := tools .CreateTrivyConfig (emptyConfig )
280323
281324 // Write to file
282- return os .WriteFile ("trivy.yaml" , []byte (content ), 0644 )
325+ return os .WriteFile (filepath . Join ( toolsConfigDir , "trivy.yaml" ) , []byte (content ), utils . DefaultRW )
283326}
284327
285328// createDefaultEslintConfigFile creates a default eslint.config.mjs configuration file
286- func createDefaultEslintConfigFile () error {
329+ func createDefaultEslintConfigFile (toolsConfigDir string ) error {
287330 // Use empty tool configuration to get default settings
288331 emptyConfig := []domain.PatternConfiguration {}
289332 content := tools .CreateEslintConfig (emptyConfig )
290333
291334 // Write to file
292- return os .WriteFile ("eslint.config.mjs" , []byte (content ), 0644 )
335+ return os .WriteFile (filepath . Join ( toolsConfigDir , "eslint.config.mjs" ) , []byte (content ), utils . DefaultRW )
293336}
294337
295338const (
0 commit comments