forked from redhat-developer/odo
/
types.go
60 lines (48 loc) · 1.86 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package machineoutput
import (
"encoding/json"
"fmt"
"github.com/openshift/odo/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Kind is what kind we should use in the machine readable output
const Kind = "Error"
// APIVersion is the current API version we are using
const APIVersion = "odo.openshift.io/v1alpha1"
// GenericError for machine readable output error messages
type GenericError struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Message string `json:"message"`
}
// GenericSuccess same as above, but copy-and-pasted just in case
// we change the output in the future
type GenericSuccess struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Message string `json:"message"`
}
// OutputSuccess outputs a "successful" machine-readable output format in json
func OutputSuccess(machineOutput interface{}) {
printableOutput, err := MarshalJSONIndented(machineOutput)
// If we error out... there's no way to output it (since we disable logging when using -o json)
if err != nil {
fmt.Fprintf(log.GetStderr(), "Unable to unmarshal JSON: %s\n", err.Error())
} else {
fmt.Fprintf(log.GetStdout(), "%s\n", string(printableOutput))
}
}
// OutputError outputs a "successful" machine-readable output format in json
func OutputError(machineOutput interface{}) {
printableOutput, err := MarshalJSONIndented(machineOutput)
// If we error out... there's no way to output it (since we disable logging when using -o json)
if err != nil {
fmt.Fprintf(log.GetStderr(), "Unable to unmarshal JSON: %s\n", err.Error())
} else {
fmt.Fprintf(log.GetStderr(), "%s\n", string(printableOutput))
}
}
// MarshalJSONIndented returns indented json representation of obj
func MarshalJSONIndented(obj interface{}) ([]byte, error) {
return json.MarshalIndent(obj, "", " ")
}