Skip to content

Commit 8d3bece

Browse files
refactor: Consolidate file permission constants and eliminate hardcoded values (#158)
- Remove duplication between utils/files.go and constants/permissions.go - Delete utils/files.go and standardize on constants package for permissions - Update 14 files to use constants.DefaultFilePerms and constants.DefaultDirPerms - Replace hardcoded 0644 and 0755 values with named constants in tool runners - Improve code maintainability with single source of truth for file permissions
1 parent 1c23a76 commit 8d3bece

File tree

15 files changed

+39
-61
lines changed

15 files changed

+39
-61
lines changed

cmd/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ Supports API token, provider, and repository flags to automatically fetch tool c
530530

531531
if outputFile != "" {
532532
// Write filtered SARIF to output file
533-
os.WriteFile(outputFile, filteredData, utils.DefaultFilePerms)
533+
os.WriteFile(outputFile, filteredData, constants.DefaultFilePerms)
534534
} else {
535535
// Print the filtered SARIF output
536536
fmt.Println(string(filteredData))

cmd/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"codacy/cli-v2/cmd/configsetup"
1313
codacyclient "codacy/cli-v2/codacy-client"
1414
"codacy/cli-v2/config"
15+
"codacy/cli-v2/constants"
1516
"codacy/cli-v2/domain"
1617
"codacy/cli-v2/plugins"
1718
"codacy/cli-v2/tools"
18-
"codacy/cli-v2/utils"
1919
"codacy/cli-v2/utils/logger"
2020

2121
"github.com/sirupsen/logrus"
@@ -101,7 +101,7 @@ func runConfigResetLogic(cmd *cobra.Command, args []string, flags domain.InitFla
101101

102102
// Create .codacy/tools-configs directory
103103
toolsConfigDir := config.Config.ToolsConfigDirectory()
104-
if err := os.MkdirAll(toolsConfigDir, utils.DefaultDirPerms); err != nil {
104+
if err := os.MkdirAll(toolsConfigDir, constants.DefaultDirPerms); err != nil {
105105
log.Fatalf("Failed to create tools-configs directory: %v", err)
106106
}
107107

@@ -267,10 +267,10 @@ func updateLanguagesConfigForTools(detectedTools map[string]struct{}, toolsConfi
267267
if err != nil {
268268
return fmt.Errorf("failed to marshal languages-config.yaml: %w", err)
269269
}
270-
if err := os.MkdirAll(toolsConfigDir, utils.DefaultDirPerms); err != nil {
270+
if err := os.MkdirAll(toolsConfigDir, constants.DefaultDirPerms); err != nil {
271271
return fmt.Errorf("failed to create tools-configs directory: %w", err)
272272
}
273-
return os.WriteFile(langConfigPath, data, utils.DefaultFilePerms)
273+
return os.WriteFile(langConfigPath, data, constants.DefaultFilePerms)
274274
}
275275

276276
// updateCodacyYAMLForTools updates the codacy.yaml file with detected tools.
@@ -430,11 +430,11 @@ func updateCodacyYAMLForTools(detectedTools map[string]struct{}, codacyYAMLPath
430430
return fmt.Errorf("error marshaling %s: %w", codacyYAMLPath, err)
431431
}
432432
// Ensure directory exists
433-
if err := os.MkdirAll(filepath.Dir(codacyYAMLPath), utils.DefaultDirPerms); err != nil {
433+
if err := os.MkdirAll(filepath.Dir(codacyYAMLPath), constants.DefaultDirPerms); err != nil {
434434
return fmt.Errorf("error creating .codacy directory: %w", err)
435435
}
436436

437-
return os.WriteFile(codacyYAMLPath, yamlData, utils.DefaultFilePerms)
437+
return os.WriteFile(codacyYAMLPath, yamlData, constants.DefaultFilePerms)
438438
}
439439

440440
func init() {

cmd/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"path/filepath"
66
"testing"
77

8+
"codacy/cli-v2/constants"
89
"codacy/cli-v2/domain"
9-
"codacy/cli-v2/utils"
1010

1111
"github.com/stretchr/testify/assert"
1212
"gopkg.in/yaml.v3"
@@ -135,7 +135,7 @@ func TestUpdateCodacyYAMLForTools_NewFile(t *testing.T) {
135135
}
136136
yamlData, err := yaml.Marshal(initialConfig)
137137
assert.NoError(t, err)
138-
err = os.WriteFile(codacyYAMLPath, yamlData, utils.DefaultFilePerms)
138+
err = os.WriteFile(codacyYAMLPath, yamlData, constants.DefaultFilePerms)
139139
assert.NoError(t, err)
140140

141141
// Mock detected tools

cmd/configsetup/setup.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"codacy/cli-v2/tools/lizard"
1818
"codacy/cli-v2/tools/pylint"
1919
reviveTool "codacy/cli-v2/tools/revive"
20-
"codacy/cli-v2/utils"
2120

2221
"gopkg.in/yaml.v3"
2322
)
@@ -549,7 +548,7 @@ func CleanConfigDirectory(toolsConfigDir string) error {
549548

550549
func createReviveConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
551550
reviveConfigurationString := reviveTool.GenerateReviveConfig(config)
552-
return os.WriteFile(filepath.Join(toolsConfigDir, "revive.toml"), []byte(reviveConfigurationString), utils.DefaultFilePerms)
551+
return os.WriteFile(filepath.Join(toolsConfigDir, "revive.toml"), []byte(reviveConfigurationString), constants.DefaultFilePerms)
553552
}
554553

555554
// BuildDefaultConfigurationFiles creates default configuration files for all tools

cmd/init_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package cmd
33
import (
44
"codacy/cli-v2/cmd/configsetup"
55
"codacy/cli-v2/config"
6+
"codacy/cli-v2/constants"
67
"codacy/cli-v2/domain"
7-
"codacy/cli-v2/utils"
88
"os"
99
"path/filepath"
1010
"testing"
@@ -164,7 +164,7 @@ func TestCleanConfigDirectory(t *testing.T) {
164164

165165
for _, file := range testFiles {
166166
filePath := filepath.Join(tempDir, file)
167-
err := os.WriteFile(filePath, []byte("test content"), utils.DefaultFilePerms)
167+
err := os.WriteFile(filePath, []byte("test content"), constants.DefaultFilePerms)
168168
assert.NoError(t, err, "Failed to create test file: %s", filePath)
169169
}
170170

@@ -213,7 +213,7 @@ func TestInitCommand_NoToken(t *testing.T) {
213213
}
214214

215215
toolsConfigDir := config.Config.ToolsConfigDirectory()
216-
if err := os.MkdirAll(toolsConfigDir, utils.DefaultDirPerms); err != nil {
216+
if err := os.MkdirAll(toolsConfigDir, constants.DefaultDirPerms); err != nil {
217217
t.Fatalf("Failed to create tools-configs directory: %v", err)
218218
}
219219

config/config.go

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

10+
"codacy/cli-v2/constants"
1011
"codacy/cli-v2/plugins"
11-
"codacy/cli-v2/utils"
1212

1313
"gopkg.in/yaml.v3" // Added import for YAML parsing
1414
)
@@ -115,11 +115,11 @@ func (c *ConfigType) writeConfig(codacyPath string, config map[string]interface{
115115
}
116116

117117
// Ensure directory exists
118-
if err := os.MkdirAll(filepath.Dir(codacyPath), utils.DefaultDirPerms); err != nil {
118+
if err := os.MkdirAll(filepath.Dir(codacyPath), constants.DefaultDirPerms); err != nil {
119119
return fmt.Errorf("error creating .codacy directory: %w", err)
120120
}
121121

122-
if err := os.WriteFile(codacyPath, yamlData, utils.DefaultFilePerms); err != nil {
122+
if err := os.WriteFile(codacyPath, yamlData, constants.DefaultFilePerms); err != nil {
123123
return fmt.Errorf("error writing codacy.yaml: %w", err)
124124
}
125125

@@ -325,22 +325,22 @@ func setupGlobalConfig(repositoryDirectory string, repositoryCache string, globa
325325
}
326326

327327
func (c *ConfigType) CreateCodacyDirs() error {
328-
if err := os.MkdirAll(c.globalCacheDirectory, utils.DefaultDirPerms); err != nil {
328+
if err := os.MkdirAll(c.globalCacheDirectory, constants.DefaultDirPerms); err != nil {
329329
return fmt.Errorf("failed to create codacy directory: %w", err)
330330
}
331331

332-
if err := os.MkdirAll(c.runtimesDirectory, utils.DefaultDirPerms); err != nil {
332+
if err := os.MkdirAll(c.runtimesDirectory, constants.DefaultDirPerms); err != nil {
333333
return fmt.Errorf("failed to create runtimes directory: %w", err)
334334
}
335335

336-
if err := os.MkdirAll(c.toolsDirectory, utils.DefaultDirPerms); err != nil {
336+
if err := os.MkdirAll(c.toolsDirectory, constants.DefaultDirPerms); err != nil {
337337
return fmt.Errorf("failed to create tools directory: %w", err)
338338
}
339339
return nil
340340
}
341341

342342
func (c *ConfigType) CreateLocalCodacyDir() error {
343-
if err := os.MkdirAll(c.localCodacyDirectory, utils.DefaultDirPerms); err != nil {
343+
if err := os.MkdirAll(c.localCodacyDirectory, constants.DefaultDirPerms); err != nil {
344344
return fmt.Errorf("failed to create local codacy directory: %w", err)
345345
}
346346
return nil

config/runtimes-installer.go

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

33
import (
4+
"codacy/cli-v2/constants"
45
"codacy/cli-v2/plugins"
56
"codacy/cli-v2/utils"
67
"codacy/cli-v2/utils/logger"
@@ -78,7 +79,7 @@ func InstallRuntime(name string, runtimeInfo *plugins.RuntimeInfo) error {
7879
func downloadAndExtractRuntime(runtimeInfo *plugins.RuntimeInfo) error {
7980
// Ensure the runtimes directory exists
8081
runtimesDir := Config.RuntimesDirectory()
81-
if err := os.MkdirAll(runtimesDir, utils.DefaultDirPerms); err != nil {
82+
if err := os.MkdirAll(runtimesDir, constants.DefaultDirPerms); err != nil {
8283
return fmt.Errorf("failed to create runtimes directory: %w", err)
8384
}
8485

@@ -137,7 +138,7 @@ func downloadAndExtractRuntime(runtimeInfo *plugins.RuntimeInfo) error {
137138

138139
// Ensure binaries have executable permissions
139140
for binaryName, fullPath := range runtimeInfo.Binaries {
140-
if err := os.Chmod(fullPath, utils.DefaultDirPerms); err != nil {
141+
if err := os.Chmod(fullPath, constants.DefaultDirPerms); err != nil {
141142
logger.Debug("Failed to set binary permissions", logrus.Fields{
142143
"binary": binaryName,
143144
"path": fullPath,

config/runtimes-installer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package config
22

33
import (
4+
"codacy/cli-v2/constants"
45
"codacy/cli-v2/plugins"
5-
"codacy/cli-v2/utils"
66
"os"
77
"path/filepath"
88
"testing"
@@ -27,7 +27,7 @@ func TestIsRuntimeInstalled(t *testing.T) {
2727
assert.False(t, Config.IsRuntimeInstalled("test-runtime", runtimeInfoNoBinaries))
2828

2929
// Create the install directory
30-
err = os.MkdirAll(runtimeInfoNoBinaries.InstallDir, utils.DefaultDirPerms)
30+
err = os.MkdirAll(runtimeInfoNoBinaries.InstallDir, constants.DefaultDirPerms)
3131
assert.NoError(t, err)
3232

3333
// Test when the install directory exists

config/tools-installer.go

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

33
import (
44
"bytes"
5+
"codacy/cli-v2/constants"
56
"codacy/cli-v2/plugins"
67
"codacy/cli-v2/utils"
78
"codacy/cli-v2/utils/logger"
@@ -102,7 +103,7 @@ func InstallTool(name string, toolInfo *plugins.ToolInfo, registry string) error
102103
}
103104

104105
// Make sure the installation directory exists
105-
err := os.MkdirAll(toolInfo.InstallDir, 0755)
106+
err := os.MkdirAll(toolInfo.InstallDir, constants.DefaultDirPerms)
106107
if err != nil {
107108
return fmt.Errorf("failed to create installation directory: %w", err)
108109
}
@@ -291,7 +292,7 @@ func installDownloadBasedTool(toolInfo *plugins.ToolInfo) error {
291292
defer file.Close()
292293

293294
// Create the installation directory
294-
err = os.MkdirAll(toolInfo.InstallDir, 0755)
295+
err = os.MkdirAll(toolInfo.InstallDir, constants.DefaultDirPerms)
295296
if err != nil {
296297
return fmt.Errorf("failed to create installation directory: %w", err)
297298
}
@@ -316,7 +317,7 @@ func installDownloadBasedTool(toolInfo *plugins.ToolInfo) error {
316317

317318
// Make sure all binaries are executable
318319
for _, binaryPath := range toolInfo.Binaries {
319-
err = os.Chmod(filepath.Join(toolInfo.InstallDir, filepath.Base(binaryPath)), 0755)
320+
err = os.Chmod(filepath.Join(toolInfo.InstallDir, filepath.Base(binaryPath)), constants.DefaultDirPerms)
320321
if err != nil && !os.IsNotExist(err) {
321322
return fmt.Errorf("failed to make binary executable: %w", err)
322323
}

tools/dartanalyzerRunner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tools
33
import (
44
"bufio"
55
"bytes"
6+
"codacy/cli-v2/constants"
67
"encoding/json"
78
"fmt"
89
"log"
@@ -121,7 +122,7 @@ func RunDartAnalyzer(workDirectory string, installationDirectory string, binary
121122
// Write SARIF output to file if specified
122123
if outputFile != "" {
123124
sarifJson, _ := json.MarshalIndent(sarif, "", " ")
124-
err := os.WriteFile(outputFile, sarifJson, 0644)
125+
err := os.WriteFile(outputFile, sarifJson, constants.DefaultFilePerms)
125126
if err != nil {
126127
fmt.Fprintf(os.Stderr, "Error writing SARIF output: %v\n", err)
127128
}

0 commit comments

Comments
 (0)