-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds Markdown documentation command * Updates command descriptions and example app details * Refactors command options to improve flexibility * Adds tests for rendering markdown command output.
- Loading branch information
1 parent
4b66068
commit 7f29ab9
Showing
10 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/matzefriedrich/cobra-extensions/internal/utils" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/types" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type markdownDocsCommand struct { | ||
use types.CommandName `flag:"markdown" short:"Exports Markdown documentation to the specified folder" description:"Exports Markdown documentation to the specified folder"` | ||
OutputFolderPath string `flag:"output" usage:"The output folder for markdown documentation" default:"."` | ||
root *cobra.Command | ||
} | ||
|
||
func (m *markdownDocsCommand) Execute() { | ||
|
||
outputPath, _ := filepath.Abs(m.OutputFolderPath) | ||
|
||
ext := filepath.Ext(outputPath) | ||
switch ext { | ||
case ".md", ".markdown": | ||
m.generateSingleMarkdownFile(outputPath) | ||
default: | ||
err := doc.GenMarkdownTree(m.root, outputPath) | ||
if err != nil { | ||
log.Fatalf("Error generating markdown documentation: %v", err) | ||
} | ||
} | ||
|
||
} | ||
|
||
func (m *markdownDocsCommand) generateSingleMarkdownFile(outputPath string) { | ||
|
||
writer, _ := os.OpenFile(outputPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | ||
defer func(writer *os.File) { | ||
_ = writer.Close() | ||
}(writer) | ||
|
||
stack := utils.MakeStack[*cobra.Command]() | ||
stack.Push(m.root) | ||
|
||
for stack.Any() { | ||
next := stack.Pop() | ||
for _, c := range next.Commands() { | ||
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { | ||
continue | ||
} | ||
stack.Push(c) | ||
} | ||
|
||
_ = doc.GenMarkdownCustom(next, writer, func(s string) string { | ||
return s | ||
}) | ||
|
||
_, _ = writer.WriteString("\n") | ||
} | ||
|
||
_ = writer.Sync() | ||
} | ||
|
||
var _ types.TypedCommand = (*markdownDocsCommand)(nil) | ||
|
||
// NewMarkdownDocsCommand creates a new Cobra command for exporting Markdown documentation to a specified folder. | ||
func NewMarkdownDocsCommand(root *cobra.Command) *cobra.Command { | ||
instance := &markdownDocsCommand{ | ||
root: root, | ||
} | ||
return CreateTypedCommand(instance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func Test_MarkdownCommand_Execute_renders_multiple_files_if_folder_path_specified(t *testing.T) { | ||
|
||
// Arrange | ||
app := &cobra.Command{} | ||
sut := NewMarkdownDocsCommand(app) | ||
|
||
app.AddCommand(sut) | ||
app.SetArgs([]string{"markdown", "--output", os.TempDir()}) | ||
|
||
// Act | ||
err := app.Execute() | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
} | ||
|
||
func Test_MarkdownCommand_Execute_renders_single_file_if_markdown_file_path_specified(t *testing.T) { | ||
|
||
// Arrange | ||
app := &cobra.Command{} | ||
sut := NewMarkdownDocsCommand(app) | ||
|
||
app.AddCommand(sut) | ||
app.SetArgs([]string{"markdown", "--output", filepath.Join(os.TempDir(), "test.md")}) | ||
|
||
// Act | ||
err := app.Execute() | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters