Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate summary report & compare #65

Merged
merged 8 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions pkg/audit_compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package pkg

import (
"encoding/json"
"errors"
"fmt"
"io"
"path/filepath"
"time"

tapi "github.com/k8sdb/apimachinery/api"
"github.com/k8sdb/cli/pkg/util"
"github.com/spf13/cobra"
diff "github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)

func NewCmdAuditCompare(out io.Writer, cmdErr io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "audit_compare",
Short: "Compare audit report",
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(compareReport(cmd, out, cmdErr, args))
},
}
util.AddAuditCompareFlags(cmd)
return cmd
}

func compareReport(cmd *cobra.Command, out, errOut io.Writer, args []string) error {
if len(args) != 2 {
fmt.Fprint(errOut, "You must provide two summary report to compare.")
usageString := "Summary reports not provided."
return cmdutil.UsageError(cmd, usageString)
}

reportFile1 := args[0]
reportFile2 := args[1]

var reportData1 *tapi.Report
if err := util.ReadFileAs(reportFile1, &reportData1); err != nil {
return err
}

var reportData2 *tapi.Report
if err := util.ReadFileAs(reportFile2, &reportData2); err != nil {
return err
}

if reportData1.Kind != reportData2.Kind {
return errors.New("Unable to compare these two summary. Kind mismatch")
}

reportData1Byte, err := json.Marshal(reportData1)
if err != nil {
return err
}

reportData2Byte, err := json.Marshal(reportData2)
if err != nil {
return err
}

// Then, compare them
differ := diff.New()
d, err := differ.Compare(reportData1Byte, reportData2Byte)
if err != nil {
return err
}

var aJson map[string]interface{}
if err := json.Unmarshal(reportData1Byte, &aJson); err != nil {
return err
}

config := formatter.AsciiFormatterConfig{
ShowArrayIndex: true,
Coloring: false,
}

format := formatter.NewAsciiFormatter(aJson, config)
diffString, err := format.Format(d)
if err != nil {
return err
}

outputDirectory := cmdutil.GetFlagString(cmd, "output")
fileName := fmt.Sprintf("result-%v.txt", time.Now().UTC().Format("20060102-150405"))
path := filepath.Join(outputDirectory, fileName)

if err := util.WriteJson(path, []byte(diffString)); err != nil {
return err
}

fmt.Println(fmt.Sprintf(`Compare result has been stored to '%v'`, path))

show := cmdutil.GetFlagBool(cmd, "show")
if show {
config := formatter.AsciiFormatterConfig{
ShowArrayIndex: true,
Coloring: true,
}
format := formatter.NewAsciiFormatter(aJson, config)
diffString, err := format.Format(d)
if err != nil {
return err
}
fmt.Println()
fmt.Println(string(diffString))
}

return nil
}
Loading