topics

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 14 Imported by: 0

README

                   Cobra Topics Extension
                   ======================

The cobrax/topics package provides a pluggable, topic-based help system for
Cobra CLI applications. It extends the default Cobra help functionality to
support arbitrary help topics loaded from files.

Features
--------

• File-based help topics with configurable extensions
• Default support for .txt and .md files
• Customizable extensions (e.g., add .txxt, .rst)
• Automatic topic discovery from directory
• Flag-style topic support (--dry-run → dry-run)
• Seamless fallback to Cobra's original help
• Shell completion for topics
• Easy single-function integration

Usage
-----

1. Import the package:

   import "github.com/arthur-debert/dodot/pkg/cobrax/topics"

2. Create help topic files in your project:

   cmd/myapp/topics/
   ├── option-dry-run.txt    # For flag/option documentation
   ├── option-verbose.txt    # Prefix with "option-" for flags
   ├── architecture.md       # General topics without prefix
   └── getting-started.txxt

3. Initialize in your CLI:

   rootCmd := &cobra.Command{...}
   
   // Option 1: Use default extensions (.txt, .md)
   if err := topics.Initialize(rootCmd, "cmd/myapp/topics"); err != nil {
       log.Debug().Err(err).Msg("Failed to initialize topics")
   }
   
   // Option 2: Add custom extensions
   opts := topics.Options{
       Extensions: []string{".txt", ".md", ".txxt", ".rst"},
   }
   if err := topics.InitializeWithOptions(rootCmd, "cmd/myapp/topics", opts); err != nil {
       log.Debug().Err(err).Msg("Failed to initialize topics")
   }

4. Users can now access help topics:

   $ myapp help dry-run        # Finds option-dry-run.txt
   $ myapp help architecture   # Finds architecture.md
   $ myapp help topics         # Lists all available topics

Topic Files
-----------

Default supported extensions:
• .txt - Plain text files
• .md - Markdown files

You can add more extensions (like .txxt, .rst, .adoc) using InitializeWithOptions.

Naming Conventions:
• option-<flag>.<ext> - For flag/option documentation (e.g., option-dry-run.txt)
• <topic>.<ext> - For general topics (e.g., architecture.md)

The option- prefix allows users to access flag help naturally:
• "help dry-run" finds option-dry-run.txt
• "help --dry-run" also finds option-dry-run.txt

Example Integration
-------------------

See dodot's integration in internal/cli/commands.go for a complete example
that searches multiple paths for help topics.

Best Practices
--------------

• Keep topic files concise and focused
• Use consistent formatting across topics
• Include examples in topic files
• Name files after the flags/concepts they explain
• Consider subdirectories for organization

Testing
-------

The package includes comprehensive tests. Run with:

$ go test ./pkg/cobrax/topics/...

Future Enhancements
-------------------

• Markdown rendering for terminal display
• Topic categories and grouping
• Multi-language support
• Topic search functionality

Documentation

Overview

Package topics provides a pluggable, topic-based help system for Cobra CLI applications. It extends the default Cobra help functionality to support arbitrary help topics loaded from files, making CLIs self-documenting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize(rootCmd *cobra.Command, topicsDir string) error

Initialize sets up the topic-based help system with default extensions

func InitializeWithOptions

func InitializeWithOptions(rootCmd *cobra.Command, topicsDir string, opts Options) error

InitializeWithOptions sets up the topic-based help system with custom options

Types

type GlamourRenderer

type GlamourRenderer struct {
	Style string // Style name: "dark", "light", "notty", "auto", or path to custom style
	Width int    // Terminal width (0 = auto-detect)
}

GlamourRenderer uses the glamour library for rich markdown rendering

func NewGlamourRenderer

func NewGlamourRenderer() *GlamourRenderer

NewGlamourRenderer creates a markdown renderer using glamour with auto-detection

func (*GlamourRenderer) Render

func (r *GlamourRenderer) Render(content string, format string) string

Render converts markdown to beautiful terminal output

type Options

type Options struct {
	// Extensions is the list of file extensions to consider as topics
	// Defaults to [".txt", ".md"] if not specified
	Extensions []string

	// Renderer for formatting topic content (optional)
	// Defaults to PlainRenderer if not specified
	Renderer Renderer
}

Options configures the TopicManager

type PlainRenderer

type PlainRenderer struct{}

PlainRenderer is the default renderer that returns content as-is

func (*PlainRenderer) Render

func (r *PlainRenderer) Render(content string, format string) string

Render returns the content unchanged

type Renderer

type Renderer interface {
	// Render takes raw content and returns formatted content for terminal display
	Render(content string, format string) string
}

Renderer defines the interface for rendering topic content

type Topic

type Topic struct {
	Name     string
	FilePath string
	Content  string
}

Topic represents a help topic

type TopicManager

type TopicManager struct {
	// contains filtered or unexported fields
}

TopicManager manages help topics for a Cobra application

func New

func New(topicsDir string) *TopicManager

New creates a new TopicManager with default extensions

func NewWithOptions

func NewWithOptions(topicsDir string, opts Options) *TopicManager

NewWithOptions creates a new TopicManager with custom options

func (*TopicManager) DisplayTopicsList

func (tm *TopicManager) DisplayTopicsList()

DisplayTopicsList prints the list of available topics

func (*TopicManager) GetTopic

func (tm *TopicManager) GetTopic(name string) (*Topic, bool)

GetTopic retrieves a topic by name

func (*TopicManager) ListTopics

func (tm *TopicManager) ListTopics() []string

ListTopics returns all available topic names

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL