2023-01-27 15:10:40 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-11 11:22:55 -04:00
|
|
|
"cmp"
|
2023-01-27 15:10:40 -03:00
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
2023-03-23 17:42:20 -05:00
|
|
|
"path/filepath"
|
2026-08-11 11:22:55 -04:00
|
|
|
"slices"
|
2023-01-27 15:10:40 -03:00
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/enterprise/cli"
|
2026-03-05 13:58:10 +02:00
|
|
|
"github.com/coder/coder/v2/scripts/atomicwrite"
|
2026-07-08 11:39:45 -04:00
|
|
|
"github.com/coder/coder/v2/scripts/docgenenv"
|
2023-03-23 17:42:20 -05:00
|
|
|
"github.com/coder/flog"
|
2024-03-15 11:24:38 -05:00
|
|
|
"github.com/coder/serpent"
|
2023-01-27 15:10:40 -03:00
|
|
|
)
|
|
|
|
|
|
2026-08-11 11:22:55 -04:00
|
|
|
// cliIndexRoute holds the "Command Line" manifest route's metadata so the
|
|
|
|
|
// generated index page can mirror it through the shared docgenenv.FrontMatter
|
|
|
|
|
// emitter (see the frontMatter template func in gen.go). main populates it
|
|
|
|
|
// before genTree runs.
|
|
|
|
|
var cliIndexRoute docgenenv.Route
|
2023-01-27 15:10:40 -03:00
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
func deleteEmptyDirs(dir string) error {
|
|
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2023-01-27 22:07:47 +02:00
|
|
|
}
|
2023-03-23 17:42:20 -05:00
|
|
|
if !info.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
ents, err := os.ReadDir(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(ents) == 0 {
|
|
|
|
|
flog.Infof("deleting empty dir\t %v", path)
|
|
|
|
|
err = os.Remove(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-01-27 22:07:47 +02:00
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
func main() {
|
2026-07-08 11:39:45 -04:00
|
|
|
docgenenv.Prepare()
|
2023-01-27 15:10:40 -03:00
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
workdir, err := os.Getwd()
|
2023-01-27 15:10:40 -03:00
|
|
|
if err != nil {
|
2023-03-23 17:42:20 -05:00
|
|
|
flog.Fatalf("getwd: %v", err)
|
|
|
|
|
}
|
|
|
|
|
root := (&cli.RootCmd{})
|
|
|
|
|
|
|
|
|
|
// wroteMap indexes file paths to commands.
|
2024-03-17 09:45:26 -05:00
|
|
|
wroteMap := make(map[string]*serpent.Command)
|
2023-03-23 17:42:20 -05:00
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
docsDir = filepath.Join(workdir, "docs")
|
2024-08-13 18:39:46 +03:00
|
|
|
cliMarkdownDir = filepath.Join(docsDir, "reference/cli")
|
2023-03-23 17:42:20 -05:00
|
|
|
)
|
|
|
|
|
|
2026-03-05 13:58:10 +02:00
|
|
|
if d := os.Getenv("DOCS_DIR"); d != "" {
|
|
|
|
|
docsDir = d
|
|
|
|
|
cliMarkdownDir = filepath.Join(docsDir, "reference/cli")
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 11:22:55 -04:00
|
|
|
// Load the manifest up front so the generated index page can mirror the
|
|
|
|
|
// "Command Line" route's curated metadata (title/description/icon_path)
|
|
|
|
|
// instead of the root command name.
|
|
|
|
|
manifestPath := filepath.Join(docsDir, "manifest.json")
|
|
|
|
|
man, err := docgenenv.LoadManifest(manifestPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
flog.Fatalf("%v", err)
|
|
|
|
|
}
|
|
|
|
|
cmdLine := man.FindRoute("Reference", "Command Line")
|
|
|
|
|
if cmdLine == nil {
|
|
|
|
|
flog.Fatalf("could not find Command Line route in manifest %q", manifestPath)
|
|
|
|
|
}
|
|
|
|
|
// Mirror the whole "Command Line" route (minus its nav children) so the
|
|
|
|
|
// index page front matter carries every current and future per-page field
|
|
|
|
|
// automatically, the same way the API index mirrors its manifest route.
|
|
|
|
|
cliIndexRoute = cliIndexRouteFrom(*cmdLine)
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
cmd, err := root.Command(root.EnterpriseSubcommands())
|
|
|
|
|
if err != nil {
|
|
|
|
|
flog.Fatalf("creating command: %v", err)
|
|
|
|
|
}
|
|
|
|
|
err = genTree(
|
|
|
|
|
cliMarkdownDir,
|
|
|
|
|
cmd,
|
|
|
|
|
wroteMap,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
flog.Fatalf("generating markdowns: %v", err)
|
2023-01-27 15:10:40 -03:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
// Delete old files
|
|
|
|
|
err = filepath.Walk(cliMarkdownDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if info.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
_, ok := wroteMap[path]
|
|
|
|
|
if !ok {
|
|
|
|
|
flog.Infof("deleting old doc\t %v", path)
|
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2023-01-27 15:10:40 -03:00
|
|
|
if err != nil {
|
2023-03-23 17:42:20 -05:00
|
|
|
flog.Fatalf("deleting old docs: %v", err)
|
2023-01-27 15:10:40 -03:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
err = deleteEmptyDirs(cliMarkdownDir)
|
2023-01-27 15:10:40 -03:00
|
|
|
if err != nil {
|
2023-03-23 17:42:20 -05:00
|
|
|
flog.Fatalf("deleting empty dirs: %v", err)
|
2023-01-27 15:10:40 -03:00
|
|
|
}
|
|
|
|
|
|
2026-08-11 11:22:55 -04:00
|
|
|
// Rebuild the "Command Line" route's children from the generated pages.
|
|
|
|
|
// cmdLine aliases the manifest loaded above, so mutating it updates the
|
|
|
|
|
// manifest in place.
|
|
|
|
|
cmdLine.Children = nil
|
|
|
|
|
for path, cmd := range wroteMap {
|
|
|
|
|
relPath, err := filepath.Rel(docsDir, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
flog.Fatalf("getting relative path: %v", err)
|
2023-03-23 17:42:20 -05:00
|
|
|
}
|
2026-08-11 11:22:55 -04:00
|
|
|
child := cliCommandRoute(cmd)
|
|
|
|
|
child.Path = relPath
|
|
|
|
|
cmdLine.Children = append(cmdLine.Children, child)
|
2023-03-23 17:42:20 -05:00
|
|
|
}
|
2026-08-11 11:22:55 -04:00
|
|
|
// Sort children by title because wroteMap iteration is non-deterministic.
|
|
|
|
|
slices.SortFunc(cmdLine.Children, func(a, b docgenenv.Route) int {
|
|
|
|
|
return cmp.Compare(a.Title, b.Title)
|
|
|
|
|
})
|
2023-01-27 15:10:40 -03:00
|
|
|
|
2026-08-11 11:22:55 -04:00
|
|
|
manifestByt, err := json.MarshalIndent(man, "", " ")
|
2023-01-27 15:10:40 -03:00
|
|
|
if err != nil {
|
2023-03-23 17:42:20 -05:00
|
|
|
flog.Fatalf("marshaling manifest: %v", err)
|
2023-01-27 15:10:40 -03:00
|
|
|
}
|
2023-03-23 17:42:20 -05:00
|
|
|
|
2026-03-05 13:58:10 +02:00
|
|
|
err = atomicwrite.File(manifestPath, manifestByt)
|
2023-01-27 15:10:40 -03:00
|
|
|
if err != nil {
|
2023-03-23 17:42:20 -05:00
|
|
|
flog.Fatalf("writing manifest: %v", err)
|
2023-01-27 15:10:40 -03:00
|
|
|
}
|
|
|
|
|
}
|