Skip to content

Commit 0e21052

Browse files
Merge 98585fc into 9739a5f
2 parents 9739a5f + 98585fc commit 0e21052

File tree

13 files changed

+214
-194
lines changed

13 files changed

+214
-194
lines changed

Formula/codacy-cli-v2.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CodacyCliV2 < Formula
2+
version "0.1.0-main.35.70f0f48"
3+
url "https://raw.githubusercontent.com/codacy/codacy-cli-v2/0.1.0-main.35.70f0f48/codacy-cli.sh"
4+
sha256 "fb616e2f5c639985566c81a6e6ce51db2e8de56bf217e837d13efe2f3ccc3042"
5+
6+
def install
7+
bin.install "codacy-cli.sh" => "codacy-cli"
8+
end
9+
end

cli-v2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package main
22

33
import (
44
"codacy/cli-v2/cmd"
5-
cfg "codacy/cli-v2/config"
5+
"codacy/cli-v2/config"
6+
cfg "codacy/cli-v2/config-file"
67
"log"
78
)
89

910
func main() {
10-
cfg.Init()
11+
config.Init()
1112

12-
runtimes, configErr := cfg.ReadConfigFile(cfg.Config.ProjectConfigFile())
13+
configErr := cfg.ReadConfigFile(config.Config.ProjectConfigFile())
1314
if configErr != nil {
1415
log.Fatal(configErr)
1516
}
16-
cfg.Config.SetRuntimes(runtimes)
1717

1818
cmd.Execute()
1919
}

cmd/analyze.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"codacy/cli-v2/config"
55
"codacy/cli-v2/tools"
6-
"fmt"
76
"log"
87
"os"
98

@@ -22,27 +21,23 @@ var analyzeCmd = &cobra.Command{
2221
Short: "Runs all linters.",
2322
Long: "Runs all tools for all runtimes.",
2423
Run: func(cmd *cobra.Command, args []string) {
25-
26-
eslintRunInfo, err := config.GetToolRunInfo("eslint")
27-
if err != nil {
28-
log.Fatal(err)
29-
}
30-
3124
workDirectory, err := os.Getwd()
3225
if err != nil {
3326
log.Fatal(err)
3427
}
3528

36-
eslintInstallationDirectory := eslintRunInfo["eslintInstallationDirectory"]
37-
nodeBinary := eslintRunInfo["nodeBinary"]
38-
3929
if len(args) == 0 {
4030
log.Fatal("You need to specify the tool you want to run for now! ;D")
4131
}
4232

43-
fmt.Printf("Running %s...\n", args[0])
33+
eslint := config.Config.Tools()["eslint"]
34+
eslintInstallationDirectory := eslint.Info()["installDir"]
35+
nodeRuntime := config.Config.Runtimes()["node"]
36+
nodeBinary := nodeRuntime.Info()["node"]
37+
38+
log.Printf("Running %s...\n", args[0])
4439
if outputFile != "" {
45-
fmt.Printf("Output will be available at %s\n", outputFile)
40+
log.Printf("Output will be available at %s\n", outputFile)
4641
}
4742

4843
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)

cmd/install.go

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package cmd
22

33
import (
44
cfg "codacy/cli-v2/config"
5-
toolutils "codacy/cli-v2/tool-utils"
6-
"codacy/cli-v2/utils"
75
"github.com/spf13/cobra"
86
"log"
9-
"os"
10-
"path/filepath"
117
)
128

139
func init() {
@@ -16,38 +12,21 @@ func init() {
1612

1713
var installCmd = &cobra.Command{
1814
Use: "install",
19-
Short: "Installs the tools specified in the project's config.",
20-
Long: "Installs all runtimes and tools specified in the project's config file.",
15+
Short: "Installs the tools specified in the project's config-file.",
16+
Long: "Installs all runtimes and tools specified in the project's config-file file.",
2117
Run: func(cmd *cobra.Command, args []string) {
2218
// install runtimes
23-
fetchRuntimes(cfg.Config.Runtimes(), cfg.Config.RuntimesDirectory())
19+
fetchRuntimes(&cfg.Config)
2420
// install tools
25-
for _, r := range cfg.Config.Runtimes() {
26-
fetchTools(r, cfg.Config.RuntimesDirectory(), cfg.Config.ToolsDirectory())
27-
}
21+
fetchTools(&cfg.Config)
2822
},
2923
}
3024

31-
func fetchRuntimes(runtimes map[string]*cfg.Runtime, runtimesDirectory string) {
32-
for _, r := range runtimes {
25+
func fetchRuntimes(config *cfg.ConfigType) {
26+
for _, r := range config.Runtimes() {
3327
switch r.Name() {
3428
case "node":
35-
// TODO should delete downloaded archive
36-
// TODO check for deflated archive
37-
log.Println("Fetching node...")
38-
downloadNodeURL := cfg.GetNodeDownloadURL(r)
39-
nodeTar, err := utils.DownloadFile(downloadNodeURL, runtimesDirectory)
40-
if err != nil {
41-
log.Fatal(err)
42-
}
43-
44-
// deflate node archive
45-
t, err := os.Open(nodeTar)
46-
defer t.Close()
47-
if err != nil {
48-
log.Fatal(err)
49-
}
50-
err = utils.ExtractTarGz(t, runtimesDirectory)
29+
err := cfg.InstallNode(r)
5130
if err != nil {
5231
log.Fatal(err)
5332
}
@@ -57,13 +36,16 @@ func fetchRuntimes(runtimes map[string]*cfg.Runtime, runtimesDirectory string) {
5736
}
5837
}
5938

60-
func fetchTools(runtime *cfg.Runtime, runtimesDirectory string, toolsDirectory string) {
61-
for _, tool := range runtime.Tools() {
39+
func fetchTools(config *cfg.ConfigType) {
40+
for _, tool := range config.Tools() {
6241
switch tool.Name() {
6342
case "eslint":
64-
npmPath := filepath.Join(runtimesDirectory, cfg.GetNodeFileName(runtime),
65-
"bin", "npm")
66-
toolutils.InstallESLint(npmPath, "eslint@" + tool.Version(), toolsDirectory)
43+
// eslint needs node runtime
44+
nodeRuntime := config.Runtimes()["node"]
45+
err := cfg.InstallEslint(nodeRuntime, tool)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
6749
default:
6850
log.Fatal("Unknown tool:", tool.Name())
6951
}
Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package config
1+
package config_file
22

33
import (
4+
"codacy/cli-v2/config"
45
"gopkg.in/yaml.v3"
56
"os"
67
)
@@ -10,42 +11,35 @@ type configFile struct {
1011
TOOLS []string
1112
}
1213

13-
func parseConfigFile(configContents []byte) (map[string]*Runtime, error) {
14+
func parseConfigFile(configContents []byte) error {
1415
configFile := configFile{}
1516
if err := yaml.Unmarshal(configContents, &configFile); err != nil {
16-
return nil, err
17+
return err
1718
}
1819

19-
runtimes := make(map[string]*Runtime)
2020
for _, rt := range configFile.RUNTIMES {
2121
ct, err := parseConfigTool(rt)
2222
if err != nil {
23-
return nil, err
24-
}
25-
runtimes[ct.name] = &Runtime{
26-
name: ct.name,
27-
version: ct.version,
23+
return err
2824
}
25+
config.Config.AddRuntime(config.NewRuntime(ct.name, ct.version))
2926
}
3027

3128
for _, tl := range configFile.TOOLS {
3229
ct, err := parseConfigTool(tl)
3330
if err != nil {
34-
return nil, err
35-
}
36-
switch ct.name {
37-
case "eslint":
38-
runtimes["node"].AddTool(ct)
31+
return err
3932
}
33+
config.Config.AddTool(config.NewRuntime(ct.name, ct.version))
4034
}
4135

42-
return runtimes, nil
36+
return nil
4337
}
4438

45-
func ReadConfigFile(configPath string) (map[string]*Runtime, error) {
39+
func ReadConfigFile(configPath string) error {
4640
content, err := os.ReadFile(configPath)
4741
if err != nil {
48-
return nil, err
42+
return err
4943
}
5044

5145
return parseConfigFile(content)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package config_file
22

33
import (
44
"errors"

config/config.go

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

9-
type configType struct {
9+
type ConfigType struct {
1010
homePath string
1111
codacyDirectory string
1212
runtimesDirectory string
@@ -15,41 +15,51 @@ type configType struct {
1515
projectConfigFile string
1616

1717
runtimes map[string]*Runtime
18+
tools map[string]*Runtime
1819
}
1920

20-
func (c *configType) HomePath() string {
21+
func (c *ConfigType) HomePath() string {
2122
return c.homePath
2223
}
2324

24-
func (c *configType) CodacyDirectory() string {
25+
func (c *ConfigType) CodacyDirectory() string {
2526
return c.codacyDirectory
2627
}
2728

28-
func (c *configType) RuntimesDirectory() string {
29+
func (c *ConfigType) RuntimesDirectory() string {
2930
return c.runtimesDirectory
3031
}
3132

32-
func (c *configType) ToolsDirectory() string {
33+
func (c *ConfigType) ToolsDirectory() string {
3334
return c.toolsDirectory
3435
}
3536

36-
func (c *configType) LocalCodacyDirectory() string {
37+
func (c *ConfigType) LocalCodacyDirectory() string {
3738
return c.localCodacyDirectory
3839
}
3940

40-
func (c *configType) ProjectConfigFile() string {
41+
func (c *ConfigType) ProjectConfigFile() string {
4142
return c.projectConfigFile
4243
}
4344

44-
func (c *configType) Runtimes() map[string]*Runtime {
45+
func (c *ConfigType) Runtimes() map[string]*Runtime {
4546
return c.runtimes
4647
}
4748

48-
func (c *configType) SetRuntimes(runtimes map[string]*Runtime) {
49-
c.runtimes = runtimes
49+
func (c *ConfigType) AddRuntime(r *Runtime) {
50+
c.runtimes[r.Name()] = r
5051
}
5152

52-
func (c *configType) initCodacyDirs() {
53+
// TODO do inheritance with tool
54+
func (c *ConfigType) Tools() map[string]*Runtime {
55+
return c.tools
56+
}
57+
58+
func (c *ConfigType) AddTool(t *Runtime) {
59+
c.tools[t.Name()] = t
60+
}
61+
62+
func (c *ConfigType) initCodacyDirs() {
5363
c.codacyDirectory = filepath.Join(c.homePath, ".cache", "codacy")
5464
err := os.MkdirAll(c.codacyDirectory, 0777)
5565
if err != nil {
@@ -84,6 +94,10 @@ func Init() {
8494
Config.homePath = homePath
8595

8696
Config.initCodacyDirs()
97+
98+
Config.runtimes = make(map[string]*Runtime)
99+
Config.tools = make(map[string]*Runtime)
87100
}
88101

89-
var Config = configType{}
102+
// Global singleton config-file
103+
var Config = ConfigType{}

config/eslint-utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os/exec"
7+
"path"
8+
)
9+
10+
func genInfoEslint(r *Runtime) map[string]string {
11+
eslintFolder := fmt.Sprintf("%s@%s", r.Name(), r.Version())
12+
installDir := path.Join(Config.ToolsDirectory(), eslintFolder)
13+
14+
return map[string]string{
15+
"installDir": installDir,
16+
"eslint": path.Join(installDir, "node_modules", ".bin", "eslint"),
17+
}
18+
}
19+
20+
/*
21+
* This installs eslint using node's npm alongside its sarif extension
22+
*/
23+
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime) error {
24+
log.Println("Installing ESLint")
25+
26+
eslintInstallArg := fmt.Sprintf("%s@%s", eslint.Name(), eslint.Version())
27+
cmd := exec.Command(nodeRuntime.Info()["npm"], "install", "--prefix", eslint.Info()["installDir"],
28+
eslintInstallArg, "@microsoft/eslint-formatter-sarif")
29+
// to use the chdir command we needed to create the folder before, we can change this after
30+
// cmd.Dir = eslintInstallationFolder
31+
stdout, err := cmd.Output()
32+
// Print the output
33+
fmt.Println(string(stdout))
34+
return err
35+
}

0 commit comments

Comments
 (0)