@@ -3,13 +3,15 @@ package cmd
33import (
44 "codacy/cli-v2/config"
55 "codacy/cli-v2/tools"
6+ "codacy/cli-v2/utils"
67 "encoding/json"
78 "errors"
89 "fmt"
910 "io"
1011 "log"
1112 "net/http"
1213 "os"
14+ "path/filepath"
1315 "time"
1416
1517 "github.com/spf13/cobra"
@@ -55,6 +57,7 @@ var initCmd = &cobra.Command{
5557 if err != nil {
5658 log .Fatal (err )
5759 }
60+ createGitIgnoreFile ()
5861 }
5962 fmt .Println ()
6063 fmt .Println ("✅ Successfully initialized Codacy configuration!" )
@@ -66,6 +69,26 @@ var initCmd = &cobra.Command{
6669 },
6770}
6871
72+ func createGitIgnoreFile () error {
73+ gitIgnorePath := filepath .Join (config .Config .LocalCodacyDirectory (), ".gitignore" )
74+ gitIgnoreFile , err := os .Create (gitIgnorePath )
75+ if err != nil {
76+ return fmt .Errorf ("failed to create .gitignore file: %w" , err )
77+ }
78+ defer gitIgnoreFile .Close ()
79+
80+ content := `# Codacy CLI
81+ tools-configs/
82+ .gitignore
83+ cli-config.yaml
84+ `
85+ if _ , err := gitIgnoreFile .WriteString (content ); err != nil {
86+ return fmt .Errorf ("failed to write to .gitignore file: %w" , err )
87+ }
88+
89+ return nil
90+ }
91+
6992func createConfigurationFiles (tools []tools.Tool , cliLocalMode bool ) error {
7093 configFile , err := os .Create (config .Config .ProjectConfigFile ())
7194 defer configFile .Close ()
@@ -142,6 +165,13 @@ func buildRepositoryConfigurationFiles(token string) error {
142165 fmt .Println ("Building repository configuration files ..." )
143166 fmt .Println ("Fetching repository configuration from codacy ..." )
144167
168+ toolsConfigDir := config .Config .ToolsConfigDirectory ()
169+
170+ // Create tools-configs directory if it doesn't exist
171+ if err := os .MkdirAll (toolsConfigDir , utils .DefaultDirPerms ); err != nil {
172+ return fmt .Errorf ("failed to create tools-configs directory: %w" , err )
173+ }
174+
145175 // API call to fetch settings
146176 url := CodacyApiBase + "/2.0/project/analysis/configuration"
147177
@@ -198,7 +228,7 @@ func buildRepositoryConfigurationFiles(token string) error {
198228 eslintDomainConfiguration := convertAPIToolConfigurationToDomain (* eslintApiConfiguration )
199229 eslintConfigurationString := tools .CreateEslintConfig (eslintDomainConfiguration )
200230
201- eslintConfigFile , err := os .Create ("eslint.config.mjs" )
231+ eslintConfigFile , err := os .Create (filepath . Join ( toolsConfigDir , "eslint.config.mjs" ) )
202232 if err != nil {
203233 return fmt .Errorf ("failed to create eslint config file: %v" , err )
204234 }
@@ -210,7 +240,7 @@ func buildRepositoryConfigurationFiles(token string) error {
210240 }
211241 fmt .Println ("ESLint configuration created based on Codacy settings" )
212242 } else {
213- err = createDefaultEslintConfigFile ()
243+ err = createDefaultEslintConfigFile (toolsConfigDir )
214244 if err != nil {
215245 return fmt .Errorf ("failed to create default ESLint config: %v" , err )
216246 }
@@ -220,13 +250,13 @@ func buildRepositoryConfigurationFiles(token string) error {
220250 // Trivy configuration
221251 trivyApiConfiguration := extractTrivyConfiguration (apiToolConfigurations )
222252 if trivyApiConfiguration != nil {
223- err = createTrivyConfigFile (* trivyApiConfiguration )
253+ err = createTrivyConfigFile (* trivyApiConfiguration , toolsConfigDir )
224254 if err != nil {
225255 return fmt .Errorf ("failed to create Trivy config: %v" , err )
226256 }
227257 fmt .Println ("Trivy configuration created based on Codacy settings" )
228258 } else {
229- err = createDefaultTrivyConfigFile ()
259+ err = createDefaultTrivyConfigFile (toolsConfigDir )
230260 if err != nil {
231261 return fmt .Errorf ("failed to create default Trivy config: %v" , err )
232262 }
@@ -236,13 +266,13 @@ func buildRepositoryConfigurationFiles(token string) error {
236266 // PMD configuration
237267 pmdApiConfiguration := extractPMDConfiguration (apiToolConfigurations )
238268 if pmdApiConfiguration != nil {
239- err = createPMDConfigFile (* pmdApiConfiguration )
269+ err = createPMDConfigFile (* pmdApiConfiguration , toolsConfigDir )
240270 if err != nil {
241271 return fmt .Errorf ("failed to create PMD config: %v" , err )
242272 }
243273 fmt .Println ("PMD configuration created based on Codacy settings" )
244274 } else {
245- err = createDefaultPMDConfigFile ()
275+ err = createDefaultPMDConfigFile (toolsConfigDir )
246276 if err != nil {
247277 return fmt .Errorf ("failed to create default PMD config: %v" , err )
248278 }
@@ -318,16 +348,16 @@ func extractPMDConfiguration(toolConfigurations []CodacyToolConfiguration) *Coda
318348 return nil
319349}
320350
321- func createPMDConfigFile (config CodacyToolConfiguration ) error {
351+ func createPMDConfigFile (config CodacyToolConfiguration , toolsConfigDir string ) error {
322352 pmdDomainConfiguration := convertAPIToolConfigurationToDomain (config )
323353 pmdConfigurationString := tools .CreatePmdConfig (pmdDomainConfiguration )
324- return os .WriteFile ("pmd-ruleset.xml" , []byte (pmdConfigurationString ), 0644 )
354+ return os .WriteFile (filepath . Join ( toolsConfigDir , "pmd-ruleset.xml" ) , []byte (pmdConfigurationString ), utils . DefaultRW )
325355}
326356
327- func createDefaultPMDConfigFile () error {
357+ func createDefaultPMDConfigFile (toolsConfigDir string ) error {
328358 emptyConfig := tools.ToolConfiguration {}
329359 content := tools .CreatePmdConfig (emptyConfig )
330- return os .WriteFile ("pmd-ruleset.xml" , []byte (content ), 0644 )
360+ return os .WriteFile (filepath . Join ( toolsConfigDir , "pmd-ruleset.xml" ) , []byte (content ), utils . DefaultRW )
331361}
332362
333363type CodacyToolConfiguration struct {
@@ -347,15 +377,15 @@ type ParameterConfiguration struct {
347377}
348378
349379// createTrivyConfigFile creates a trivy.yaml configuration file based on the API configuration
350- func createTrivyConfigFile (config CodacyToolConfiguration ) error {
380+ func createTrivyConfigFile (config CodacyToolConfiguration , toolsConfigDir string ) error {
351381 // Convert CodacyToolConfiguration to tools.ToolConfiguration
352382 trivyDomainConfiguration := convertAPIToolConfigurationForTrivy (config )
353383
354384 // Use the shared CreateTrivyConfig function to generate the config content
355385 trivyConfigurationString := tools .CreateTrivyConfig (trivyDomainConfiguration )
356386
357387 // Write to file
358- return os .WriteFile ("trivy.yaml" , []byte (trivyConfigurationString ), 0644 )
388+ return os .WriteFile (filepath . Join ( toolsConfigDir , "trivy.yaml" ) , []byte (trivyConfigurationString ), utils . DefaultRW )
359389}
360390
361391// convertAPIToolConfigurationForTrivy converts API tool configuration to domain model for Trivy
@@ -399,21 +429,21 @@ func convertAPIToolConfigurationForTrivy(config CodacyToolConfiguration) tools.T
399429}
400430
401431// createDefaultTrivyConfigFile creates a default trivy.yaml configuration file
402- func createDefaultTrivyConfigFile () error {
432+ func createDefaultTrivyConfigFile (toolsConfigDir string ) error {
403433 // Use empty tool configuration to get default settings
404434 emptyConfig := tools.ToolConfiguration {}
405435 content := tools .CreateTrivyConfig (emptyConfig )
406436
407437 // Write to file
408- return os .WriteFile ("trivy.yaml" , []byte (content ), 0644 )
438+ return os .WriteFile (filepath . Join ( toolsConfigDir , "trivy.yaml" ) , []byte (content ), utils . DefaultRW )
409439}
410440
411441// createDefaultEslintConfigFile creates a default eslint.config.mjs configuration file
412- func createDefaultEslintConfigFile () error {
442+ func createDefaultEslintConfigFile (toolsConfigDir string ) error {
413443 // Use empty tool configuration to get default settings
414444 emptyConfig := tools.ToolConfiguration {}
415445 content := tools .CreateEslintConfig (emptyConfig )
416446
417447 // Write to file
418- return os .WriteFile ("eslint.config.mjs" , []byte (content ), 0644 )
448+ return os .WriteFile (filepath . Join ( toolsConfigDir , "eslint.config.mjs" ) , []byte (content ), utils . DefaultRW )
419449}
0 commit comments