Skip to content

Commit

Permalink
Implement pretty printing for facts
Browse files Browse the repository at this point in the history
Co-authored-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
  • Loading branch information
dottorblaster and fabriziosestito committed Jan 26, 2023
1 parent cafd94b commit d6a0f88
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 19 deletions.
19 changes: 1 addition & 18 deletions cmd/facts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"bytes"
"encoding/json"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions pkg/factsengine/entities/fact_value.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package entities

import (
"bytes"
"encoding/json"
"fmt"
"math"
"strconv"
"strings"

"github.com/pkg/errors"
)

// nolint:gochecknoglobals
Expand Down Expand Up @@ -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
}
16 changes: 15 additions & 1 deletion pkg/factsengine/entities/facts_gathered.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package entities

import "fmt"
import (
"fmt"

"github.com/pkg/errors"
)

const (
FactsGathererdEventSource = "https://github.com/trento-project/agent"
Expand Down Expand Up @@ -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 pretty-printing 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,
Expand Down
45 changes: 45 additions & 0 deletions pkg/factsengine/entities/facts_gathered_test.go
Original file line number Diff line number Diff line change
@@ -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}")
}

0 comments on commit d6a0f88

Please sign in to comment.