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

Add --output flag to support JSON #21

Merged
merged 1 commit into from
Jan 17, 2023
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
51 changes: 43 additions & 8 deletions cli/agnosticv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -33,6 +34,7 @@ var rootFlag string
var validateFlag bool
var versionFlag bool
var gitFlag bool
var outputFlag string

// Build info
var Version = "development"
Expand Down Expand Up @@ -96,6 +98,7 @@ By default, it's empty, and the scope of the git repository is used, so you shou
need this parameter unless your files are not in a git repository, or if you want to use a subdir. Use -root flag with -merge.`)
flags.BoolVar(&versionFlag, "version", false, "Print build version.")
flags.BoolVar(&gitFlag, "git", true, "Perform git operations to gather and inject information into the merged vars like 'last_update'. Git operations are slow so this option is automatically disabled for listing.")
flags.StringVar(&outputFlag, "output", "", "Output format. Possible values: json or yaml. Default is 'yaml' for merging.")

if err := flags.Parse(args[1:]); err != nil {
flags.PrintDefaults()
Expand Down Expand Up @@ -134,6 +137,11 @@ need this parameter unless your files are not in a git repository, or if you wan
return controlFlow{true, 2}
}

if mergeFlag != "" && outputFlag == "" {
// Set to YAML by default when merging
outputFlag = "yaml"
}

if rootFlag != "" {
if !fileExists(rootFlag) {
log.Fatalf("File %s does not exist", rootFlag)
Expand Down Expand Up @@ -168,6 +176,13 @@ need this parameter unless your files are not in a git repository, or if you wan
logDebug = log.New(os.Stdout, "(d) ", log.LstdFlags)
}

switch outputFlag {
case "yaml", "json", "":
default:
fmt.Fprintln(output, "Unsupported format for output: ", outputFlag)
return controlFlow{true, 2}
}

return controlFlow{false, 0}
}

Expand Down Expand Up @@ -576,8 +591,20 @@ func main() {
logErr.Printf("error walking the path %q: %v\n", ".", err)
return
}
for _, ci := range catalogItems {
fmt.Println(ci)

switch outputFlag {
case "yaml":
out, _ := yaml.Marshal(catalogItems)
fmt.Printf("%s", out)

case "json":
out, _ := json.Marshal(catalogItems)
fmt.Printf("%s", out)

default:
for _, ci := range catalogItems {
fmt.Println(ci)
}
}
}

Expand All @@ -593,11 +620,19 @@ func main() {
}
}

out, _ := yaml.Marshal(merged)

fmt.Printf("---\n")
printMergeStrategies()
printPaths(mergeList, workDir)
fmt.Printf("%s", out)
switch outputFlag {
case "json":
out, _ := json.Marshal(merged)
fmt.Printf("%s", out)
case "yaml":
out, _ := yaml.Marshal(merged)

fmt.Printf("---\n")
printMergeStrategies()
printPaths(mergeList, workDir)
fmt.Printf("%s", out)
default:
logErr.Fatal("Unsupported format for output:", outputFlag)
}
}
}
32 changes: 32 additions & 0 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ func TestFlag(t *testing.T) {
description: "-merge and -list both provided",
result: controlFlow{true, 2},
},
{
args: []string{"agnosticv", "--list",
"--output", "json"},
description: "-output and -list, json output",
result: controlFlow{false, 0},
},
{
args: []string{"agnosticv", "--list",
"--output", "yaml"},
description: "-output and -list, yaml output ",
result: controlFlow{false, 0},
},
{
args: []string{"agnosticv", "--list",
"--output", "unknown"},
description: "-output and -list, wrong output",
result: controlFlow{true, 2},
},
{
args: []string{"agnosticv",
"--merge", "fixtures/test/BABYLON_EMPTY_CONFIG/dev.yaml",
"--output", "unknown"},
description: "-output and -merge, wrong output",
result: controlFlow{true, 2},
},
{
args: []string{"agnosticv",
"--merge", "fixtures/test/BABYLON_EMPTY_CONFIG/dev.yaml",
"--output", "yaml"},
description: "-output and -merge, yaml output",
result: controlFlow{false, 0},
},
}

for _, tc := range testCases {
Expand Down
7 changes: 7 additions & 0 deletions tools/test_new_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ for ci in $($cli1 --list); do
fi

echo YES

echo -n "testing merge $ci JSON ......................."
if ! $cli2 --merge $ci --output json | jq . > /dev/null; then
echo NO
else
echo YES
fi
done

for fil in $(find -name common.yaml) $(find -name account.yaml) $(find includes -type f); do
Expand Down