From 1c89b879882e3575dbe175ff5f0e7c31975766f6 Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Tue, 24 Jan 2023 12:15:51 +0100 Subject: [PATCH 1/2] Implement pretty printing for facts Co-authored-by: Fabrizio Sestito --- cmd/facts.go | 19 +------- pkg/factsengine/entities/fact_value.go | 29 ++++++++++++ pkg/factsengine/entities/facts_gathered.go | 16 ++++++- .../entities/facts_gathered_test.go | 45 +++++++++++++++++++ 4 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 pkg/factsengine/entities/facts_gathered_test.go diff --git a/cmd/facts.go b/cmd/facts.go index 3c94575e..29eb93a7 100644 --- a/cmd/facts.go +++ b/cmd/facts.go @@ -1,9 +1,6 @@ package cmd import ( - "bytes" - "encoding/json" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -116,7 +113,7 @@ func gather(*cobra.Command, []string) { cleanupAndFatal(err) } - result, err := prettifyInterfaceToJSON(value[0]) + result, err := value[0].Prettify() if err != nil { cleanupAndFatal(err) } @@ -161,17 +158,3 @@ func list(*cobra.Command, []string) { log.Printf(g) } } - -func prettifyInterfaceToJSON(data interface{}) (string, error) { - jsonResult, err := json.Marshal(data) - if err != nil { - return "", errors.Wrap(err, "Error building the response") - } - - var prettyJSON bytes.Buffer - if err := json.Indent(&prettyJSON, jsonResult, "", " "); err != nil { - return "", errors.Wrap(err, "Error indenting the json data") - } - - return prettyJSON.String(), nil -} diff --git a/pkg/factsengine/entities/fact_value.go b/pkg/factsengine/entities/fact_value.go index efa1e32f..a346ebfc 100644 --- a/pkg/factsengine/entities/fact_value.go +++ b/pkg/factsengine/entities/fact_value.go @@ -1,10 +1,14 @@ package entities import ( + "bytes" + "encoding/json" "fmt" "math" "strconv" "strings" + + "github.com/pkg/errors" ) // nolint:gochecknoglobals @@ -192,3 +196,28 @@ func getValue(fact FactValue, values []string) (FactValue, error) { return value, nil } } + +func Prettify(fact FactValue) (string, error) { + if fact == nil { + return "()", nil + } + + interfaceValue := fact.AsInterface() + + jsonResult, err := json.Marshal(interfaceValue) + if err != nil { + return "", errors.Wrap(err, "Error building the response") + } + + var prettyfiedJSON bytes.Buffer + if err := json.Indent(&prettyfiedJSON, jsonResult, "", " "); err != nil { + return "", errors.Wrap(err, "Error indenting the json data") + } + + prettifiedJSONString := prettyfiedJSON.String() + + rhaiValue := strings.ReplaceAll(prettifiedJSONString, "{", "#{") + rhaiValue = strings.ReplaceAll(rhaiValue, "null", "()") + + return rhaiValue, nil +} diff --git a/pkg/factsengine/entities/facts_gathered.go b/pkg/factsengine/entities/facts_gathered.go index 84caad5d..a1ffc99a 100644 --- a/pkg/factsengine/entities/facts_gathered.go +++ b/pkg/factsengine/entities/facts_gathered.go @@ -1,6 +1,10 @@ package entities -import "fmt" +import ( + "fmt" + + "github.com/pkg/errors" +) const ( FactsGathererdEventSource = "https://github.com/trento-project/agent" @@ -36,6 +40,16 @@ func (e *FactGatheringError) Wrap(msg string) *FactGatheringError { } } +func (e *Fact) Prettify() (string, error) { + prettifiedValue, err := Prettify(e.Value) + if err != nil { + return "", errors.Wrap(err, "Error prettifying fact value data") + } + + result := fmt.Sprintf("Name: %s\nCheck ID: %s\n\nValue:\n\n%s", e.Name, e.CheckID, prettifiedValue) + return result, nil +} + func NewFactGatheredWithRequest(factReq FactRequest, value FactValue) Fact { return Fact{ Name: factReq.Name, diff --git a/pkg/factsengine/entities/facts_gathered_test.go b/pkg/factsengine/entities/facts_gathered_test.go new file mode 100644 index 00000000..5451da56 --- /dev/null +++ b/pkg/factsengine/entities/facts_gathered_test.go @@ -0,0 +1,45 @@ +package entities_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + "github.com/trento-project/agent/pkg/factsengine/entities" +) + +type FactsGatheredTestSuite struct { + suite.Suite +} + +func TestFactsGatheredTestSuite(t *testing.T) { + suite.Run(t, new(FactsGatheredTestSuite)) +} + +func (suite *FactsGatheredTestSuite) TestFactPrettify() { + factValueMap := &entities.FactValueMap{ + Value: map[string]entities.FactValue{ + "basic": &entities.FactValueString{Value: "basic"}, + "list": &entities.FactValueList{ + Value: []entities.FactValue{ + &entities.FactValueString{Value: "string"}, + &entities.FactValueInt{Value: 2}, + &entities.FactValueList{Value: []entities.FactValue{ + &entities.FactValueFloat{Value: 1.5}, + }}, + }}, + "map": &entities.FactValueMap{Value: map[string]entities.FactValue{ + "int": &entities.FactValueInt{Value: 5}, + }}, + }} + + fact := entities.Fact{ + Name: "fact", + CheckID: "12345", + Value: factValueMap, + Error: nil, + } + + prettyPrintedOutput, _ := fact.Prettify() + + suite.Equal(prettyPrintedOutput, "Name: fact\nCheck ID: 12345\n\nValue:\n\n#{\n \"basic\": \"basic\",\n \"list\": [\n \"string\",\n 2,\n [\n 1.5\n ]\n ],\n \"map\": #{\n \"int\": 5\n }\n}") +} From 6a893310fcb831a40ebda0aa11ea4b92eff2e6f0 Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Wed, 25 Jan 2023 16:42:34 +0100 Subject: [PATCH 2/2] Adapt dummy gatherer CI test --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a2f7eed7..21ffa421 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -106,7 +106,7 @@ jobs: ./trento-agent facts list --plugins-folder /tmp/plugins 2>&1 | grep -q "dummy" - name: run facts gather command run: | - ./trento-agent facts gather --gatherer dummy --argument 1 --plugins-folder /tmp/plugins 2>&1 | grep -q '\"Name\\\": \\\"1\\\"' + ./trento-agent facts gather --gatherer dummy --argument 1 --plugins-folder /tmp/plugins 2>&1 | grep -q 'Name: 1' build-static-binary: runs-on: ubuntu-20.04