-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
48 lines (40 loc) · 1.08 KB
/
dump.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
package config
import (
"bufio"
"bytes"
"io"
"github.com/BurntSushi/toml"
"github.com/redforks/appinfo"
)
func getDefaultOptionKVs() map[string]Option {
opts := make(map[string]Option, len(options))
for _, rec := range options {
opts[rec.name] = rec.creator()
}
return opts
}
func commentOutAll(prefix string, reader io.Reader) string {
writer := bytes.Buffer{}
_, _ = writer.WriteString(prefix) // no need to check error returned by Buffer WriteXXX() methods
liner := bufio.NewScanner(reader)
for liner.Scan() {
if len(liner.Bytes()) != 0 {
_, _ = writer.WriteString("# ")
}
_, _ = writer.Write(liner.Bytes())
_, _ = writer.WriteRune('\n')
}
return writer.String()
}
// DumpDefaultOptions dump default options in config file format. All options
// are comment out.
func DumpDefaultOptions() (string, error) {
opts := getDefaultOptionKVs()
buf := bytes.Buffer{}
encoder := toml.NewEncoder(&buf)
encoder.Indent = ""
if err := encoder.Encode(opts); err != nil {
return "", err
}
return commentOutAll("# default options for "+appinfo.CodeName()+"\n\n", &buf), nil
}