Skip to content

Commit

Permalink
feat: add support for exporting to json from run
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Oct 22, 2023
1 parent c7cb670 commit 7b12271
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
41 changes: 39 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"log"
"os"
Expand All @@ -22,7 +23,7 @@ import (
)

var outputFile, dataFile, runNamespace string
var junit, csv bool
var junit, csv, jsonExport bool

var Run = &cobra.Command{
Use: "run <canary.yaml>",
Expand Down Expand Up @@ -112,6 +113,21 @@ var Run = &cobra.Command{
logger.Fatalf("error writing output file: %v", err)
}
}
if jsonExport {

for _, result := range results {
result.Name = def(result.Name, result.Check.GetName(), result.Canary.Name)
result.Description = def(result.Description, result.Check.GetDescription())
result.Labels = merge(result.Check.GetLabels(), result.Labels)
fmt.Println(result.Name)
}

data, err := json.Marshal(results)
if err != nil {
logger.Fatalf("Failed to marshall json: %s", err)
}
output.HandleOutput(string(data), outputFile)
}

logger.Infof("%d passed, %d failed in %s", passed, failed, timer)

Expand All @@ -121,11 +137,32 @@ var Run = &cobra.Command{
},
}

func merge(m1, m2 map[string]string) map[string]string {
out := make(map[string]string)
for k, v := range m1 {
out[k] = v
}
for k, v := range m2 {
out[k] = v
}
return out
}

func def(a ...string) string {
for _, s := range a {
if s != "" {
return s
}
}
return ""
}

func init() {
Run.PersistentFlags().StringVarP(&dataFile, "data", "d", "", "Template out each spec using the JSON or YAML data in this file")
Run.PersistentFlags().StringVarP(&outputFile, "output-file", "o", "", "file to output the results in")
Run.Flags().StringVarP(&runNamespace, "namespace", "n", "", "Namespace to run canary checks in")
Run.Flags().BoolVarP(&junit, "junit", "j", false, "output results in junit format")
Run.Flags().BoolVar(&junit, "junit", false, "output results in junit format")
Run.Flags().BoolVarP(&jsonExport, "json", "j", false, "output results in json format")
Run.Flags().BoolVar(&csv, "csv", false, "output results in csv format")
}

Expand Down
30 changes: 16 additions & 14 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,21 +401,23 @@ type URL struct {

type SystemResult struct{}
type CheckResult struct {
Start time.Time
Pass bool
Invalid bool
Detail interface{}
Data map[string]interface{}
Duration int64
Description string
DisplayType string
Message string
Error string
Metrics []Metric
Transformed bool
Name string `json:"name,omitempty"`
Start time.Time `json:"start,omitempty"`
Pass bool `json:"pass,omitempty"`
Invalid bool `json:"invalid,omitempty"`
Detail interface{} `json:"detail,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Duration int64 `json:"duration,omitempty"`
Description string `json:"description,omitempty"`
DisplayType string `json:"display_type,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Metrics []Metric `json:"metrics,omitempty"`
Transformed bool `json:"transformed,omitempty"`
// Check is the configuration
Check external.Check
Canary v1.Canary
Check external.Check `json:"-"`
Canary v1.Canary `json:"-"`
}

func (result CheckResult) GetDescription() string {
Expand Down

0 comments on commit 7b12271

Please sign in to comment.