Skip to content

Commit aa3c0eb

Browse files
authored
Merge pull request #16 from blendle/json
Allow printing output in JSON formated style
2 parents 9408029 + 4936823 commit aa3c0eb

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ Options:
6767
-r NAME=URL, --repo=NAME=URL,... List of NAME=URL pairs of repositories to add
6868
to the index before compiling charts config
6969
-p DIR, --partials-dir=DIR Path from which to load partial templates
70+
[default: config/deploy/partials]
71+
-j, --json Print resources formatted as JSON instead of
72+
YAML. Each resource is printed on a single
73+
line.
7074
--example-config Print an example charts.yaml, including
7175
extended documentation on the tunables
7276
```

config/cli.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ Options:
4444
to the index before compiling charts config
4545
-p DIR, --partials-dir=DIR Path from which to load partial templates
4646
[default: config/deploy/partials]
47+
-j, --json Print resources formatted as JSON instead of
48+
YAML. Each resource is printed on a single
49+
line.
4750
--example-config Print an example charts.yaml, including
4851
extended documentation on the tunables
4952
`

config/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type CLIOptions struct {
1010
ChartsConfigurationPath string
1111
PartialTemplatesPath string
1212
ChartsConfigurationOptions *ChartsConfigurationOptions
13+
OutputJSON bool
1314
}
1415

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

3233
c := &CLIOptions{
34+
OutputJSON: cli["--json"].(bool),
3335
ChartsConfigurationPath: path,
3436
ChartsConfigurationOptions: &ChartsConfigurationOptions{
3537
Name: name,

main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -9,6 +10,7 @@ import (
910
"github.com/blendle/kubecrt/chartsconfig"
1011
"github.com/blendle/kubecrt/config"
1112
"github.com/blendle/kubecrt/helm"
13+
"github.com/ghodss/yaml"
1214
)
1315

1416
func main() {
@@ -70,6 +72,14 @@ func main() {
7072
os.Exit(1)
7173
}
7274

75+
if opts.OutputJSON {
76+
out, err = toJSON(out)
77+
if err != nil {
78+
fmt.Printf("error converting chart to JSON format: %s\n", err)
79+
os.Exit(1)
80+
}
81+
}
82+
7383
if cli["--output"] == nil {
7484
fmt.Print(string(out))
7585
return
@@ -88,3 +98,21 @@ func readInput(input string) ([]byte, error) {
8898

8999
return ioutil.ReadFile(input)
90100
}
101+
102+
func toJSON(input []byte) ([]byte, error) {
103+
var err error
104+
105+
bs := bytes.Split(input, []byte("---"))
106+
for i := range bs {
107+
if len(bs[i]) == 0 {
108+
continue
109+
}
110+
111+
bs[i], err = yaml.YAMLToJSON(bs[i])
112+
if err != nil {
113+
return nil, err
114+
}
115+
}
116+
117+
return bytes.Join(bs, []byte("\n")), nil
118+
}

0 commit comments

Comments
 (0)