/
output.go
95 lines (81 loc) · 2.3 KB
/
output.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package viper
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path"
"strings"
"time"
jsonschemax "github.com/jkgx/jsonschema"
errorsx "github.com/jkgx/errors"
"github.com/ory/jsonschema/v3"
"github.com/ory/viper"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v1"
)
func formatValidationErrorForCLI(w io.Writer, conf []byte, err error) {
switch e := errorsx.Cause(err).(type) {
case *jsonschema.ValidationError:
pointer, validation := jsonschemaFormatError(e)
if pointer == "#" {
if len(e.Causes) == 0 {
_, _ = fmt.Fprintln(w, "(root)")
_, _ = fmt.Fprintln(w, "^-- "+validation)
_, _ = fmt.Fprintln(w, "")
}
} else {
spaces := make([]string, len(pointer)+3)
_, _ = fmt.Fprintf(w, "%s: %+v", pointer, gjson.GetBytes(conf, pointer).Value())
_, _ = fmt.Fprintln(w, "")
_, _ = fmt.Fprintf(w, "%s^-- %s", strings.Join(spaces, " "), validation)
_, _ = fmt.Fprintln(w, "")
_, _ = fmt.Fprintln(w, "")
}
for _, cause := range e.Causes {
formatValidationErrorForCLI(w, conf, cause)
}
default:
fmt.Printf("Received unexpected error: %+v", err)
}
}
// PrintHumanReadableValidationErrors prints human readable validation errors. Duh.
func PrintHumanReadableValidationErrors(w io.Writer, err error) {
var conf bytes.Buffer
_ = json.NewEncoder(&conf).Encode(viper.AllSettings())
formatValidationErrorForCLI(w, conf.Bytes(), err)
}
func jsonschemaFormatError(e *jsonschema.ValidationError) (string, string) {
var (
err error
pointer string
message string
)
pointer = e.InstancePtr
message = e.Message
switch ctx := e.Context.(type) {
case *jsonschema.ValidationErrorContextRequired:
if len(ctx.Missing) > 0 {
message = "one or more required properties are missing"
pointer = ctx.Missing[0]
}
}
// We can ignore the error as it will simply echo the pointer.
pointer, err = jsonschemax.JSONPointerToDotNotation(pointer)
if err != nil {
pointer = e.InstancePtr
}
return pointer, message
}
const permOwnerRW = 0o600
func sensitiveDumpAllValues(dir string) error {
configContent, err := yaml.Marshal(viper.AllSettings())
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(
ioutil.WriteFile(path.Join(dir, fmt.Sprintf("config-%d.yml", time.Now().UnixNano())), configContent, permOwnerRW),
)
}