From f3f705d23b57bccedd53b60efcb919ba19a97990 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 13 Oct 2020 21:07:53 +0200 Subject: [PATCH] Add method to write out the file Signed-off-by: kolaente --- magefile.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/magefile.go b/magefile.go index 064255418..efab51287 100644 --- a/magefile.go +++ b/magefile.go @@ -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 = `` +) + // 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 }