Skip to content

Commit dfdaee7

Browse files
committed
Write json output to stdout
1 parent cb877d4 commit dfdaee7

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ go 1.16
44

55
require (
66
github.com/Microsoft/go-winio v0.4.16 // indirect
7+
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
78
github.com/anaskhan96/soup v1.2.4
89
github.com/containerd/containerd v1.4.4 // indirect
910
github.com/docker/distribution v2.7.1+incompatible // indirect
1011
github.com/docker/docker v20.10.5+incompatible
1112
github.com/docker/go-connections v0.4.0
1213
github.com/docker/go-units v0.4.0 // indirect
14+
github.com/fatih/color v1.10.0 // indirect
1315
github.com/golang/protobuf v1.4.3 // indirect
1416
github.com/google/go-cmp v0.5.4 // indirect
1517
github.com/gopherdata/gophernotes v0.7.2 // indirect
@@ -32,6 +34,7 @@ require (
3234
github.com/stretchr/testify v1.7.0
3335
github.com/xeonx/timeago v1.0.0-rc4
3436
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7 // indirect
37+
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 // indirect
3538
golang.org/x/text v0.3.5 // indirect
3639
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
3740
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
1818
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
1919
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
2020
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
21+
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
22+
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
2123
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
2224
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
2325
github.com/anaskhan96/soup v1.2.4 h1:or+sKs9QbzJGZVTYFmTs2VBateEywoq00a6K14z331E=
@@ -74,6 +76,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
7476
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
7577
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
7678
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
79+
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
80+
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
7781
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
7882
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
7983
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
@@ -189,6 +193,8 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
189193
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
190194
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
191195
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
196+
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
197+
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
192198
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
193199
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
194200
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
@@ -392,6 +398,8 @@ golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7w
392398
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
393399
golang.org/x/sys v0.0.0-20210223095934-7937bea0104d h1:u0GOGnBJ3EKE/tNqREhhGiCzE9jFXydDo2lf7hOwGuc=
394400
golang.org/x/sys v0.0.0-20210223095934-7937bea0104d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
401+
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg=
402+
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
395403
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
396404
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
397405
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=

pkg/cli/infer.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"mime"
@@ -11,6 +12,7 @@ import (
1112
"strings"
1213
"time"
1314

15+
"github.com/TylerBrock/colorjson"
1416
log "github.com/sirupsen/logrus"
1517
"github.com/spf13/cobra"
1618

@@ -91,32 +93,43 @@ func cmdInfer(cmd *cobra.Command, args []string) error {
9193
// TODO(andreas): support multiple outputs?
9294
output := result.Values["output"]
9395

94-
// TODO check content type so we don't barf binary data to stdout
95-
96-
if output.MimeType != "plain/text" && outPath == "" {
96+
// Write to stdout
97+
if outPath == "" {
98+
// Is it something we can sensibly write to stdout?
99+
if output.MimeType == "plain/text" {
100+
_, err := io.Copy(os.Stdout, output.Buffer)
101+
return err
102+
} else if output.MimeType == "application/json" {
103+
var obj map[string]interface{}
104+
dec := json.NewDecoder(output.Buffer)
105+
if err := dec.Decode(&obj); err != nil {
106+
return err
107+
}
108+
f := colorjson.NewFormatter()
109+
f.Indent = 2
110+
s, _ := f.Marshal(obj)
111+
fmt.Println(string(s))
112+
return nil
113+
}
114+
// Otherwise, fall back to writing file
97115
outPath = "output"
98116
extension, _ := mime.ExtensionsByType(output.MimeType)
99117
if len(extension) > 0 {
100118
outPath += extension[0]
101119
}
102120
}
103121

104-
outFile := os.Stdout
105-
if outPath != "" {
106-
outFile, err = os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE, 0755)
107-
if err != nil {
108-
return err
109-
}
122+
// Write to file
123+
outFile, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE, 0755)
124+
if err != nil {
125+
return err
110126
}
111127

112128
if _, err := io.Copy(outFile, output.Buffer); err != nil {
113129
return err
114130
}
115131

116-
if outPath != "" {
117-
fmt.Println("--> Written output to " + outPath)
118-
119-
}
132+
fmt.Println("--> Written output to " + outPath)
120133
return nil
121134
}
122135

0 commit comments

Comments
 (0)