Skip to content

Commit 9b11b2b

Browse files
refactor: update configuration handling and directory creation
- Renamed `codacyDirectory` to `globalCacheDirectory` for clarity. - Updated methods to create necessary directories during initialization and installation. - Removed the old directory creation function in favor of a more streamlined approach.
1 parent 9a9d488 commit 9b11b2b

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

cmd/init.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ var initCmd = &cobra.Command{
2929
Run: func(cmd *cobra.Command, args []string) {
3030
// Initialize configuration without creating directories
3131
config.Init()
32-
33-
// Create necessary directories
34-
if err := config.EnsureDirectories(); err != nil {
35-
log.Fatal(err)
36-
}
32+
config.Config.CreateLocalCodacyDir()
3733

3834
if len(codacyRepositoryToken) == 0 {
3935
fmt.Println("No project token was specified, skipping fetch configurations ")

cmd/install.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cmd
22

33
import (
4+
"codacy/cli-v2/config"
45
cfg "codacy/cli-v2/config"
56
config_file "codacy/cli-v2/config-file"
67
"fmt"
8+
"io"
79
"log"
810
"os"
911
"time"
@@ -30,6 +32,11 @@ var installCmd = &cobra.Command{
3032
bold := color.New(color.Bold)
3133
green := color.New(color.FgGreen)
3234

35+
// Create necessary directories
36+
if err := config.Config.CreateCodacyDirs(); err != nil {
37+
log.Fatal(err)
38+
}
39+
3340
// Load config file
3441
if err := config_file.ReadConfigFile(cfg.Config.ProjectConfigFile()); err != nil {
3542
fmt.Println()
@@ -121,6 +128,12 @@ var installCmd = &cobra.Command{
121128
}),
122129
)
123130

131+
// Redirect all output to /dev/null during installation
132+
oldStdout := os.Stdout
133+
devNull, _ := os.Open(os.DevNull)
134+
os.Stdout = devNull
135+
log.SetOutput(io.Discard)
136+
124137
// Install runtimes first
125138
for name, runtime := range cfg.Config.Runtimes() {
126139
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
@@ -145,6 +158,11 @@ var installCmd = &cobra.Command{
145158
}
146159
}
147160

161+
// Restore output
162+
os.Stdout = oldStdout
163+
devNull.Close()
164+
log.SetOutput(os.Stderr)
165+
148166
// Print completion status
149167
fmt.Println()
150168
for name, runtime := range cfg.Config.Runtimes() {

config/config.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type ConfigType struct {
1313
homePath string
14-
codacyDirectory string
14+
globalCacheDirectory string
1515
runtimesDirectory string
1616
toolsDirectory string
1717
localCodacyDirectory string
@@ -26,7 +26,7 @@ func (c *ConfigType) HomePath() string {
2626
}
2727

2828
func (c *ConfigType) CodacyDirectory() string {
29-
return c.codacyDirectory
29+
return c.globalCacheDirectory
3030
}
3131

3232
func (c *ConfigType) RuntimesDirectory() string {
@@ -84,9 +84,9 @@ func (c *ConfigType) AddTools(configs []plugins.ToolConfig) error {
8484
}
8585

8686
func (c *ConfigType) setupCodacyPaths() {
87-
c.codacyDirectory = filepath.Join(c.homePath, ".cache", "codacy")
88-
c.runtimesDirectory = filepath.Join(c.codacyDirectory, "runtimes")
89-
c.toolsDirectory = filepath.Join(c.codacyDirectory, "tools")
87+
c.globalCacheDirectory = filepath.Join(c.homePath, ".cache", "codacy")
88+
c.runtimesDirectory = filepath.Join(c.globalCacheDirectory, "runtimes")
89+
c.toolsDirectory = filepath.Join(c.globalCacheDirectory, "tools")
9090
c.localCodacyDirectory = ".codacy"
9191

9292
yamlPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
@@ -99,8 +99,8 @@ func (c *ConfigType) setupCodacyPaths() {
9999
}
100100
}
101101

102-
func (c *ConfigType) createCodacyDirs() error {
103-
if err := os.MkdirAll(c.codacyDirectory, 0777); err != nil {
102+
func (c *ConfigType) CreateCodacyDirs() error {
103+
if err := os.MkdirAll(c.globalCacheDirectory, 0777); err != nil {
104104
return fmt.Errorf("failed to create codacy directory: %w", err)
105105
}
106106

@@ -111,11 +111,13 @@ func (c *ConfigType) createCodacyDirs() error {
111111
if err := os.MkdirAll(c.toolsDirectory, 0777); err != nil {
112112
return fmt.Errorf("failed to create tools directory: %w", err)
113113
}
114+
return nil
115+
}
114116

117+
func (c *ConfigType) CreateLocalCodacyDir() error {
115118
if err := os.MkdirAll(c.localCodacyDirectory, 0777); err != nil {
116119
return fmt.Errorf("failed to create local codacy directory: %w", err)
117120
}
118-
119121
return nil
120122
}
121123

@@ -131,11 +133,6 @@ func Init() {
131133
Config.tools = make(map[string]*plugins.ToolInfo)
132134
}
133135

134-
// EnsureDirectories creates all necessary Codacy directories if they don't exist
135-
func EnsureDirectories() error {
136-
return Config.createCodacyDirs()
137-
}
138-
139136
// IsRuntimeInstalled checks if a runtime is already installed
140137
func (c *ConfigType) IsRuntimeInstalled(name string, runtime *plugins.RuntimeInfo) bool {
141138
// If there are no binaries, check the install directory

0 commit comments

Comments
 (0)