1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
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{
Use: cmdUseDocsCLI,
Short: "Generate CLI docs",
RunE: docsCLIRunE,
DisableAutoGenTag: true,
}
return cmd
}
func docsCLIRunE(cmd *cobra.Command, args []string) (err error) {
var outputPath string
if outputPath, err = getPFlagPath(cmd.Flags(), cmdFlagRoot, cmdFlagDocs, cmdFlagDocsContent, cmdFlagDocsCLIReference); err != nil {
return err
}
if err = os.MkdirAll(outputPath, 0775); err != nil {
if !os.IsExist(err) {
return err
}
}
if err = genCLIDoc(commands.NewRootCmd(), filepath.Join(outputPath, "authelia")); err != nil {
return err
}
if err = genCLIDocWriteIndex(outputPath, "authelia"); err != nil {
return err
}
if err = genCLIDoc(cmdscripts.NewRootCmd(), filepath.Join(outputPath, "authelia-scripts")); err != nil {
return err
}
if err = genCLIDocWriteIndex(outputPath, "authelia-scripts"); err != nil {
return err
}
if err = genCLIDoc(newRootCmd(), filepath.Join(outputPath, cmdUseRoot)); err != nil {
return err
}
if err = genCLIDocWriteIndex(outputPath, cmdUseRoot); err != nil {
return err
}
return nil
}
func genCLIDoc(cmd *cobra.Command, path string) (err error) {
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)
}
}
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
}
weight := genCLIDocCmdToWeight(name)
_, err = fmt.Fprintf(f, indexDocs, name, now.Format(dateFmtYAML), prefixCLI+name, weight)
return err
}
func prepend(input string) string {
now := time.Now()
_, filename := filepath.Split(strings.Replace(input, ".md", "", 1))
parts := strings.Split(filename, "_")
cmd := parts[0]
args := strings.Join(parts, " ")
weight := genCLIDocCmdToWeight(parts[0])
if len(parts) != 1 {
weight += 5
}
return fmt.Sprintf(prefixDocs, args, fmt.Sprintf("Reference for the %s command.", args), "", now.Format(dateFmtYAML), prefixCLI+cmd, weight)
}
func genCLIDocCmdToWeight(cmd string) int {
switch cmd {
case "authelia":
return 900
case "authelia-gen":
return 910
case "authelia-scripts":
return 920
default:
return 990
}
}
func linker(input string) string {
return input
}
const indexDocs = `---
title: "%s"
description: ""
lead: ""
date: %s
draft: false
images: []
sidebar:
collapsed: true
menu:
reference:
parent: "cli"
identifier: "%s"
weight: %d
toc: true
seo:
title: "" # custom title (optional)
description: "" # custom description (recommended)
canonical: "" # custom canonical URL (optional)
noindex: false # false (default) or true
---
`
const prefixDocs = `---
title: "%s"
description: "%s"
lead: "%s"
date: %s
draft: false
images: []
menu:
reference:
parent: "%s"
weight: %d
toc: true
seo:
title: "" # custom title (optional)
description: "" # custom description (recommended)
canonical: "" # custom canonical URL (optional)
noindex: false # false (default) or true
---
`
|