Add method to write out the file

Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
kolaente 2020-10-13 21:07:53 +02:00
parent decfd35cb1
commit f3f705d23b
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 39 additions and 1 deletions

View File

@ -746,6 +746,8 @@ func parseYamlConfigNode(node *yaml.Node) (config *configOption) {
description: strings.ReplaceAll(node.HeadComment, "# ", ""),
}
// TODO: second-level comments don't work
for i, n2 := range node.Content {
coo := &configOption{
key: n2.Value,
@ -777,6 +779,7 @@ func printConfig(config []*configOption, level int) (rendered string) {
for _, option := range config {
if option.key != "" {
rendered += "#"
for i := 0; i <= level; i++ {
rendered += "#"
}
@ -802,6 +805,11 @@ func printConfig(config []*configOption, level int) (rendered string) {
return
}
const (
configDocPath = `docs/content/doc/setup/config.md`
configInjectComment = `<!-- Generated config will be injected here -->`
)
// Generates the error docs from a commented config.yml.sample file in the repo root.
func GenerateDocs() error {
@ -825,7 +833,37 @@ func GenerateDocs() error {
}
}
fmt.Println(printConfig(conf, 0))
renderedConfig := printConfig(conf, 0)
// Rebuild the config
file, err := os.OpenFile(configDocPath, os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
// We read the config doc up until the marker, then stop and append our generated config
fullConfig := ""
scanner := bufio.NewScanner(file)
for scanner.Scan() {
t := scanner.Text()
fullConfig += t + "\n"
if t == configInjectComment {
break
}
}
if err := scanner.Err(); err != nil {
return err
}
fullConfig += "\n" + renderedConfig
if _, err := file.WriteAt([]byte(fullConfig), 0); err != nil {
return err
}
return nil
}