Skip to content

Commit

Permalink
Only write to file when -o is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoit committed May 22, 2024
1 parent 0de2233 commit f1a1714
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
28 changes: 20 additions & 8 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"codacy/cli-v2/config"
"codacy/cli-v2/tools"
"fmt"
"github.com/spf13/cobra"
"log"
"os"
"path"

"github.com/spf13/cobra"
)

var outputFolder string
var outputFile string

func init() {
analyzeCmd.Flags().StringVarP(&outputFolder, "output", "o", path.Join(".codacy", "out"), "where to output the results")
analyzeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "output file for the results")
rootCmd.AddCommand(analyzeCmd)
}

Expand All @@ -23,8 +23,6 @@ var analyzeCmd = &cobra.Command{
Long: "Runs all tools for all runtimes.",
Run: func(cmd *cobra.Command, args []string) {

fmt.Println(outputFolder)

eslintRunInfo, err := config.GetToolRunInfo("eslint")
if err != nil {
log.Fatal(err)
Expand All @@ -42,8 +40,22 @@ var analyzeCmd = &cobra.Command{
log.Fatal("You need to specify the tool you want to run for now! ;D")
}

fmt.Printf("Running the tool %s. Output will be available at %s\n", args[0], outputFolder)
err = tools.RunEslintToFile(workDirectory, eslintInstallationDirectory, nodeBinary, outputFolder)
fmt.Printf("Running %s...\n", args[0])
if outputFile != "" {
fmt.Printf("Output will be available at %s\n", outputFile)
}

if outputFile != "" {
err = tools.RunEslintToFile(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
} else {
out, err2 := tools.RunEslintToString(workDirectory, eslintInstallationDirectory, nodeBinary)
if err2 != nil {
log.Fatal(err2)
}

fmt.Println(out)
}

if err != nil {
log.Fatal(err)
}
Expand Down
19 changes: 11 additions & 8 deletions tools/eslintRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"path/filepath"
)

func RunEslintToFile(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFolder string) error {
_, err := runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, outputFolder)
func RunEslintToFile(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFile string) error {
_, err := runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
return err
}

Expand All @@ -18,15 +18,14 @@ func RunEslintToString(repositoryToAnalyseDirectory string, eslintInstallationDi
// * Run from the root of the repo we want to analyse
// * NODE_PATH="<the installed eslint path>/node_modules"
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFolder string) (string, error) {
func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFile string) (string, error) {
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin", "eslint")

cmd := exec.Command(nodeBinary, eslintJsPath, "-f", "@microsoft/eslint-formatter-sarif")

if outputFolder != "" {
outputFile := filepath.Join(outputFolder, "eslint.sarif")
cmd.Args = append(cmd.Args, "-o", outputFile)
cmd := exec.Command(nodeBinary, eslintJsPath)
if outputFile != "" {
//When writing to file, we write is SARIF
cmd.Args = append(cmd.Args, "-f", "@microsoft/eslint-formatter-sarif", "-o", outputFile)
}

cmd.Dir = repositoryToAnalyseDirectory
Expand All @@ -38,5 +37,9 @@ func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory
// TODO eslint returns 1 when it finds errors, so we're not propagating it
out, _ := cmd.Output()

//DEBUG:
//fmt.Println(cmd.Env)
//fmt.Println(cmd)

return string(out), nil
}

0 comments on commit f1a1714

Please sign in to comment.