Wiki Deploy Code

This commit is contained in:
2026-04-30 19:51:50 +02:00
parent d893785c0c
commit f480815e5d
7 changed files with 801 additions and 407 deletions
+82 -9
View File
@@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
@@ -139,7 +140,7 @@ var commands = []command{
},
{
name: "deploy-wiki",
description: "Deploy wiki pages to DokuWiki.",
description: "Deploy generated wiki pages to NodeBB.",
run: runDeployWiki,
},
}
@@ -674,9 +675,11 @@ func runDeployWiki(ctx context) error {
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
fmt.Fprintf(ctx.stdout, "local pages: %d\n", result.LocalPages)
fmt.Fprintf(ctx.stdout, "created: %d\n", result.Created)
fmt.Fprintf(ctx.stdout, "updated: %d\n", result.Updated)
fmt.Fprintf(ctx.stdout, "skipped: %d\n", result.Skipped)
fmt.Fprintf(ctx.stdout, "deleted: %d\n", result.Deleted)
fmt.Fprintf(ctx.stdout, "stale: %d\n", result.Stale)
fmt.Fprintf(ctx.stdout, "drifted: %d\n", result.Drifted)
fmt.Fprintf(ctx.stdout, "manifest: %s\n", result.Manifest)
return nil
}
@@ -710,6 +713,12 @@ func parseDeployWikiArgs(commandName string, args []string) (topdata.DeployWikiO
return opts, fmt.Errorf("--password requires a value")
}
opts.Password = args[i]
case "--token":
i++
if i >= len(args) {
return opts, fmt.Errorf("--token requires a value")
}
opts.Token = args[i]
case "--version":
i++
if i >= len(args) {
@@ -722,6 +731,17 @@ func parseDeployWikiArgs(commandName string, args []string) (topdata.DeployWikiO
return opts, fmt.Errorf("--namespace requires a value")
}
opts.Namespaces = append(opts.Namespaces, args[i])
case "--category":
i++
if i >= len(args) {
return opts, fmt.Errorf("--category requires a namespace=cid value")
}
if opts.CategoryIDs == nil {
opts.CategoryIDs = map[string]int{}
}
if err := parseWikiCategoryArg(args[i], opts.CategoryIDs); err != nil {
return opts, err
}
case "--manifest":
i++
if i >= len(args) {
@@ -730,8 +750,12 @@ func parseDeployWikiArgs(commandName string, args []string) (topdata.DeployWikiO
opts.ManifestPath = args[i]
case "--dry-run":
opts.DryRun = true
case "--force":
opts.Force = true
case "--create":
opts.AllowCreates = true
case "-h", "--help":
return opts, fmt.Errorf("usage: %s [--source-dir <path>] [--endpoint <url>] [--username <user>] [--password <pass>] [--version <ver>] [--namespace <ns> ...] [--manifest <path>] [--dry-run]", commandName)
return opts, fmt.Errorf("usage: %s [--source-dir <path>] [--endpoint <url>] [--token <token>] [--version <ver>] [--namespace <ns> ...] [--category <namespace=cid> ...] [--manifest <path>] [--dry-run] [--create] [--force]", commandName)
default:
if strings.HasPrefix(arg, "--source-dir=") {
opts.SourceDir = strings.TrimPrefix(arg, "--source-dir=")
@@ -741,14 +765,27 @@ func parseDeployWikiArgs(commandName string, args []string) (topdata.DeployWikiO
opts.Username = strings.TrimPrefix(arg, "--username=")
} else if strings.HasPrefix(arg, "--password=") {
opts.Password = strings.TrimPrefix(arg, "--password=")
} else if strings.HasPrefix(arg, "--token=") {
opts.Token = strings.TrimPrefix(arg, "--token=")
} else if strings.HasPrefix(arg, "--version=") {
opts.Version = strings.TrimPrefix(arg, "--version=")
} else if strings.HasPrefix(arg, "--namespace=") {
opts.Namespaces = append(opts.Namespaces, strings.TrimPrefix(arg, "--namespace="))
} else if strings.HasPrefix(arg, "--category=") {
if opts.CategoryIDs == nil {
opts.CategoryIDs = map[string]int{}
}
if err := parseWikiCategoryArg(strings.TrimPrefix(arg, "--category="), opts.CategoryIDs); err != nil {
return opts, err
}
} else if strings.HasPrefix(arg, "--manifest=") {
opts.ManifestPath = strings.TrimPrefix(arg, "--manifest=")
} else if arg == "--dry-run" {
opts.DryRun = true
} else if arg == "--force" {
opts.Force = true
} else if arg == "--create" {
opts.AllowCreates = true
} else {
return opts, fmt.Errorf("unknown %s argument %q", commandName, arg)
}
@@ -756,21 +793,57 @@ func parseDeployWikiArgs(commandName string, args []string) (topdata.DeployWikiO
}
if opts.Endpoint == "" {
opts.Endpoint = os.Getenv("DOKUWIKI_RPC_ENDPOINT")
opts.Endpoint = os.Getenv("NODEBB_API_ENDPOINT")
}
if opts.Username == "" {
opts.Username = os.Getenv("DOKUWIKI_RPC_USERNAME")
}
if opts.Password == "" {
opts.Password = os.Getenv("DOKUWIKI_RPC_PASSWORD")
if opts.Token == "" {
opts.Token = os.Getenv("NODEBB_API_TOKEN")
}
if opts.Version == "" {
opts.Version = os.Getenv("GITHUB_REF_NAME")
}
if opts.CategoryIDs == nil {
opts.CategoryIDs = map[string]int{}
}
if raw := os.Getenv("NODEBB_WIKI_CATEGORIES"); raw != "" {
if err := parseWikiCategoryList(raw, opts.CategoryIDs); err != nil {
return opts, err
}
}
return opts, nil
}
func parseWikiCategoryList(raw string, target map[string]int) error {
for _, part := range strings.Split(raw, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
if err := parseWikiCategoryArg(part, target); err != nil {
return err
}
}
return nil
}
func parseWikiCategoryArg(raw string, target map[string]int) error {
namespace, rawCID, ok := strings.Cut(raw, "=")
if !ok {
return fmt.Errorf("wiki category mapping %q must be namespace=cid", raw)
}
namespace = strings.TrimSpace(namespace)
rawCID = strings.TrimSpace(rawCID)
if namespace == "" || rawCID == "" {
return fmt.Errorf("wiki category mapping %q must be namespace=cid", raw)
}
cid, err := strconv.Atoi(rawCID)
if err != nil || cid <= 0 {
return fmt.Errorf("wiki category mapping %q has invalid cid", raw)
}
target[namespace] = cid
return nil
}
func loadProject(cwd string) (*project.Project, error) {
root, err := project.FindRoot(cwd)
if err != nil {