2022-06-14 12:40:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
|
|
|
|
|
|
|
cmdscripts "github.com/authelia/authelia/v4/cmd/authelia-scripts/cmd"
|
|
|
|
"github.com/authelia/authelia/v4/internal/commands"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newDocsCLICmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2022-09-16 04:21:05 +00:00
|
|
|
Use: cmdUseDocsCLI,
|
2022-06-14 12:40:00 +00:00
|
|
|
Short: "Generate CLI docs",
|
|
|
|
RunE: docsCLIRunE,
|
2022-09-01 02:24:47 +00:00
|
|
|
|
|
|
|
DisableAutoGenTag: true,
|
2022-06-14 12:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func docsCLIRunE(cmd *cobra.Command, args []string) (err error) {
|
2022-10-22 11:19:32 +00:00
|
|
|
var outputPath string
|
2022-09-16 04:21:05 +00:00
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if outputPath, err = getPFlagPath(cmd.Flags(), cmdFlagRoot, cmdFlagDocs, cmdFlagDocsContent, cmdFlagDocsCLIReference); err != nil {
|
2022-09-16 04:21:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-14 12:40:00 +00:00
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = os.MkdirAll(outputPath, 0775); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
if !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = genCLIDoc(commands.NewRootCmd(), filepath.Join(outputPath, "authelia")); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = genCLIDocWriteIndex(outputPath, "authelia"); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = genCLIDoc(cmdscripts.NewRootCmd(), filepath.Join(outputPath, "authelia-scripts")); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = genCLIDocWriteIndex(outputPath, "authelia-scripts"); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = genCLIDoc(newRootCmd(), filepath.Join(outputPath, cmdUseRoot)); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 11:19:32 +00:00
|
|
|
if err = genCLIDocWriteIndex(outputPath, cmdUseRoot); err != nil {
|
2022-06-14 12:40:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genCLIDoc(cmd *cobra.Command, path string) (err error) {
|
2022-09-16 04:21:05 +00:00
|
|
|
if _, err = os.Stat(path); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
if err = os.RemoveAll(path); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove docs: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 12:40:00 +00:00
|
|
|
if err = os.Mkdir(path, 0755); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = doc.GenMarkdownTreeCustom(cmd, path, prepend, linker); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genCLIDocWriteIndex(path, name string) (err error) {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
f, err := os.Create(filepath.Join(path, name, "_index.md"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
weight := genCLIDocCmdToWeight(name)
|
2022-06-14 12:40:00 +00:00
|
|
|
|
|
|
|
_, err = fmt.Fprintf(f, indexDocs, name, now.Format(dateFmtYAML), "cli-"+name, weight)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepend(input string) string {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
pathz := strings.Split(strings.Replace(input, ".md", "", 1), "\\")
|
|
|
|
parts := strings.Split(pathz[len(pathz)-1], "_")
|
|
|
|
|
|
|
|
cmd := parts[0]
|
|
|
|
|
|
|
|
args := strings.Join(parts, " ")
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
weight := genCLIDocCmdToWeight(parts[0])
|
|
|
|
|
|
|
|
if len(parts) != 1 {
|
|
|
|
weight += 5
|
2022-06-14 12:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(prefixDocs, args, fmt.Sprintf("Reference for the %s command.", args), "", now.Format(dateFmtYAML), "cli-"+cmd, weight)
|
|
|
|
}
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
func genCLIDocCmdToWeight(cmd string) int {
|
|
|
|
switch cmd {
|
|
|
|
case "authelia":
|
|
|
|
return 900
|
|
|
|
case "authelia-gen":
|
|
|
|
return 910
|
|
|
|
case "authelia-scripts":
|
|
|
|
return 920
|
|
|
|
default:
|
|
|
|
return 990
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 12:40:00 +00:00
|
|
|
func linker(input string) string {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
const indexDocs = `---
|
|
|
|
title: "%s"
|
|
|
|
description: ""
|
|
|
|
lead: ""
|
|
|
|
date: %s
|
|
|
|
draft: false
|
|
|
|
images: []
|
|
|
|
menu:
|
|
|
|
reference:
|
|
|
|
parent: "cli"
|
|
|
|
identifier: "%s"
|
|
|
|
weight: %d
|
|
|
|
toc: true
|
|
|
|
---
|
|
|
|
`
|
|
|
|
|
|
|
|
const prefixDocs = `---
|
|
|
|
title: "%s"
|
|
|
|
description: "%s"
|
|
|
|
lead: "%s"
|
|
|
|
date: %s
|
|
|
|
draft: false
|
|
|
|
images: []
|
|
|
|
menu:
|
|
|
|
reference:
|
|
|
|
parent: "%s"
|
|
|
|
weight: %d
|
|
|
|
toc: true
|
|
|
|
---
|
|
|
|
|
|
|
|
`
|