-
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.
Introduces a new command to export Markdown documentation to a user-defined folder. Updates dependencies and integrates the new command into the CommandLineApplication workflow.
- Loading branch information
1 parent
4b66068
commit 0c2fbbd
Showing
5 changed files
with
100 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/matzefriedrich/cobra-extensions/internal/utils" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/commands" | ||
"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 commands.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
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