Skip to content

Commit e0b95d2

Browse files
Refactor file permission settings in init.go
- Updated file creation functions to use `utils.DefaultRW` for read/write permissions instead of hardcoded values. - Ensured consistency in permission settings across PMD and Trivy configuration file creation.
1 parent 7c209a5 commit e0b95d2

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

cmd/init.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"codacy/cli-v2/config"
55
"codacy/cli-v2/tools"
6+
"codacy/cli-v2/utils"
67
"encoding/json"
78
"errors"
89
"fmt"
@@ -168,7 +169,7 @@ func buildRepositoryConfigurationFiles(token string) error {
168169
toolsConfigDir := config.Config.ToolsConfigDirectory()
169170

170171
// Create tools-configs directory if it doesn't exist
171-
if err := os.MkdirAll(toolsConfigDir, 0777); err != nil {
172+
if err := os.MkdirAll(toolsConfigDir, utils.DefaultDirPerms); err != nil {
172173
return fmt.Errorf("failed to create tools-configs directory: %w", err)
173174
}
174175

@@ -351,13 +352,13 @@ func extractPMDConfiguration(toolConfigurations []CodacyToolConfiguration) *Coda
351352
func createPMDConfigFile(config CodacyToolConfiguration, toolsConfigDir string) error {
352353
pmdDomainConfiguration := convertAPIToolConfigurationToDomain(config)
353354
pmdConfigurationString := tools.CreatePmdConfig(pmdDomainConfiguration)
354-
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(pmdConfigurationString), 0644)
355+
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(pmdConfigurationString), utils.DefaultRW)
355356
}
356357

357358
func createDefaultPMDConfigFile(toolsConfigDir string) error {
358359
emptyConfig := tools.ToolConfiguration{}
359360
content := tools.CreatePmdConfig(emptyConfig)
360-
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(content), 0644)
361+
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(content), utils.DefaultRW)
361362
}
362363

363364
type CodacyToolConfiguration struct {
@@ -385,7 +386,7 @@ func createTrivyConfigFile(config CodacyToolConfiguration, toolsConfigDir string
385386
trivyConfigurationString := tools.CreateTrivyConfig(trivyDomainConfiguration)
386387

387388
// Write to file
388-
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(trivyConfigurationString), 0644)
389+
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(trivyConfigurationString), utils.DefaultRW)
389390
}
390391

391392
// convertAPIToolConfigurationForTrivy converts API tool configuration to domain model for Trivy
@@ -435,7 +436,7 @@ func createDefaultTrivyConfigFile(toolsConfigDir string) error {
435436
content := tools.CreateTrivyConfig(emptyConfig)
436437

437438
// Write to file
438-
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(content), 0644)
439+
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(content), utils.DefaultRW)
439440
}
440441

441442
// createDefaultEslintConfigFile creates a default eslint.config.mjs configuration file
@@ -445,5 +446,5 @@ func createDefaultEslintConfigFile(toolsConfigDir string) error {
445446
content := tools.CreateEslintConfig(emptyConfig)
446447

447448
// Write to file
448-
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), 0644)
449+
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultRW)
449450
}

utils/files.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package utils
2+
3+
const (
4+
5+
// FilePermission represents the default file permission (rw-r--r--)
6+
// This permission gives:
7+
// - read/write (rw-) permissions to the owner
8+
// - read-only (r--) permissions to the group
9+
// - read-only (r--) permissions to others
10+
DefaultRW = 0644
11+
12+
// DefaultDirPerms represents the default directory permission (rwxr-xr-x)
13+
// This permission gives:
14+
// - read/write/execute (rwx) permissions to the owner
15+
// - read/execute (r-x) permissions to the group
16+
// - read/execute (r-x) permissions to others
17+
//
18+
// Execute permission on directories is required to:
19+
// - List directory contents (ls)
20+
// - Access files within the directory (cd)
21+
// - Create/delete files in the directory
22+
// Without execute permission, users cannot traverse into or use the directory,
23+
// even if they have read/write permissions on files inside it
24+
25+
DefaultDirPerms = 0755 // For directories
26+
)

0 commit comments

Comments
 (0)