Skip to content

Commit bc477ab

Browse files
committed
CLOUDP-317123 change markup for code blocks in Atlas CLI generated docs
1 parent ff05591 commit bc477ab

File tree

4 files changed

+70
-48
lines changed

4 files changed

+70
-48
lines changed

.github/workflows/code-health.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,25 @@ jobs:
268268
with:
269269
config: ${{ vars.PERMISSIONS_CONFIG }}
270270
- uses: actions/checkout@v4
271+
- name: Install ShellCheck
272+
run: |
273+
sudo apt-get update
274+
sudo apt-get install -y shellcheck
271275
- name: Run ShellCheck
272-
uses: bewuethr/shellcheck-action@d01912909579c4b1a335828b8fca197fbb8e0aa4
276+
run: |
277+
# Find all shell scripts excluding specific patterns or directories
278+
# Add exclusions by adding more -not -path patterns as needed
279+
find . -name "*.sh" \
280+
-not -path "./.git/*" \
281+
-not -path "./docs/*" \
282+
-print0 | xargs -0 shellcheck --format=gcc
283+
284+
# Also check for shell scripts without .sh extension
285+
find . -type f -exec grep -l '^#!/bin/\(ba\)\?sh' {} \; \
286+
| grep -v '\.sh$' \
287+
| grep -v './.git/' \
288+
| grep -v './docs/' \
289+
| xargs -r shellcheck --format=gcc
273290
tidy:
274291
runs-on: ubuntu-latest
275292
steps:

build/ci/check-licenses.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ find_files() {
2020
find . -not \( \
2121
\( \
2222
-wholename '*mock*' \
23+
-o -wholename '*docs*' \
2324
-o -wholename '*third_party*' \
2425
\) -prune \
2526
\) \

tools/cmd/docs/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28+
const (
29+
docsPath = "./docs/command"
30+
includesPath = "./docs/command/includes"
31+
)
32+
2833
func main() {
29-
if err := os.RemoveAll("./docs/command"); err != nil {
34+
if err := os.RemoveAll(docsPath); err != nil {
3035
log.Fatal(err)
3136
}
3237

3338
const docsPermissions = 0766
34-
if err := os.MkdirAll("./docs/command", docsPermissions); err != nil {
39+
if err := os.MkdirAll(includesPath, docsPermissions); err != nil {
3540
log.Fatal(err)
3641
}
3742

@@ -43,7 +48,7 @@ func main() {
4348
log.Fatal(err)
4449
}
4550

46-
if err := cobra2snooty.GenTreeDocs(atlasBuilder, "./docs/command", cobra2snooty.WithCustomExampleFormatter(func(w io.Writer, cmd *cobra.Command) {
51+
if err := cobra2snooty.GenTreeDocs(atlasBuilder, docsPath, cobra2snooty.WithCustomExampleFormatter(func(w io.Writer, cmd *cobra.Command) {
4752
if isAPICommand(cmd) {
4853
_, _ = w.Write([]byte(cmd.Example))
4954
return

tools/cmd/docs/transformations.go

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package main
1717
import (
1818
"cmp"
1919
_ "embed"
20+
"fmt"
21+
"os"
2022
"regexp"
2123
"slices"
2224
"strconv"
@@ -138,9 +140,9 @@ func countExamples(examples map[string][]metadatatypes.Example) int {
138140
return count
139141
}
140142

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
142144
if len(examples) == 0 {
143-
return ""
145+
return "", nil
144146
}
145147

146148
var sb strings.Builder
@@ -159,6 +161,11 @@ func buildExamples(cmd *cobra.Command, examples map[string][]metadatatypes.Examp
159161
exampleIdx := 0
160162
for _, version := range sortedKeys(examples) {
161163
for _, ex := range examples[version] {
164+
source := strings.ToLower(strings.ReplaceAll(ex.Source, " ", "_"))
165+
if ex.Source == "-" {
166+
source = "default"
167+
}
168+
162169
if tabs {
163170
sb.WriteString(" .. tab:: ")
164171
if ex.Name == "" {
@@ -174,43 +181,36 @@ func buildExamples(cmd *cobra.Command, examples map[string][]metadatatypes.Examp
174181
sb.WriteString("\n :tabid: ")
175182
sb.WriteString(version)
176183
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)
182185
sb.WriteString("\n\n")
183186
}
184187

185188
if ex.Value != "" {
186189
if tabs {
187190
sb.WriteString(" ")
188191
}
189-
sb.WriteString("Create the file below and save it as ``payload.json``\n\n")
192+
sb.WriteString(ex.Description + "\n\n")
190193

191194
if tabs {
192195
sb.WriteString(" ")
193196
}
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")
201198

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+
}
207205

208-
sb.WriteString(" " + line + "\n")
206+
if tabs {
207+
sb.WriteString(" ")
209208
}
209+
sb.WriteString(fmt.Sprintf(" literalinclude:: /includes/%s\n", includePayloadFileName))
210210
if tabs {
211-
sb.WriteString("\n ")
211+
sb.WriteString(" ")
212212
}
213-
sb.WriteString(".. Code end marker, please don't delete this comment\n\n")
213+
sb.WriteString(" :language: shell\n")
214214
if tabs {
215215
sb.WriteString(" ")
216216
}
@@ -219,36 +219,34 @@ func buildExamples(cmd *cobra.Command, examples map[string][]metadatatypes.Examp
219219
sb.WriteString(" ")
220220
}
221221

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])
228228
}
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)
231236
}
237+
232238
if tabs {
233239
sb.WriteString(" ")
234240
}
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))
244242
if tabs {
245243
sb.WriteString(" ")
246244
}
247-
sb.WriteString(".. Code end marker, please don't delete this comment\n\n")
245+
sb.WriteString(":language: shell\n")
248246
}
249247
}
250248

251-
return sb.String()
249+
return sb.String(), nil
252250
}
253251

254252
func updateExamples(cmd *cobra.Command) error {
@@ -262,9 +260,10 @@ func updateExamples(cmd *cobra.Command) error {
262260
return nil
263261
}
264262

265-
cmd.Example = buildExamples(cmd, cmdMetadata.Examples)
263+
var err error
264+
cmd.Example, err = buildExamples(cmd, cmdMetadata.Examples)
266265

267-
return nil
266+
return err
268267
}
269268

270269
func removeCommandsWithOnlyPrivatePreview(cmd *cobra.Command) {

0 commit comments

Comments
 (0)