From cddae0b0ace78e705aa963c0e28f0a30cbb2047e Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Fri, 24 May 2024 11:59:48 +0200 Subject: [PATCH] Get ESLint version from Codacy API --- .codacy/codacy.yaml | 2 +- cmd/init.go | 48 ++++++++++++++++++++---------------------- tools/getTools.go | 2 +- tools/getTools_test.go | 2 +- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/.codacy/codacy.yaml b/.codacy/codacy.yaml index efc9a77..c2cccd0 100644 --- a/.codacy/codacy.yaml +++ b/.codacy/codacy.yaml @@ -1,4 +1,4 @@ runtimes: - node@22.2.0 tools: - - eslint@9.3.0 + - eslint@8.57.0 diff --git a/cmd/init.go b/cmd/init.go index 8d83f90..1b79b78 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -5,12 +5,13 @@ import ( "codacy/cli-v2/tools" "encoding/json" "fmt" - "github.com/spf13/cobra" "io" "log" "net/http" "os" "time" + + "github.com/spf13/cobra" ) // TODO change to prod??? @@ -28,7 +29,12 @@ var initCmd = &cobra.Command{ Short: "Bootstraps project configuration", Long: "Bootstraps project configuration, creates codacy configuration file", Run: func(cmd *cobra.Command, args []string) { - err := configurationFileSetup() + apiTools, err := tools.GetTools() + if err != nil { + log.Fatal(err) + } + + err = createConfigurationFile(apiTools) if len(codacyRepositoryToken) == 0 { fmt.Println("No project token was specified, skipping fetch configurations ") } else { @@ -45,24 +51,7 @@ var initCmd = &cobra.Command{ }, } -func configurationFileSetup() error { - configFile, err := os.Open(config.Config.ProjectConfigFile()) - defer configFile.Close() - if err != nil { - fmt.Println("Codacy cli configuration file was not found in", config.Config.LocalCodacyDirectory(), "- Creating file now.") - err := createConfigurationFile() - if err != nil { - return err - } - return nil - } else { - fmt.Println("Codacy cli configuration file was already present in ", config.Config.LocalCodacyDirectory()) - } - - return nil -} - -func createConfigurationFile() error { +func createConfigurationFile(tools []tools.Tool) error { configFile, err := os.Create(config.Config.ProjectConfigFile()) defer configFile.Close() @@ -70,7 +59,7 @@ func createConfigurationFile() error { log.Fatal(err) } - _, err = configFile.WriteString(configFileTemplate()) + _, err = configFile.WriteString(configFileTemplate(tools)) if err != nil { log.Fatal(err) } @@ -78,12 +67,21 @@ func createConfigurationFile() error { return nil } -func configFileTemplate() string { - return `runtimes: +func configFileTemplate(tools []tools.Tool) string { + + var eslintVersion string + + for _, tool := range tools { + if tool.Uuid == "f8b29663-2cb2-498d-b923-a10c6a8c05cd" { + eslintVersion = tool.Version + } + } + + return fmt.Sprintf(`runtimes: - node@22.2.0 tools: - - eslint@9.3.0 -` + - eslint@%s +`, eslintVersion) } func buildRepositoryConfigurationFiles(token string) error { diff --git a/tools/getTools.go b/tools/getTools.go index 2ef4dfd..07773e0 100644 --- a/tools/getTools.go +++ b/tools/getTools.go @@ -8,7 +8,7 @@ import ( "time" ) -func getTools() ([]Tool, error) { +func GetTools() ([]Tool, error) { client := &http.Client{ Timeout: 10 * time.Second, } diff --git a/tools/getTools_test.go b/tools/getTools_test.go index 31bcb39..a1ba85c 100644 --- a/tools/getTools_test.go +++ b/tools/getTools_test.go @@ -7,7 +7,7 @@ import ( ) func TestGetTools(t *testing.T) { - obtained, err := getTools() + obtained, err := GetTools() assert.Nil(t, err)