Skip to content

Commit f1a1714

Browse files
committed
Only write to file when -o is specified
1 parent 0de2233 commit f1a1714

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

cmd/analyze.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import (
44
"codacy/cli-v2/config"
55
"codacy/cli-v2/tools"
66
"fmt"
7-
"github.com/spf13/cobra"
87
"log"
98
"os"
10-
"path"
9+
10+
"github.com/spf13/cobra"
1111
)
1212

13-
var outputFolder string
13+
var outputFile string
1414

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

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

26-
fmt.Println(outputFolder)
27-
2826
eslintRunInfo, err := config.GetToolRunInfo("eslint")
2927
if err != nil {
3028
log.Fatal(err)
@@ -42,8 +40,22 @@ var analyzeCmd = &cobra.Command{
4240
log.Fatal("You need to specify the tool you want to run for now! ;D")
4341
}
4442

45-
fmt.Printf("Running the tool %s. Output will be available at %s\n", args[0], outputFolder)
46-
err = tools.RunEslintToFile(workDirectory, eslintInstallationDirectory, nodeBinary, outputFolder)
43+
fmt.Printf("Running %s...\n", args[0])
44+
if outputFile != "" {
45+
fmt.Printf("Output will be available at %s\n", outputFile)
46+
}
47+
48+
if outputFile != "" {
49+
err = tools.RunEslintToFile(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
50+
} else {
51+
out, err2 := tools.RunEslintToString(workDirectory, eslintInstallationDirectory, nodeBinary)
52+
if err2 != nil {
53+
log.Fatal(err2)
54+
}
55+
56+
fmt.Println(out)
57+
}
58+
4759
if err != nil {
4860
log.Fatal(err)
4961
}

tools/eslintRunner.go

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

9-
func RunEslintToFile(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFolder string) error {
10-
_, err := runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, outputFolder)
9+
func RunEslintToFile(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFile string) error {
10+
_, err := runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
1111
return err
1212
}
1313

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

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

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

40+
//DEBUG:
41+
//fmt.Println(cmd.Env)
42+
//fmt.Println(cmd)
43+
4144
return string(out), nil
4245
}

0 commit comments

Comments
 (0)