Skip to content

Commit fa346be

Browse files
refactor: change created files permission (#61)
-create reusable static for file permissons -align files and dirs creation with that permissons
1 parent 703c641 commit fa346be

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

cmd/init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
313313

314314
func createPMDConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
315315
pmdConfigurationString := tools.CreatePmdConfig(config)
316-
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(pmdConfigurationString), utils.DefaultRW)
316+
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(pmdConfigurationString), utils.DefaultFilePerms)
317317
}
318318

319319
func createDefaultPMDConfigFile(toolsConfigDir string) error {
320320
content := tools.CreatePmdConfig([]domain.PatternConfiguration{})
321-
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(content), utils.DefaultRW)
321+
return os.WriteFile(filepath.Join(toolsConfigDir, "pmd-ruleset.xml"), []byte(content), utils.DefaultFilePerms)
322322
}
323323

324324
// createTrivyConfigFile creates a trivy.yaml configuration file based on the API configuration
@@ -327,7 +327,7 @@ func createTrivyConfigFile(config []domain.PatternConfiguration, toolsConfigDir
327327
trivyConfigurationString := tools.CreateTrivyConfig(config)
328328

329329
// Write to file
330-
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(trivyConfigurationString), utils.DefaultRW)
330+
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(trivyConfigurationString), utils.DefaultFilePerms)
331331
}
332332

333333
// createDefaultTrivyConfigFile creates a default trivy.yaml configuration file
@@ -337,7 +337,7 @@ func createDefaultTrivyConfigFile(toolsConfigDir string) error {
337337
content := tools.CreateTrivyConfig(emptyConfig)
338338

339339
// Write to file
340-
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(content), utils.DefaultRW)
340+
return os.WriteFile(filepath.Join(toolsConfigDir, "trivy.yaml"), []byte(content), utils.DefaultFilePerms)
341341
}
342342

343343
// createDefaultEslintConfigFile creates a default eslint.config.mjs configuration file
@@ -347,7 +347,7 @@ func createDefaultEslintConfigFile(toolsConfigDir string) error {
347347
content := tools.CreateEslintConfig(emptyConfig)
348348

349349
// Write to file
350-
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultRW)
350+
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultFilePerms)
351351
}
352352

353353
const (

config/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88

99
"codacy/cli-v2/plugins"
10+
"codacy/cli-v2/utils"
1011
)
1112

1213
type ConfigType struct {
@@ -109,22 +110,22 @@ func (c *ConfigType) setupCodacyPaths() {
109110
}
110111

111112
func (c *ConfigType) CreateCodacyDirs() error {
112-
if err := os.MkdirAll(c.globalCacheDirectory, 0777); err != nil {
113+
if err := os.MkdirAll(c.globalCacheDirectory, utils.DefaultDirPerms); err != nil {
113114
return fmt.Errorf("failed to create codacy directory: %w", err)
114115
}
115116

116-
if err := os.MkdirAll(c.runtimesDirectory, 0777); err != nil {
117+
if err := os.MkdirAll(c.runtimesDirectory, utils.DefaultDirPerms); err != nil {
117118
return fmt.Errorf("failed to create runtimes directory: %w", err)
118119
}
119120

120-
if err := os.MkdirAll(c.toolsDirectory, 0777); err != nil {
121+
if err := os.MkdirAll(c.toolsDirectory, utils.DefaultDirPerms); err != nil {
121122
return fmt.Errorf("failed to create tools directory: %w", err)
122123
}
123124
return nil
124125
}
125126

126127
func (c *ConfigType) CreateLocalCodacyDir() error {
127-
if err := os.MkdirAll(c.localCodacyDirectory, 0777); err != nil {
128+
if err := os.MkdirAll(c.localCodacyDirectory, utils.DefaultDirPerms); err != nil {
128129
return fmt.Errorf("failed to create local codacy directory: %w", err)
129130
}
130131
return nil

config/runtimes-installer_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"codacy/cli-v2/plugins"
5+
"codacy/cli-v2/utils"
56
"os"
67
"path/filepath"
78
"testing"
@@ -26,7 +27,7 @@ func TestIsRuntimeInstalled(t *testing.T) {
2627
assert.False(t, isRuntimeInstalled(runtimeInfoNoBinaries))
2728

2829
// Create the install directory
29-
err = os.MkdirAll(runtimeInfoNoBinaries.InstallDir, 0755)
30+
err = os.MkdirAll(runtimeInfoNoBinaries.InstallDir, utils.DefaultDirPerms)
3031
assert.NoError(t, err)
3132

3233
// Test when the install directory exists
@@ -52,4 +53,4 @@ func TestIsRuntimeInstalled(t *testing.T) {
5253

5354
// Test when the binary exists
5455
assert.True(t, isRuntimeInstalled(runtimeInfoWithBinaries))
55-
}
56+
}

utils/extract.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func ExtractTarGz(archive *os.File, targetDir string) error {
2525
switch f.IsDir() {
2626
case true:
2727
// create a directory
28-
err := os.MkdirAll(path, 0777)
28+
err := os.MkdirAll(path, DefaultDirPerms)
2929
if err != nil {
3030
return err
3131
}
@@ -37,9 +37,8 @@ func ExtractTarGz(archive *os.File, targetDir string) error {
3737
return nil
3838
}
3939

40-
4140
// Ensure parent directory exists
42-
err := os.MkdirAll(filepath.Dir(path), 0777)
41+
err := os.MkdirAll(filepath.Dir(path), DefaultDirPerms)
4342
if err != nil {
4443
return err
4544
}
@@ -74,7 +73,7 @@ func ExtractTarGz(archive *os.File, targetDir string) error {
7473
os.Remove(path)
7574

7675
// Ensure parent directory exists
77-
err := os.MkdirAll(filepath.Dir(path), 0777)
76+
err := os.MkdirAll(filepath.Dir(path), DefaultDirPerms)
7877
if err != nil {
7978
return err
8079
}
@@ -99,7 +98,7 @@ func ExtractZip(zipPath string, targetDir string) error {
9998
switch f.IsDir() {
10099
case true:
101100
// create a directory
102-
err := os.MkdirAll(path, 0777)
101+
err := os.MkdirAll(path, DefaultDirPerms)
103102
if err != nil {
104103
return err
105104
}
@@ -116,7 +115,7 @@ func ExtractZip(zipPath string, targetDir string) error {
116115
}
117116

118117
// ensure parent directory exists
119-
err := os.MkdirAll(filepath.Dir(path), 0777)
118+
err := os.MkdirAll(filepath.Dir(path), DefaultDirPerms)
120119
if err != nil {
121120
return err
122121
}

utils/files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const (
77
// - read/write (rw-) permissions to the owner
88
// - read-only (r--) permissions to the group
99
// - read-only (r--) permissions to others
10-
DefaultRW = 0644
10+
DefaultFilePerms = 0644
1111

1212
// DefaultDirPerms represents the default directory permission (rwxr-xr-x)
1313
// This permission gives:

0 commit comments

Comments
 (0)