github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/gen_config_doc.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  import (
     7  	"log"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  	"text/template"
    12  	"time"
    13  
    14  	"github.com/choria-io/go-choria/build"
    15  	"github.com/choria-io/go-choria/config"
    16  	"github.com/choria-io/go-choria/confkey"
    17  	"github.com/choria-io/go-choria/internal/fs"
    18  	"github.com/choria-io/go-choria/internal/util"
    19  )
    20  
    21  type configs struct {
    22  	Keys    [][]string
    23  	Docs    []*confkey.Doc
    24  	Now     string
    25  	Version string
    26  }
    27  
    28  func panicIfErr(err error) {
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  }
    33  
    34  func main() {
    35  	cfg, err := config.NewDefaultConfig()
    36  	panicIfErr(err)
    37  
    38  	keys, err := cfg.ConfigKeys(".")
    39  	panicIfErr(err)
    40  
    41  	if len(keys) == 0 {
    42  		panic("no configuration keys found")
    43  	}
    44  
    45  	docs := configs{
    46  		Docs:    []*confkey.Doc{},
    47  		Now:     time.Now().UTC().Format(time.RFC822),
    48  		Version: build.Version,
    49  	}
    50  
    51  	util.SliceGroups(keys, 2, func(grp []string) {
    52  		docs.Keys = append(docs.Keys, grp)
    53  	})
    54  
    55  	for _, key := range keys {
    56  		doc := cfg.DocForConfigKey(key)
    57  		if doc == nil {
    58  			continue
    59  		}
    60  
    61  		docs.Docs = append(docs.Docs, doc)
    62  	}
    63  
    64  	if len(docs.Docs) == 0 {
    65  		panic("no documentation strings were generated")
    66  	}
    67  
    68  	funcs := template.FuncMap{
    69  		"gha": func(s string) string {
    70  			return strings.ReplaceAll(s, ".", "")
    71  		},
    72  	}
    73  
    74  	templ, err := fs.FS.ReadFile("misc/config_doc.templ")
    75  	if err != nil {
    76  		return
    77  	}
    78  
    79  	t, err := template.New("templates").Funcs(funcs).Delims("<<", ">>").Parse(string(templ))
    80  	if err != nil {
    81  		panic(err)
    82  	}
    83  
    84  	outfile := "docs/content/configuration/_index.md"
    85  
    86  	out, err := os.Create(path.Join(outfile))
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	defer out.Close()
    91  
    92  	err = t.Execute(out, docs)
    93  
    94  	log.Println("Generated " + outfile)
    95  }