Skip to content

Commit

Permalink
Get ESLint version from Codacy API
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed May 24, 2024
1 parent 737fcc8 commit cddae0b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .codacy/codacy.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
runtimes:
- [email protected]
tools:
- eslint@9.3.0
- eslint@8.57.0
48 changes: 23 additions & 25 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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???
Expand All @@ -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 {
Expand All @@ -45,45 +51,37 @@ 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()
if err != nil {
log.Fatal(err)
}

_, err = configFile.WriteString(configFileTemplate())
_, err = configFile.WriteString(configFileTemplate(tools))
if err != nil {
log.Fatal(err)
}

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:
- [email protected]
tools:
- eslint@9.3.0
`
- eslint@%s
`, eslintVersion)
}

func buildRepositoryConfigurationFiles(token string) error {
Expand Down
2 changes: 1 addition & 1 deletion tools/getTools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

func getTools() ([]Tool, error) {
func GetTools() ([]Tool, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
Expand Down
2 changes: 1 addition & 1 deletion tools/getTools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestGetTools(t *testing.T) {
obtained, err := getTools()
obtained, err := GetTools()

assert.Nil(t, err)

Expand Down

0 comments on commit cddae0b

Please sign in to comment.