@@ -3,7 +3,9 @@ package config
3
3
import (
4
4
"github.com/pkg/errors"
5
5
"gopkg.in/yaml.v2"
6
+ "io/ioutil"
6
7
"os"
8
+ "path/filepath"
7
9
"time"
8
10
)
9
11
@@ -103,20 +105,34 @@ func LoadControllerConfig(fileName string) (config Controller, err error) {
103
105
if err != nil {
104
106
return
105
107
}
106
-
107
- b , err := readAll (f )
108
+ b , err := ioutil .ReadAll (f )
108
109
if err != nil {
109
110
return
110
111
}
111
112
112
113
err = yaml .Unmarshal (b , & config )
113
114
if err != nil {
114
- err = errors .Errorf ( "Could not unmarshal config file %v. %v" , fileName , err )
115
+ err = errors .Wrapf ( err , "Could not unmarshal config file %v. %v" , fileName )
115
116
}
116
117
117
118
return
118
119
}
119
120
121
+ func (c * Controller ) WriteTo (fileName string ) (err error ) {
122
+ b , err := yaml .Marshal (c )
123
+ if err != nil {
124
+ err = errors .Wrapf (err , "Could not marshal config %v" , c )
125
+ return
126
+ }
127
+ err = os .MkdirAll (filepath .Dir (fileName ), 0755 )
128
+ if err != nil {
129
+ err = errors .Wrapf (err , "Could not create directly for config file: %v" , fileName )
130
+ return
131
+ }
132
+ err = ioutil .WriteFile (fileName , b , 0600 )
133
+ return
134
+ }
135
+
120
136
// DefaultControllerConfig creates a config with default values
121
137
func DefaultControllerConfig () (c Controller ) {
122
138
c .Network .PublishAddress = "224.5.23.1:10003"
@@ -180,15 +196,3 @@ func DefaultControllerConfig() (c Controller) {
180
196
181
197
return
182
198
}
183
-
184
- func readAll (f * os.File ) ([]byte , error ) {
185
- b := make ([]byte , 10000 )
186
- n , err := f .Read (b )
187
- if err != nil {
188
- return []byte {}, errors .Errorf ("Can not read config files: %v" , err )
189
- }
190
- if n == len (b ) {
191
- return []byte {}, errors .New ("Buffer size for reading config file is too small" )
192
- }
193
- return b [:n ], nil
194
- }
0 commit comments