Skip to content

Commit 3f5d27e

Browse files
nickytonlinejpmcb
andauthored
feat: now the documentation for the pizza-cli can be generated via pizza docs (#143)
* feat: now the documentation for the pizza-cli can be generated via pizza docs * test: added some test for the docs command * disabled auto generation of Cobra tail end in markdown Co-authored-by: John McBride <john@opensauced.pizza> * added docs/ to gitignore * now docs are generated from the root command * now all autogen tagging is disabled for doc generation * docs command now hidden * refactored directory creation out into functions * simplified absolute filepath generation Co-authored-by: John McBride <john@opensauced.pizza> * docs: added CLI docs * some cleanup --------- Co-authored-by: John McBride <john@opensauced.pizza>
1 parent cf9917f commit 3f5d27e

19 files changed

+698
-1
lines changed

cmd/docs/docs.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package docs
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/cobra/doc"
10+
)
11+
12+
type Options struct {
13+
Path string
14+
}
15+
16+
const DefaultPath = "./docs"
17+
18+
func GetDocsPath(path string) (string, error) {
19+
if path == "" {
20+
fmt.Printf("No path was provided. Using default path: %s\n", DefaultPath)
21+
path = DefaultPath
22+
}
23+
24+
absPath, err := filepath.Abs(path)
25+
26+
if err != nil {
27+
return "", fmt.Errorf("error resolving absolute path: %w", err)
28+
}
29+
30+
_, err = os.Stat(absPath)
31+
32+
if os.IsNotExist(err) {
33+
fmt.Printf("The directory %s does not exist. Creating it...\n", absPath)
34+
if err := os.MkdirAll(absPath, os.ModePerm); err != nil {
35+
return "", fmt.Errorf("error creating directory %s: %w", absPath, err)
36+
}
37+
} else if err != nil {
38+
return "", fmt.Errorf("error checking directory %s: %w", absPath, err)
39+
}
40+
41+
return absPath, nil
42+
}
43+
44+
func GenerateDocumentation(rootCmd *cobra.Command, path string) error {
45+
fmt.Printf("Generating documentation in %s...\n", path)
46+
err := doc.GenMarkdownTree(rootCmd, path)
47+
48+
if err != nil {
49+
return err
50+
}
51+
52+
fmt.Printf("Finished generating documentation in %s\n", path)
53+
54+
return nil
55+
}
56+
57+
func NewDocsCommand() *cobra.Command {
58+
return &cobra.Command{
59+
Use: "docs [path]",
60+
Short: "Generates the documentation for the CLI",
61+
Args: cobra.MaximumNArgs(1),
62+
RunE: func(cmd *cobra.Command, args []string) error {
63+
cmd.Parent().Root().DisableAutoGenTag = true
64+
65+
var path string
66+
if len(args) > 0 {
67+
path = args[0]
68+
}
69+
70+
resolvedPath, err := GetDocsPath(path)
71+
if err != nil {
72+
return err
73+
}
74+
75+
return GenerateDocumentation(cmd.Parent().Root(), resolvedPath)
76+
},
77+
}
78+
}

cmd/docs/docs_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package docs
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
func TestGetDocsPath(t *testing.T) {
10+
t.Parallel()
11+
12+
t.Run("No path provided", func(t *testing.T) {
13+
t.Parallel()
14+
actual, err := GetDocsPath("")
15+
16+
if err != nil {
17+
t.Errorf("GetDocsPath() error = %v, wantErr false", err)
18+
return
19+
}
20+
21+
expected, _ := filepath.Abs(DefaultPath)
22+
if actual != expected {
23+
t.Errorf("GetDocsPath() = %v, want %v", actual, expected)
24+
}
25+
})
26+
27+
t.Run("With path provided", func(t *testing.T) {
28+
t.Parallel()
29+
inputPath := filepath.Join(os.TempDir(), "docs")
30+
actual, err := GetDocsPath(inputPath)
31+
32+
if err != nil {
33+
t.Errorf("GetDocsPath() error = %v, wantErr false", err)
34+
return
35+
}
36+
37+
expected, _ := filepath.Abs(inputPath)
38+
if actual != expected {
39+
t.Errorf("GetDocsPath() = %v, want %v", actual, expected)
40+
}
41+
42+
if _, err := os.Stat(actual); os.IsNotExist(err) {
43+
t.Errorf("GetDocsPath() failed to create directory %s", actual)
44+
}
45+
})
46+
47+
t.Run("Invalid path", func(t *testing.T) {
48+
t.Parallel()
49+
invalidPath := string([]byte{0})
50+
51+
_, err := GetDocsPath(invalidPath)
52+
53+
if err == nil {
54+
t.Errorf("GetDocsPath() error = nil, wantErr true")
55+
}
56+
})
57+
}
58+
59+
func TestGetDocsPath_ExistingDirectory(t *testing.T) {
60+
t.Parallel()
61+
62+
tempDir, err := os.MkdirTemp("", "docs_test_existing")
63+
if err != nil {
64+
t.Fatalf("Failed to create temp dir: %v", err)
65+
}
66+
67+
actual, err := GetDocsPath(tempDir)
68+
69+
if err != nil {
70+
t.Errorf("GetDocsPath() error = %v, wantErr false", err)
71+
return
72+
}
73+
74+
expected, _ := filepath.Abs(tempDir)
75+
if actual != expected {
76+
t.Errorf("GetDocsPath() = %v, want %v", actual, expected)
77+
}
78+
79+
if _, err := os.Stat(actual); os.IsNotExist(err) {
80+
t.Errorf("GetDocsPath() failed to recognize existing directory %s", actual)
81+
}
82+
}

cmd/root/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88

99
"github.com/open-sauced/pizza-cli/cmd/auth"
10+
"github.com/open-sauced/pizza-cli/cmd/docs"
1011
"github.com/open-sauced/pizza-cli/cmd/generate"
1112
"github.com/open-sauced/pizza-cli/cmd/insights"
1213
"github.com/open-sauced/pizza-cli/cmd/version"
@@ -44,6 +45,11 @@ func NewRootCommand() (*cobra.Command, error) {
4445
cmd.AddCommand(insights.NewInsightsCommand())
4546
cmd.AddCommand(version.NewVersionCommand())
4647

48+
// The docs command is hidden as it's only used by the pizza-cli maintainers
49+
docsCmd := docs.NewDocsCommand()
50+
docsCmd.Hidden = true
51+
cmd.AddCommand(docsCmd)
52+
4753
err := cmd.PersistentFlags().MarkHidden(constants.FlagNameEndpoint)
4854
if err != nil {
4955
return nil, fmt.Errorf("error marking %s as hidden: %w", constants.FlagNameEndpoint, err)

docs/pizza.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## pizza
2+
3+
OpenSauced CLI
4+
5+
### Synopsis
6+
7+
A command line utility for insights, metrics, and all things OpenSauced
8+
9+
```
10+
pizza <command> <subcommand> [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-c, --config string The saucectl config (default "~/.sauced.yaml")
17+
--disable-telemetry Disable sending telemetry data to OpenSauced
18+
-h, --help help for pizza
19+
-l, --log-level string The logging level. Options: error, warn, info, debug (default "info")
20+
--tty-disable Disable log stylization. Suitable for CI/CD and automation
21+
```
22+
23+
### SEE ALSO
24+
25+
* [pizza completion](pizza_completion.md) - Generate the autocompletion script for the specified shell
26+
* [pizza generate](pizza_generate.md) - Generates something
27+
* [pizza insights](pizza_insights.md) - Gather insights about git contributors, repositories, users and pull requests
28+
* [pizza login](pizza_login.md) - Log into the CLI via GitHub
29+
* [pizza version](pizza_version.md) - Displays the build version of the CLI
30+

docs/pizza_completion.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## pizza completion
2+
3+
Generate the autocompletion script for the specified shell
4+
5+
### Synopsis
6+
7+
Generate the autocompletion script for pizza for the specified shell.
8+
See each sub-command's help for details on how to use the generated script.
9+
10+
11+
### Options
12+
13+
```
14+
-h, --help help for completion
15+
```
16+
17+
### Options inherited from parent commands
18+
19+
```
20+
-c, --config string The saucectl config (default "~/.sauced.yaml")
21+
--disable-telemetry Disable sending telemetry data to OpenSauced
22+
-l, --log-level string The logging level. Options: error, warn, info, debug (default "info")
23+
--tty-disable Disable log stylization. Suitable for CI/CD and automation
24+
```
25+
26+
### SEE ALSO
27+
28+
* [pizza](pizza.md) - OpenSauced CLI
29+
* [pizza completion bash](pizza_completion_bash.md) - Generate the autocompletion script for bash
30+
* [pizza completion fish](pizza_completion_fish.md) - Generate the autocompletion script for fish
31+
* [pizza completion powershell](pizza_completion_powershell.md) - Generate the autocompletion script for powershell
32+
* [pizza completion zsh](pizza_completion_zsh.md) - Generate the autocompletion script for zsh
33+

docs/pizza_completion_bash.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## pizza completion bash
2+
3+
Generate the autocompletion script for bash
4+
5+
### Synopsis
6+
7+
Generate the autocompletion script for the bash shell.
8+
9+
This script depends on the 'bash-completion' package.
10+
If it is not installed already, you can install it via your OS's package manager.
11+
12+
To load completions in your current shell session:
13+
14+
source <(pizza completion bash)
15+
16+
To load completions for every new session, execute once:
17+
18+
#### Linux:
19+
20+
pizza completion bash > /etc/bash_completion.d/pizza
21+
22+
#### macOS:
23+
24+
pizza completion bash > $(brew --prefix)/etc/bash_completion.d/pizza
25+
26+
You will need to start a new shell for this setup to take effect.
27+
28+
29+
```
30+
pizza completion bash
31+
```
32+
33+
### Options
34+
35+
```
36+
-h, --help help for bash
37+
--no-descriptions disable completion descriptions
38+
```
39+
40+
### Options inherited from parent commands
41+
42+
```
43+
-c, --config string The saucectl config (default "~/.sauced.yaml")
44+
--disable-telemetry Disable sending telemetry data to OpenSauced
45+
-l, --log-level string The logging level. Options: error, warn, info, debug (default "info")
46+
--tty-disable Disable log stylization. Suitable for CI/CD and automation
47+
```
48+
49+
### SEE ALSO
50+
51+
* [pizza completion](pizza_completion.md) - Generate the autocompletion script for the specified shell
52+

docs/pizza_completion_fish.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## pizza completion fish
2+
3+
Generate the autocompletion script for fish
4+
5+
### Synopsis
6+
7+
Generate the autocompletion script for the fish shell.
8+
9+
To load completions in your current shell session:
10+
11+
pizza completion fish | source
12+
13+
To load completions for every new session, execute once:
14+
15+
pizza completion fish > ~/.config/fish/completions/pizza.fish
16+
17+
You will need to start a new shell for this setup to take effect.
18+
19+
20+
```
21+
pizza completion fish [flags]
22+
```
23+
24+
### Options
25+
26+
```
27+
-h, --help help for fish
28+
--no-descriptions disable completion descriptions
29+
```
30+
31+
### Options inherited from parent commands
32+
33+
```
34+
-c, --config string The saucectl config (default "~/.sauced.yaml")
35+
--disable-telemetry Disable sending telemetry data to OpenSauced
36+
-l, --log-level string The logging level. Options: error, warn, info, debug (default "info")
37+
--tty-disable Disable log stylization. Suitable for CI/CD and automation
38+
```
39+
40+
### SEE ALSO
41+
42+
* [pizza completion](pizza_completion.md) - Generate the autocompletion script for the specified shell
43+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## pizza completion powershell
2+
3+
Generate the autocompletion script for powershell
4+
5+
### Synopsis
6+
7+
Generate the autocompletion script for powershell.
8+
9+
To load completions in your current shell session:
10+
11+
pizza completion powershell | Out-String | Invoke-Expression
12+
13+
To load completions for every new session, add the output of the above command
14+
to your powershell profile.
15+
16+
17+
```
18+
pizza completion powershell [flags]
19+
```
20+
21+
### Options
22+
23+
```
24+
-h, --help help for powershell
25+
--no-descriptions disable completion descriptions
26+
```
27+
28+
### Options inherited from parent commands
29+
30+
```
31+
-c, --config string The saucectl config (default "~/.sauced.yaml")
32+
--disable-telemetry Disable sending telemetry data to OpenSauced
33+
-l, --log-level string The logging level. Options: error, warn, info, debug (default "info")
34+
--tty-disable Disable log stylization. Suitable for CI/CD and automation
35+
```
36+
37+
### SEE ALSO
38+
39+
* [pizza completion](pizza_completion.md) - Generate the autocompletion script for the specified shell
40+

0 commit comments

Comments
 (0)