diff --git a/magefile.go b/magefile.go index ca98c9af0..e096a4820 100644 --- a/magefile.go +++ b/magefile.go @@ -740,7 +740,7 @@ type configOption struct { children []*configOption } -func parseYamlNode(node *yaml.Node) (config *configOption) { +func parseYamlConfigNode(node *yaml.Node) (config *configOption) { config = &configOption{ key: node.Value, description: node.HeadComment, @@ -765,7 +765,7 @@ func parseYamlNode(node *yaml.Node) (config *configOption) { if len(n2.Content) > 0 { for _, n := range n2.Content { - coo.children = append(coo.children, parseYamlNode(n)) + coo.children = append(coo.children, parseYamlConfigNode(n)) } } } @@ -773,6 +773,27 @@ func parseYamlNode(node *yaml.Node) (config *configOption) { return config } +func printConfig(config []*configOption, level int) (rendered string) { + for _, option := range config { + if option.key != "" { + + for i := 0; i <= level; i++ { + rendered += "#" + } + rendered += " " + option.key + "\n" + + if option.description != "" { + rendered += "\n\n" + option.description + "\n\n" + } + rendered += "Default: `" + option.defaultValue + "`\n" + } + + rendered += "\n" + printConfig(option.children, level+1) + } + + return +} + // Generates the error docs from a commented config.yml.sample file in the repo root. func GenerateDocs() error { @@ -791,33 +812,12 @@ func GenerateDocs() error { for _, node := range d.Content { for _, n := range node.Content { - co := parseYamlNode(n) + co := parseYamlConfigNode(n) conf = append(conf, co) } } - for _, option := range conf { - if option.key != "" { - fmt.Printf("Option: %s\n", option.key) - - if option.description != "" { - fmt.Printf("Description: %s\n", option.description) - } - } - - for _, child := range option.children { - if child.key != "" { - - // TODO: Add generating docs from recursive structure - - fmt.Printf("[Child] Option: %s, Default: %s\n", child.key, child.defaultValue) - - if child.description != "" { - fmt.Printf("[Child] Description: %s\n", child.description) - } - } - } - } + fmt.Println(printConfig(conf, 0)) return nil }