Skip to content

Commit 5947d8c

Browse files
committed
generating docs for subcommands§
1 parent 42a18d7 commit 5947d8c

File tree

8 files changed

+255
-31
lines changed

8 files changed

+255
-31
lines changed

cmd/doc-gen/main.go

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"regexp"
88

99
"github.com/l3montree-dev/devguard/cmd/devguard-scanner/commands"
10+
"github.com/spf13/cobra"
1011
"github.com/spf13/cobra/doc"
1112
)
1213

@@ -58,6 +59,46 @@ func postProcessMarkdown(filename string) {
5859
}
5960
}
6061

62+
// generateDocsForCommand recursively generates docs for a command and all its subcommands
63+
func generateDocsForCommand(cmd *cobra.Command, outDir string) {
64+
identity := func(s string) string { return s }
65+
emptyStr := func(s string) string { return "" }
66+
67+
// Print to stdout first
68+
err := doc.GenMarkdownCustom(cmd, os.Stdout, emptyStr)
69+
if err != nil {
70+
slog.Error("could not generate markdown documentation", "err", err, "cmd", cmd.Name())
71+
return
72+
}
73+
74+
// Generate markdown for this command
75+
filename := filepath.Join(outDir, cmd.Name()+".md")
76+
f, err := os.Create(filename)
77+
if err != nil {
78+
slog.Error("could not create file", "err", err, "file", filename)
79+
return
80+
}
81+
82+
err = doc.GenMarkdownCustom(cmd, f, identity)
83+
f.Close()
84+
if err != nil {
85+
slog.Error("could not write markdown", "err", err, "file", filename)
86+
return
87+
}
88+
89+
// Post-process the file
90+
postProcessMarkdown(filename)
91+
92+
// Recursively generate docs for subcommands
93+
for _, subCmd := range cmd.Commands() {
94+
// Skip hidden commands
95+
if subCmd.Hidden {
96+
continue
97+
}
98+
generateDocsForCommand(subCmd, outDir)
99+
}
100+
}
101+
61102
func main() {
62103
// check if no arguments were provided
63104
if len(os.Args) < 2 {
@@ -67,38 +108,8 @@ func main() {
67108
return
68109
}
69110

70-
// Generate docs with custom settings to disable "SEE ALSO" section
111+
// Generate root command doc first
71112
identity := func(s string) string { return s }
72-
emptyStr := func(s string) string { return "" }
73-
74-
for _, cmd := range commands.RootCmd.Commands() {
75-
// Generate markdown for each command
76-
err := doc.GenMarkdownCustom(cmd, os.Stdout, emptyStr)
77-
if err != nil {
78-
slog.Error("could not generate markdown documentation", "err", err, "cmd", cmd.Name())
79-
continue
80-
}
81-
82-
// Write to file
83-
filename := filepath.Join("docs/scanner", cmd.Name()+".md")
84-
f, err := os.Create(filename)
85-
if err != nil {
86-
slog.Error("could not create file", "err", err, "file", filename)
87-
continue
88-
}
89-
90-
err = doc.GenMarkdownCustom(cmd, f, identity)
91-
f.Close()
92-
if err != nil {
93-
slog.Error("could not write markdown", "err", err, "file", filename)
94-
continue
95-
}
96-
97-
// Post-process: remove "devguard-scanner " prefix from headline and remove SEE ALSO
98-
postProcessMarkdown(filename)
99-
}
100-
101-
// Generate root command doc separately
102113
rootFilename := filepath.Join("docs/scanner", "devguard-scanner.md")
103114
rootFile, err := os.Create(rootFilename)
104115
if err != nil {
@@ -113,6 +124,15 @@ func main() {
113124
}
114125
}
115126

127+
// Generate docs for all commands (including nested subcommands)
128+
for _, cmd := range commands.RootCmd.Commands() {
129+
// Skip hidden commands
130+
if cmd.Hidden {
131+
continue
132+
}
133+
generateDocsForCommand(cmd, "docs/scanner")
134+
}
135+
116136
return
117137
}
118138
}

docs/scanner/fetch-links.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## intoto fetch-links
2+
3+
Fetch links for a given supply chain
4+
5+
```shell
6+
devguard-scanner intoto fetch-links [flags]
7+
```
8+
9+
### Options
10+
11+
```shell
12+
--apiUrl string The devguard api url (default "api.devguard.org")
13+
--assetName string The asset name to use
14+
-h, --help help for fetch-links
15+
--supplyChainId string The supply chain id to fetch the links for
16+
--token string The token to use to authenticate with the devguard api
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```shell
22+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
23+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
24+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
25+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
26+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
27+
--step string The name of the in-toto link
28+
```

docs/scanner/help.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## intoto help
2+
3+
Help about any command
4+
5+
### Synopsis
6+
7+
Help provides help for any command in the application.
8+
Simply type intoto help [path to command] for full details.
9+
10+
```shell
11+
devguard-scanner intoto help [command] [flags]
12+
```
13+
14+
### Options
15+
16+
```shell
17+
-h, --help help for help
18+
```
19+
20+
### Options inherited from parent commands
21+
22+
```shell
23+
--apiUrl string The devguard api url
24+
--assetName string The asset name to use
25+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
26+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
27+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
28+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
29+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
30+
--step string The name of the in-toto link
31+
--supplyChainId string The supply chain id to use. If empty, tries to extract the current commit hash.
32+
--token string The token to use for in-toto
33+
```

docs/scanner/run.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## intoto run
2+
3+
4+
5+
```shell
6+
devguard-scanner intoto run [flags]
7+
```
8+
9+
### Options
10+
11+
```shell
12+
--apiUrl string The URL of the devguard API
13+
-h, --help help for run
14+
--step string The step to run
15+
--supplyChainOutputDigest string If defined, sends this digest to devguard. This should be the digest of the whole supply chain.
16+
```
17+
18+
### Options inherited from parent commands
19+
20+
```shell
21+
--assetName string The asset name to use
22+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
23+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
24+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
25+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
26+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
27+
--supplyChainId string The supply chain id to use. If empty, tries to extract the current commit hash.
28+
--token string The token to use for in-toto
29+
```

docs/scanner/setup.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## intoto setup
2+
3+
Setup in-toto
4+
5+
```shell
6+
devguard-scanner intoto setup [flags]
7+
```
8+
9+
### Options
10+
11+
```shell
12+
-h, --help help for setup
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```shell
18+
--apiUrl string The devguard api url
19+
--assetName string The asset name to use
20+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
21+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
22+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
23+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
24+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
25+
--step string The name of the in-toto link
26+
--supplyChainId string The supply chain id to use. If empty, tries to extract the current commit hash.
27+
--token string The token to use for in-toto
28+
```

docs/scanner/start.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## intoto start
2+
3+
Start in-toto recording
4+
5+
```shell
6+
devguard-scanner intoto start [flags]
7+
```
8+
9+
### Options
10+
11+
```shell
12+
-h, --help help for start
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```shell
18+
--apiUrl string The devguard api url
19+
--assetName string The asset name to use
20+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
21+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
22+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
23+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
24+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
25+
--step string The name of the in-toto link
26+
--supplyChainId string The supply chain id to use. If empty, tries to extract the current commit hash.
27+
--token string The token to use for in-toto
28+
```

docs/scanner/stop.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## intoto stop
2+
3+
Stop in-toto recording
4+
5+
```shell
6+
devguard-scanner intoto stop [flags]
7+
```
8+
9+
### Options
10+
11+
```shell
12+
-h, --help help for stop
13+
--output string The output file name. Default is the <step>.link.json name
14+
```
15+
16+
### Options inherited from parent commands
17+
18+
```shell
19+
--apiUrl string The devguard api url
20+
--assetName string The asset name to use
21+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
22+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
23+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
24+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
25+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
26+
--step string The name of the in-toto link
27+
--supplyChainId string The supply chain id to use. If empty, tries to extract the current commit hash.
28+
--token string The token to use for in-toto
29+
```

docs/scanner/verify.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## intoto verify
2+
3+
Verify a supply chain
4+
5+
```shell
6+
devguard-scanner intoto verify [flags]
7+
```
8+
9+
### Options
10+
11+
```shell
12+
-h, --help help for verify
13+
--layoutKey string Path to the layout key
14+
--supplyChainId string Supply chain ID
15+
--token string Token
16+
```
17+
18+
### Options inherited from parent commands
19+
20+
```shell
21+
--apiUrl string The devguard api url
22+
--assetName string The asset name to use
23+
--generateSlsaProvenance Generate SLSA provenance for the in-toto link. The provenance will be stored in <stepname>.provenance.json. It will be signed using the intoto token.
24+
--ignore stringArray The ignore patterns for the in-toto link (default [.git/**/*])
25+
-l, --logLevel string Set the log level. Options: debug, info, warn, error (default "info")
26+
--materials stringArray The materials to include in the in-toto link. Default is the current directory (default [.])
27+
--products stringArray The products to include in the in-toto link. Default is the current directory (default [.])
28+
--step string The name of the in-toto link
29+
```

0 commit comments

Comments
 (0)