forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
111 lines (95 loc) · 2.73 KB
/
helpers.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package latest
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
kyaml "github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/ghodss/yaml"
)
func ReadSessionSecrets(filename string) (*configapi.SessionSecrets, error) {
config := &configapi.SessionSecrets{}
if err := ReadYAMLFile(filename, config); err != nil {
return nil, err
}
return config, nil
}
func ReadMasterConfig(filename string) (*configapi.MasterConfig, error) {
config := &configapi.MasterConfig{}
if err := ReadYAMLFile(filename, config); err != nil {
return nil, err
}
return config, nil
}
func ReadAndResolveMasterConfig(filename string) (*configapi.MasterConfig, error) {
masterConfig, err := ReadMasterConfig(filename)
if err != nil {
return nil, err
}
if err := configapi.ResolveMasterConfigPaths(masterConfig, path.Dir(filename)); err != nil {
return nil, err
}
return masterConfig, nil
}
func ReadNodeConfig(filename string) (*configapi.NodeConfig, error) {
config := &configapi.NodeConfig{}
if err := ReadYAMLFile(filename, config); err != nil {
return nil, err
}
return config, nil
}
func ReadAndResolveNodeConfig(filename string) (*configapi.NodeConfig, error) {
nodeConfig, err := ReadNodeConfig(filename)
if err != nil {
return nil, err
}
if err := configapi.ResolveNodeConfigPaths(nodeConfig, path.Dir(filename)); err != nil {
return nil, err
}
return nodeConfig, nil
}
func WriteYAML(obj runtime.Object) ([]byte, error) {
json, err := Codec.Encode(obj)
if err != nil {
return nil, err
}
content, err := yaml.JSONToYAML(json)
if err != nil {
return nil, err
}
return content, err
}
func ReadYAMLFile(filename string, obj runtime.Object) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
data, err = kyaml.ToJSON(data, false)
if err != nil {
return err
}
err = Codec.DecodeInto(data, obj)
return captureSurroundingJSONForError(fmt.Sprintf("could not load config file %q due to a error: ", filename), data, err)
}
// TODO: we ultimately want a better decoder for JSON that allows us exact line numbers and better
// surrounding text description. This should be removed / replaced when that happens.
func captureSurroundingJSONForError(prefix string, data []byte, err error) error {
if syntaxErr, ok := err.(*json.SyntaxError); err != nil && ok {
offset := syntaxErr.Offset
begin := offset - 20
if begin < 0 {
begin = 0
}
end := offset + 20
if end > int64(len(data)) {
end = int64(len(data))
}
return fmt.Errorf("%s%v (found near '%s')", prefix, err, string(data[begin:end]))
}
if err != nil {
return fmt.Errorf("%s%v", prefix, err)
}
return err
}