Skip to content

Commit

Permalink
Merge pull request #16 from blendle/json
Browse files Browse the repository at this point in the history
Allow printing output in JSON formated style
  • Loading branch information
JeanMertz authored Feb 23, 2018
2 parents 9408029 + 4936823 commit aa3c0eb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Options:
-r NAME=URL, --repo=NAME=URL,... List of NAME=URL pairs of repositories to add
to the index before compiling charts config
-p DIR, --partials-dir=DIR Path from which to load partial templates
[default: config/deploy/partials]
-j, --json Print resources formatted as JSON instead of
YAML. Each resource is printed on a single
line.
--example-config Print an example charts.yaml, including
extended documentation on the tunables
```
Expand Down
3 changes: 3 additions & 0 deletions config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Options:
to the index before compiling charts config
-p DIR, --partials-dir=DIR Path from which to load partial templates
[default: config/deploy/partials]
-j, --json Print resources formatted as JSON instead of
YAML. Each resource is printed on a single
line.
--example-config Print an example charts.yaml, including
extended documentation on the tunables
`
Expand Down
2 changes: 2 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type CLIOptions struct {
ChartsConfigurationPath string
PartialTemplatesPath string
ChartsConfigurationOptions *ChartsConfigurationOptions
OutputJSON bool
}

// ChartsConfigurationOptions contains the CLI options relevant for the charts
Expand All @@ -30,6 +31,7 @@ func NewCLIOptions(cli map[string]interface{}) (*CLIOptions, error) {
namespace, _ := cli["--namespace"].(string)

c := &CLIOptions{
OutputJSON: cli["--json"].(bool),
ChartsConfigurationPath: path,
ChartsConfigurationOptions: &ChartsConfigurationOptions{
Name: name,
Expand Down
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/blendle/kubecrt/chartsconfig"
"github.com/blendle/kubecrt/config"
"github.com/blendle/kubecrt/helm"
"github.com/ghodss/yaml"
)

func main() {
Expand Down Expand Up @@ -70,6 +72,14 @@ func main() {
os.Exit(1)
}

if opts.OutputJSON {
out, err = toJSON(out)
if err != nil {
fmt.Printf("error converting chart to JSON format: %s\n", err)
os.Exit(1)
}
}

if cli["--output"] == nil {
fmt.Print(string(out))
return
Expand All @@ -88,3 +98,21 @@ func readInput(input string) ([]byte, error) {

return ioutil.ReadFile(input)
}

func toJSON(input []byte) ([]byte, error) {
var err error

bs := bytes.Split(input, []byte("---"))
for i := range bs {
if len(bs[i]) == 0 {
continue
}

bs[i], err = yaml.YAMLToJSON(bs[i])
if err != nil {
return nil, err
}
}

return bytes.Join(bs, []byte("\n")), nil
}

0 comments on commit aa3c0eb

Please sign in to comment.