forked from unknwon/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
56 lines (52 loc) · 1.38 KB
/
write.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
// Copyright 2013 The Author - Unknown. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package GoConfig
import (
"bytes"
"os"
)
// SaveConfigFile writes configuration file to local file system
func SaveConfigFile(c *ConfigFile, filename string) (err error) {
// Write configuration file by filename
var f *os.File
if f, err = os.Create(filename); err != nil {
return err
}
// Data buffer
buf := bytes.NewBuffer(nil)
// Write sections
for section, sectionmap := range c.data {
// Write section comments
if len(c.GetSectionComments(section)) > 0 {
if _, err = buf.WriteString(c.GetSectionComments(section) + LineBreak); err != nil {
return err
}
}
// Write section name
if _, err = buf.WriteString("[" + section + "]" + LineBreak); err != nil {
return err
}
// Write keys
for key, value := range sectionmap {
// Write key comments
if len(c.GetKeyComments(section, key)) > 0 {
if _, err = buf.WriteString(c.GetKeyComments(section, key) + LineBreak); err != nil {
return err
}
}
// Write key and value
if _, err = buf.WriteString(key + "=" + value + LineBreak); err != nil {
return err
}
}
// Put a line between sections
if _, err = buf.WriteString(LineBreak); err != nil {
return err
}
}
// Write to file
buf.WriteTo(f)
f.Close()
return nil
}