Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
feat: support yaml and json output
Browse files Browse the repository at this point in the history
  • Loading branch information
dimabru committed Apr 20, 2021
1 parent d5412e9 commit babe134
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
26 changes: 25 additions & 1 deletion bl/evaluator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package bl

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/datreeio/datree/pkg/cliClient"
"github.com/datreeio/datree/pkg/printer"
"github.com/datreeio/datree/pkg/propertiesExtractor"
"gopkg.in/yaml.v3"
)

type Printer interface {
Expand Down Expand Up @@ -63,7 +65,29 @@ func (e *Evaluator) Evaluate(pattern string, cliId string, evaluationConc int) (
return results, fileErrors, nil
}

func (e *Evaluator) PrintResults(results *EvaluationResults, cliId string) error {
func (e *Evaluator) PrintResults(results *EvaluationResults, cliId string, output string) error {
if output == "json" {
jsonOutput, err := json.Marshal(results)
if err != nil {
fmt.Println(err)
return err
}

fmt.Println(string(jsonOutput))
return nil
}

if output == "yaml" {
yamlOutput, err := yaml.Marshal(results)
if err != nil {
fmt.Println(err)
return err
}

fmt.Println(string(yamlOutput))
return nil
}

warnings, err := e.parseEvaluationResultsToWarnings(results)
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion bl/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestPrintResults(t *testing.T) {
}{},
}

evaluator.PrintResults(&results, "cli_id")
evaluator.PrintResults(&results, "cli_id", "")

expectedPrinterWarnings := []printer.Warning{}
printerSpy.AssertCalled(t, "PrintWarnings", expectedPrinterWarnings)
Expand Down
24 changes: 19 additions & 5 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type LocalConfigManager interface {
}

type Evaluator interface {
PrintResults(results *bl.EvaluationResults, cliId string) error
PrintResults(results *bl.EvaluationResults, cliId string, output string) error
PrintFileParsingErrors(errors []propertiesExtractor.FileError)
Evaluate(pattern string, cliId string, evaluationConc int) (*bl.EvaluationResults, []propertiesExtractor.FileError, error)
}
Expand All @@ -27,21 +27,35 @@ type TestCommandContext struct {
Evaluator Evaluator
}

type TestCommandFlags struct {
Output string
}

func CreateTestCommand(ctx *TestCommandContext) *cobra.Command {
return &cobra.Command{
testCommand := &cobra.Command{
Use: "test",
Short: "Execute static analysis for pattern",
Long: `Execute static analysis for pattern. Input should be glob`,
RunE: func(cmd *cobra.Command, args []string) error {
return test(ctx, args[0])
outputFlag, err := cmd.Flags().GetString("output")
if err != nil {
fmt.Println(err)
return err
}

testCommandFlags := TestCommandFlags{Output: outputFlag}
return test(ctx, args[0], testCommandFlags)
},
Args: cobra.ExactValidArgs(1),
SilenceUsage: true,
SilenceErrors: true,
}

testCommand.Flags().StringP("output", "o", "", "Define output format")
return testCommand
}

func test(ctx *TestCommandContext, pattern string) error {
func test(ctx *TestCommandContext, pattern string, flags TestCommandFlags) error {
absolutePath, err := filepath.Abs(pattern)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -77,5 +91,5 @@ func test(ctx *TestCommandContext, pattern string) error {

}

return ctx.Evaluator.PrintResults(evaluationResponse, config.CliId)
return ctx.Evaluator.PrintResults(evaluationResponse, config.CliId, flags.Output)
}
10 changes: 5 additions & 5 deletions cmd/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type mockEvaluator struct {
mock.Mock
}

func (m *mockEvaluator) PrintResults(results *bl.EvaluationResults, cliId string) error {
m.Called(results, cliId)
func (m *mockEvaluator) PrintResults(results *bl.EvaluationResults, cliId string, output string) error {
m.Called(results, cliId, output)
return nil
}

Expand All @@ -47,7 +47,7 @@ func TestTestCommand(t *testing.T) {
}
evaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything).Return(mockedEvaluateResponse, []propertiesExtractor.FileError{}, nil)
evaluator.On("PrintFileParsingErrors", mock.Anything).Return()
evaluator.On("PrintResults", mock.Anything, mock.Anything).Return()
evaluator.On("PrintResults", mock.Anything, mock.Anything, mock.Anything).Return()

localConfigManager := &mockLocalConfigManager{}
localConfigManager.On("GetConfiguration").Return(localConfig.LocalConfiguration{CliId: "134kh"}, nil)
Expand All @@ -57,11 +57,11 @@ func TestTestCommand(t *testing.T) {
LocalConfig: localConfigManager,
}

test(ctx, "8/*")
test(ctx, "8/*", TestCommandFlags{})
localConfigManager.AssertCalled(t, "GetConfiguration")

expectedPath, _ := filepath.Abs("8/*")
evaluator.AssertCalled(t, "Evaluate", expectedPath, "134kh", 50)
evaluator.AssertNotCalled(t, "PrintFileParsingErrors")
evaluator.AssertCalled(t, "PrintResults", mockedEvaluateResponse, "134kh")
evaluator.AssertCalled(t, "PrintResults", mockedEvaluateResponse, "134kh", "")
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/google/uuid v1.2.0 // indirect
github.com/kyokomi/emoji v2.2.4+incompatible
github.com/lithammer/shortuuid v3.0.0+incompatible
github.com/magiconair/properties v1.8.1
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
Expand Down

0 comments on commit babe134

Please sign in to comment.