Add method to write out the file
Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
parent
decfd35cb1
commit
f3f705d23b
40
magefile.go
40
magefile.go
|
@ -746,6 +746,8 @@ func parseYamlConfigNode(node *yaml.Node) (config *configOption) {
|
||||||
description: strings.ReplaceAll(node.HeadComment, "# ", ""),
|
description: strings.ReplaceAll(node.HeadComment, "# ", ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: second-level comments don't work
|
||||||
|
|
||||||
for i, n2 := range node.Content {
|
for i, n2 := range node.Content {
|
||||||
coo := &configOption{
|
coo := &configOption{
|
||||||
key: n2.Value,
|
key: n2.Value,
|
||||||
|
@ -777,6 +779,7 @@ func printConfig(config []*configOption, level int) (rendered string) {
|
||||||
for _, option := range config {
|
for _, option := range config {
|
||||||
if option.key != "" {
|
if option.key != "" {
|
||||||
|
|
||||||
|
rendered += "#"
|
||||||
for i := 0; i <= level; i++ {
|
for i := 0; i <= level; i++ {
|
||||||
rendered += "#"
|
rendered += "#"
|
||||||
}
|
}
|
||||||
|
@ -802,6 +805,11 @@ func printConfig(config []*configOption, level int) (rendered string) {
|
||||||
return
|
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.
|
// Generates the error docs from a commented config.yml.sample file in the repo root.
|
||||||
func GenerateDocs() error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue