aboutsummaryrefslogtreecommitdiffstats
path: root/commands/execenv
diff options
context:
space:
mode:
Diffstat (limited to 'commands/execenv')
-rw-r--r--commands/execenv/env.go11
-rw-r--r--commands/execenv/env_testing.go10
2 files changed, 21 insertions, 0 deletions
diff --git a/commands/execenv/env.go b/commands/execenv/env.go
index b383dde0..4be7c247 100644
--- a/commands/execenv/env.go
+++ b/commands/execenv/env.go
@@ -1,6 +1,7 @@
package execenv
import (
+ "encoding/json"
"fmt"
"io"
"os"
@@ -38,6 +39,7 @@ type Out interface {
Printf(format string, a ...interface{})
Print(a ...interface{})
Println(a ...interface{})
+ PrintJSON(v interface{}) error
// String returns what have been written in the output before, as a string.
// This only works in test scenario.
@@ -66,6 +68,15 @@ func (o out) Println(a ...interface{}) {
_, _ = fmt.Fprintln(o, a...)
}
+func (o out) PrintJSON(v interface{}) error {
+ raw, err := json.MarshalIndent(v, "", " ")
+ if err != nil {
+ return err
+ }
+ o.Println(string(raw))
+ return nil
+}
+
func (o out) String() string {
panic("only work with a test env")
}
diff --git a/commands/execenv/env_testing.go b/commands/execenv/env_testing.go
index 5761b410..34eafc9c 100644
--- a/commands/execenv/env_testing.go
+++ b/commands/execenv/env_testing.go
@@ -2,6 +2,7 @@ package execenv
import (
"bytes"
+ "encoding/json"
"fmt"
"testing"
@@ -27,6 +28,15 @@ func (te *TestOut) Println(a ...interface{}) {
_, _ = fmt.Fprintln(te.Buffer, a...)
}
+func (te *TestOut) PrintJSON(v interface{}) error {
+ raw, err := json.MarshalIndent(v, "", " ")
+ if err != nil {
+ return err
+ }
+ te.Println(string(raw))
+ return nil
+}
+
func NewTestEnv(t *testing.T) *Env {
t.Helper()