Skip to content

Commit d99271c

Browse files
refactor: make config.init not create files
1 parent 8aed5d8 commit d99271c

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ runtimes:
44
tools:
55
66
7-
87
8+

cmd/init.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717

1818
const CodacyApiBase = "https://app.codacy.com"
1919

20-
var codacyRepositoryToken string
21-
2220
func init() {
2321
initCmd.Flags().StringVar(&codacyRepositoryToken, "repository-token", "", "optional codacy repository token, if defined configurations will be fetched from codacy")
2422
rootCmd.AddCommand(initCmd)
@@ -29,9 +27,14 @@ var initCmd = &cobra.Command{
2927
Short: "Bootstraps project configuration",
3028
Long: "Bootstraps project configuration, creates codacy configuration file",
3129
Run: func(cmd *cobra.Command, args []string) {
32-
30+
// Initialize configuration without creating directories
3331
config.Init()
3432

33+
// Create necessary directories
34+
if err := config.EnsureDirectories(); err != nil {
35+
log.Fatal(err)
36+
}
37+
3538
if len(codacyRepositoryToken) == 0 {
3639
fmt.Println("No project token was specified, skipping fetch configurations ")
3740
noTools := []tools.Tool{}
@@ -48,10 +51,6 @@ var initCmd = &cobra.Command{
4851
if err != nil {
4952
log.Fatal(err)
5053
}
51-
err = buildRepositoryConfigurationFiles(codacyRepositoryToken)
52-
if err != nil {
53-
log.Fatal(err)
54-
}
5554
}
5655
fmt.Println("Run install command to install dependencies.")
5756
},
@@ -79,7 +78,7 @@ func configFileTemplate(tools []tools.Tool) string {
7978
eslintVersion := "9.3.0"
8079
trivyVersion := "0.59.1" // Latest stable version
8180
pylintVersion := "3.3.6"
82-
81+
pmdVersion := "7.12.0"
8382
for _, tool := range tools {
8483
if tool.Uuid == "f8b29663-2cb2-498d-b923-a10c6a8c05cd" {
8584
eslintVersion = tool.Version
@@ -99,7 +98,8 @@ tools:
9998
- eslint@%s
10099
- trivy@%s
101100
- pylint@%s
102-
`, eslintVersion, trivyVersion, pylintVersion)
101+
- pmd@%s
102+
`, eslintVersion, trivyVersion, pylintVersion, pmdVersion)
103103
}
104104

105105
func buildRepositoryConfigurationFiles(token string) error {

cmd/install.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
cfg "codacy/cli-v2/config"
55
config_file "codacy/cli-v2/config-file"
66
"fmt"
7-
"io"
87
"log"
98
"os"
109
"time"
@@ -15,9 +14,11 @@ import (
1514
)
1615

1716
var registry string
17+
var codacyRepositoryToken string
1818

1919
func init() {
2020
installCmd.Flags().StringVarP(&registry, "registry", "r", "", "Registry to use for installing tools")
21+
installCmd.Flags().StringVar(&codacyRepositoryToken, "repository-token", "", "Codacy repository token for fetching tool configurations")
2122
rootCmd.AddCommand(installCmd)
2223
}
2324

@@ -29,12 +30,16 @@ var installCmd = &cobra.Command{
2930
bold := color.New(color.Bold)
3031
green := color.New(color.FgGreen)
3132

32-
// Initialize config
33+
// Initialize config global object
3334
cfg.Init()
3435

3536
// Load config file
3637
if err := config_file.ReadConfigFile(cfg.Config.ProjectConfigFile()); err != nil {
37-
log.Fatalf("Failed to load config file: %v", err)
38+
fmt.Println()
39+
color.Red("⚠️ Warning: Could not find configuration file!")
40+
fmt.Println("Please run 'codacy-cli init' first to create a configuration file.")
41+
fmt.Println()
42+
os.Exit(1)
3843
}
3944

4045
// Check if anything needs to be installed
@@ -119,13 +124,7 @@ var installCmd = &cobra.Command{
119124
}),
120125
)
121126

122-
// Redirect all output to /dev/null during installation
123-
oldStdout := os.Stdout
124-
devNull, _ := os.Open(os.DevNull)
125-
os.Stdout = devNull
126-
log.SetOutput(io.Discard)
127-
128-
// Install runtimes
127+
// Install runtimes first
129128
for name, runtime := range cfg.Config.Runtimes() {
130129
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
131130
progressBar.Describe(fmt.Sprintf("Installing runtime: %s v%s...", name, runtime.Version))
@@ -149,11 +148,6 @@ var installCmd = &cobra.Command{
149148
}
150149
}
151150

152-
// Restore output
153-
os.Stdout = oldStdout
154-
devNull.Close()
155-
log.SetOutput(os.Stderr)
156-
157151
// Print completion status
158152
fmt.Println()
159153
for name, runtime := range cfg.Config.Runtimes() {

config/config.go

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

33
import (
4+
"fmt"
45
"log"
56
"os"
67
"path/filepath"
@@ -82,30 +83,11 @@ func (c *ConfigType) AddTools(configs []plugins.ToolConfig) error {
8283
return nil
8384
}
8485

85-
func (c *ConfigType) initCodacyDirs() {
86+
func (c *ConfigType) setupCodacyPaths() {
8687
c.codacyDirectory = filepath.Join(c.homePath, ".cache", "codacy")
87-
err := os.MkdirAll(c.codacyDirectory, 0777)
88-
if err != nil {
89-
log.Fatal(err)
90-
}
91-
9288
c.runtimesDirectory = filepath.Join(c.codacyDirectory, "runtimes")
93-
err = os.MkdirAll(c.runtimesDirectory, 0777)
94-
if err != nil {
95-
log.Fatal(err)
96-
}
97-
9889
c.toolsDirectory = filepath.Join(c.codacyDirectory, "tools")
99-
err = os.MkdirAll(c.toolsDirectory, 0777)
100-
if err != nil {
101-
log.Fatal(err)
102-
}
103-
10490
c.localCodacyDirectory = ".codacy"
105-
err = os.MkdirAll(c.localCodacyDirectory, 0777)
106-
if err != nil {
107-
log.Fatal(err)
108-
}
10991

11092
yamlPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
11193
ymlPath := filepath.Join(c.localCodacyDirectory, "codacy.yml")
@@ -117,19 +99,43 @@ func (c *ConfigType) initCodacyDirs() {
11799
}
118100
}
119101

102+
func (c *ConfigType) createCodacyDirs() error {
103+
if err := os.MkdirAll(c.codacyDirectory, 0777); err != nil {
104+
return fmt.Errorf("failed to create codacy directory: %w", err)
105+
}
106+
107+
if err := os.MkdirAll(c.runtimesDirectory, 0777); err != nil {
108+
return fmt.Errorf("failed to create runtimes directory: %w", err)
109+
}
110+
111+
if err := os.MkdirAll(c.toolsDirectory, 0777); err != nil {
112+
return fmt.Errorf("failed to create tools directory: %w", err)
113+
}
114+
115+
if err := os.MkdirAll(c.localCodacyDirectory, 0777); err != nil {
116+
return fmt.Errorf("failed to create local codacy directory: %w", err)
117+
}
118+
119+
return nil
120+
}
121+
120122
func Init() {
121123
homePath, err := os.UserHomeDir()
122124
if err != nil {
123125
log.Fatal(err)
124126
}
125127
Config.homePath = homePath
126128

127-
Config.initCodacyDirs()
128-
129+
Config.setupCodacyPaths()
129130
Config.runtimes = make(map[string]*plugins.RuntimeInfo)
130131
Config.tools = make(map[string]*plugins.ToolInfo)
131132
}
132133

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

0 commit comments

Comments
 (0)