Skip to content

Commit 8393ddb

Browse files
committed
adds init command
1 parent 7f2b91f commit 8393ddb

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
runtimes:
22
- node@22.2.0
33
tools:
4-
- eslint@9.3.0
4+
- eslint@9.3.0

cli-v2.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import (
44
"codacy/cli-v2/cmd"
55
"codacy/cli-v2/config"
66
cfg "codacy/cli-v2/config-file"
7-
"log"
7+
"fmt"
8+
"os"
89
)
910

1011
func main() {
1112
config.Init()
1213

1314
configErr := cfg.ReadConfigFile(config.Config.ProjectConfigFile())
14-
if configErr != nil {
15-
log.Fatal(configErr)
15+
// whenever there is no configuration file, the only command allowed to run is the 'init'
16+
if configErr != nil && os.Args[1] != "init" {
17+
fmt.Println("No configuration file was found, execute init command first.")
18+
return
1619
}
1720

1821
cmd.Execute()
19-
}
22+
}

cmd/init.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cmd
2+
3+
import (
4+
"codacy/cli-v2/config"
5+
"fmt"
6+
"github.com/spf13/cobra"
7+
"log"
8+
"os"
9+
)
10+
11+
func init() {
12+
rootCmd.AddCommand(initCmd)
13+
}
14+
15+
var initCmd = &cobra.Command{
16+
Use: "init",
17+
Short: "Bootstraps project configuration",
18+
Long: "Bootstraps project configuration, creates codacy configuration file",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
err := configurationFileSetup()
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
fmt.Println("Run install command to install dependencies.")
25+
},
26+
}
27+
28+
func configurationFileSetup() error {
29+
configFile, err := os.Open(config.Config.ProjectConfigFile())
30+
defer configFile.Close()
31+
if err != nil {
32+
fmt.Println("Codacy cli configuration file was not found in", config.Config.LocalCodacyDirectory(), "- Creating file now.")
33+
err := createConfigurationFile()
34+
if err != nil {
35+
return err
36+
}
37+
return nil
38+
} else {
39+
fmt.Println("Codacy cli configuration file was already present in ", config.Config.LocalCodacyDirectory())
40+
}
41+
42+
return nil
43+
}
44+
45+
func createConfigurationFile() error {
46+
47+
configFile, err := os.Create(config.Config.ProjectConfigFile())
48+
defer configFile.Close()
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
53+
_, err = configFile.WriteString(configFileTemplate())
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
58+
return nil
59+
}
60+
61+
func configFileTemplate() string {
62+
return `runtimes:
63+
- node@22.2.0
64+
tools:
65+
- eslint@9.3.0
66+
`
67+
}

0 commit comments

Comments
 (0)