Files
zeekayandHanzo Dev cbf668a980 Build on Go 1.27
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.

Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:47:46 -07:00

83 lines
2.1 KiB
Go

package main
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func main() {
}
func updateMkDocsNav(parent string, child string, subchild string, files []string) error {
trimPrefixes(files, "docs/")
sort.Strings(files)
data, err := os.ReadFile("mkdocs.yml")
if err != nil {
return fmt.Errorf("error reading mkdocs.yml: %w", err)
}
var un unstructured.Unstructured
if e := yaml.Unmarshal(data, &un.Object); e != nil {
return e
}
nav := un.Object["nav"].([]any)
rootitem, _ := findNavItem(nav, parent)
if rootitem == nil {
return fmt.Errorf("can't find '%s' root item in mkdoc.yml", parent)
}
rootnavitemmap := rootitem.(map[any]any)
childnav, _ := findNavItem(rootnavitemmap[parent].([]any), child)
if childnav == nil {
return fmt.Errorf("can't find '%s' chile item under '%s' parent item in mkdoc.yml", child, parent)
}
childnavmap := childnav.(map[any]any)
childnavitems := childnavmap[child].([]any)
childnavitems = removeNavItem(childnavitems, subchild)
commands := make(map[string]any)
commands[subchild] = files
childnavmap[child] = append(childnavitems, commands)
newmkdocs, err := yaml.Marshal(un.Object)
if err != nil {
return fmt.Errorf("error in marshaling final configmap: %w", err)
}
// The marshaller drops custom tags, so re-add this one. Turns out this is much less invasive than trying to handle
// it at the YAML parser level.
newmkdocs = bytes.Replace(newmkdocs, []byte("site_url: READTHEDOCS_CANONICAL_URL"), []byte("site_url: !ENV READTHEDOCS_CANONICAL_URL"), 1)
return os.WriteFile("mkdocs.yml", newmkdocs, 0o644)
}
func trimPrefixes(files []string, prefix string) {
for i, f := range files {
files[i] = strings.TrimPrefix(f, prefix)
}
}
func findNavItem(nav []any, key string) (any, int) {
for i, item := range nav {
o, ismap := item.(map[any]any)
if ismap {
if _, ok := o[key]; ok {
return o, i
}
}
}
return nil, -1
}
func removeNavItem(nav []any, key string) []any {
_, i := findNavItem(nav, key)
if i != -1 {
nav = append(nav[:i], nav[i+1:]...)
}
return nav
}