@@ -17,6 +17,8 @@ package main
17
17
import (
18
18
"cmp"
19
19
_ "embed"
20
+ "fmt"
21
+ "os"
20
22
"regexp"
21
23
"slices"
22
24
"strconv"
@@ -138,9 +140,9 @@ func countExamples(examples map[string][]metadatatypes.Example) int {
138
140
return count
139
141
}
140
142
141
- func buildExamples (cmd * cobra.Command , examples map [string ][]metadatatypes.Example ) string { //nolint:gocyclo // code used by CI
143
+ func buildExamples (cmd * cobra.Command , examples map [string ][]metadatatypes.Example ) ( string , error ) { //nolint:gocyclo // cyclomatic complexity is high, but the code is readable and only used for documentation generation
142
144
if len (examples ) == 0 {
143
- return ""
145
+ return "" , nil
144
146
}
145
147
146
148
var sb strings.Builder
@@ -159,6 +161,11 @@ func buildExamples(cmd *cobra.Command, examples map[string][]metadatatypes.Examp
159
161
exampleIdx := 0
160
162
for _ , version := range sortedKeys (examples ) {
161
163
for _ , ex := range examples [version ] {
164
+ source := strings .ToLower (strings .ReplaceAll (ex .Source , " " , "_" ))
165
+ if ex .Source == "-" {
166
+ source = "default"
167
+ }
168
+
162
169
if tabs {
163
170
sb .WriteString (" .. tab:: " )
164
171
if ex .Name == "" {
@@ -174,43 +181,36 @@ func buildExamples(cmd *cobra.Command, examples map[string][]metadatatypes.Examp
174
181
sb .WriteString ("\n :tabid: " )
175
182
sb .WriteString (version )
176
183
sb .WriteString ("_" )
177
- if ex .Source == "-" {
178
- sb .WriteString ("default" )
179
- } else {
180
- sb .WriteString (strings .ToLower (strings .ReplaceAll (ex .Source , " " , "_" )))
181
- }
184
+ sb .WriteString (source )
182
185
sb .WriteString ("\n \n " )
183
186
}
184
187
185
188
if ex .Value != "" {
186
189
if tabs {
187
190
sb .WriteString (" " )
188
191
}
189
- sb .WriteString ("Create the file below and save it as ``payload.json`` \n \n " )
192
+ sb .WriteString (ex . Description + " \n \n " )
190
193
191
194
if tabs {
192
195
sb .WriteString (" " )
193
196
}
194
- sb .WriteString (".. code-block::\n \n " )
195
- if ex .Description != "" {
196
- if tabs {
197
- sb .WriteString (" " )
198
- }
199
- sb .WriteString (" # " + ex .Description + "\n " )
200
- }
197
+ sb .WriteString ("Create the file below and save it as ``payload.json``\n \n " )
201
198
202
- lines := strings .Split (ex .Value , "\n " )
203
- for _ , line := range lines {
204
- if tabs {
205
- sb .WriteString (" " )
206
- }
199
+ includePayloadFileName := fmt .Sprintf ("%s-%s-%s-payload.json" , strings .ReplaceAll (cmd .CommandPath (), " " , "-" ), version , source )
200
+ const permissions = 0600
201
+ err := os .WriteFile (fmt .Sprintf ("%s/%s" , includesPath , includePayloadFileName ), []byte (ex .Value ), permissions )
202
+ if err != nil {
203
+ return "" , fmt .Errorf ("failed to write payload file %s: %w" , includePayloadFileName , err )
204
+ }
207
205
208
- sb .WriteString (" " + line + "\n " )
206
+ if tabs {
207
+ sb .WriteString (" " )
209
208
}
209
+ sb .WriteString (fmt .Sprintf (" literalinclude:: /includes/%s\n " , includePayloadFileName ))
210
210
if tabs {
211
- sb .WriteString ("\n " )
211
+ sb .WriteString (" " )
212
212
}
213
- sb .WriteString (".. Code end marker, please don't delete this comment \n \n " )
213
+ sb .WriteString (" :language: shell \n " )
214
214
if tabs {
215
215
sb .WriteString (" " )
216
216
}
@@ -219,36 +219,34 @@ func buildExamples(cmd *cobra.Command, examples map[string][]metadatatypes.Examp
219
219
sb .WriteString (" " )
220
220
}
221
221
222
- if tabs {
223
- sb . WriteString ( " " )
224
- }
225
- sb .WriteString (".. code-block:: \n \n " )
226
- if tabs {
227
- sb .WriteString (" " )
222
+ var cmdSB strings. Builder
223
+
224
+ cmdSB . WriteString ( cmd . CommandPath ())
225
+ cmdSB .WriteString (" --version " + version )
226
+ for _ , flagName := range sortedKeys ( ex . Flags ) {
227
+ cmdSB .WriteString (" --" + flagName + " " + ex . Flags [ flagName ] )
228
228
}
229
- if ex .Description != "" {
230
- sb .WriteString (" # " + ex .Description + "\n " )
229
+ cmdSB .WriteString ("\n " )
230
+
231
+ includeCommandFileName := fmt .Sprintf ("%s-%s-%s.sh" , strings .ReplaceAll (cmd .CommandPath (), " " , "-" ), version , source )
232
+ const permissions = 0600
233
+ err := os .WriteFile (fmt .Sprintf ("%s/%s" , includesPath , includeCommandFileName ), []byte (cmdSB .String ()), permissions )
234
+ if err != nil {
235
+ return "" , fmt .Errorf ("failed to write file %s: %w" , includeCommandFileName , err )
231
236
}
237
+
232
238
if tabs {
233
239
sb .WriteString (" " )
234
240
}
235
- sb .WriteString (" " + cmd .CommandPath ())
236
- sb .WriteString (" --version " + version )
237
- if ex .Value != "" {
238
- sb .WriteString (" --file payload.json" )
239
- }
240
- for _ , flagName := range sortedKeys (ex .Flags ) {
241
- sb .WriteString (" --" + flagName + " " + ex .Flags [flagName ])
242
- }
243
- sb .WriteString ("\n \n " )
241
+ sb .WriteString (fmt .Sprintf ("literalinclude:: /includes/%s\n " , includeCommandFileName ))
244
242
if tabs {
245
243
sb .WriteString (" " )
246
244
}
247
- sb .WriteString (".. Code end marker, please don't delete this comment \n \n " )
245
+ sb .WriteString (":language: shell \n " )
248
246
}
249
247
}
250
248
251
- return sb .String ()
249
+ return sb .String (), nil
252
250
}
253
251
254
252
func updateExamples (cmd * cobra.Command ) error {
@@ -262,9 +260,10 @@ func updateExamples(cmd *cobra.Command) error {
262
260
return nil
263
261
}
264
262
265
- cmd .Example = buildExamples (cmd , cmdMetadata .Examples )
263
+ var err error
264
+ cmd .Example , err = buildExamples (cmd , cmdMetadata .Examples )
266
265
267
- return nil
266
+ return err
268
267
}
269
268
270
269
func removeCommandsWithOnlyPrivatePreview (cmd * cobra.Command ) {
0 commit comments