Skip to content

Commit 1a0980c

Browse files
committed
breaking: Control the output format with the format flag
Before it would always output to SARIF if it would output to a file
1 parent 362dd17 commit 1a0980c

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

cmd/analyze.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type Pattern struct {
9696
func init() {
9797
analyzeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "output file for the results")
9898
analyzeCmd.Flags().StringVarP(&toolToAnalyze, "tool", "t", "", "Which tool to run analysis with")
99-
analyzeCmd.Flags().StringVar(&outputFormat, "format", "", "Output format (use 'sarif' for SARIF format to terminal)")
99+
analyzeCmd.Flags().StringVar(&outputFormat, "format", "", "Output format (use 'sarif' for SARIF format)")
100100
analyzeCmd.Flags().BoolVarP(&autoFix, "fix", "f", false, "Apply auto fix to your issues when available")
101101
analyzeCmd.Flags().BoolVar(&doNewPr, "new-pr", false, "Create a new PR on GitHub containing the fixed issues")
102102
rootCmd.AddCommand(analyzeCmd)
@@ -221,10 +221,12 @@ var analyzeCmd = &cobra.Command{
221221
nodeBinary := nodeRuntime.Info()["node"]
222222

223223
log.Printf("Running %s...\n", toolToAnalyze)
224+
if outputFormat == "sarif" {
225+
log.Println("Output will be in SARIF format")
226+
}
227+
224228
if outputFile != "" {
225229
log.Println("Output will be available at", outputFile)
226-
} else if outputFormat == "sarif" {
227-
log.Println("Output will be in SARIF format")
228230
}
229231

230232
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)

tools/eslintRunner.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory
1717
if autoFix {
1818
cmd.Args = append(cmd.Args, "--fix")
1919
}
20-
if outputFile != "" {
21-
//When writing to file, we write is SARIF
22-
cmd.Args = append(cmd.Args, "-f", "@microsoft/eslint-formatter-sarif", "-o", outputFile)
23-
} else if outputFormat == "sarif" {
24-
//When outputting to terminal in SARIF format
20+
if outputFormat == "sarif" {
21+
//When outputting in SARIF format
2522
cmd.Args = append(cmd.Args, "-f", "@microsoft/eslint-formatter-sarif")
2623
}
24+
25+
if outputFile != "" {
26+
//When writing to file, use the output file option
27+
cmd.Args = append(cmd.Args, "-o", outputFile)
28+
}
2729
if len(pathsToCheck) > 0 {
2830
cmd.Args = append(cmd.Args, pathsToCheck...)
2931
} else {

tools/eslintRunner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestRunEslintToFile(t *testing.T) {
2828
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy/tools/[email protected]")
2929
nodeBinary := "node"
3030

31-
RunEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, nil, false, tempResultFile, "")
31+
RunEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, nil, false, tempResultFile, "sarif")
3232

3333
expectedSarifBytes, err := os.ReadFile(expectedSarifFile)
3434
if err != nil {

0 commit comments

Comments
 (0)