Documentation
¶
Overview ¶
Example ¶
Example demonstrates basic usage of the SQLite help system
package main
import (
"fmt"
"log"
"github.com/go-go-golems/glazed/pkg/help/model"
"github.com/go-go-golems/glazed/pkg/help/store"
)
func main() {
// Create an in-memory help system for demonstration
hs, err := store.NewInMemoryHelpSystem()
if err != nil {
log.Fatal(err)
}
defer func() {
_ = hs.Close()
}()
// Add some example sections
sections := []*model.Section{
{
Slug: "getting-started",
Title: "Getting Started",
SectionType: model.SectionGeneralTopic,
Content: "This section covers the basics of getting started.",
Topics: []string{"basics", "introduction"},
IsTopLevel: true,
ShowPerDefault: true,
Order: 1,
},
{
Slug: "example-hello",
Title: "Hello World Example",
SectionType: model.SectionExample,
Content: "A simple hello world example.",
Topics: []string{"basics", "examples"},
Commands: []string{"hello"},
ShowPerDefault: true,
Order: 2,
},
{
Slug: "advanced-tutorial",
Title: "Advanced Tutorial",
SectionType: model.SectionTutorial,
Content: "Advanced concepts and techniques.",
Topics: []string{"advanced", "tutorial"},
ShowPerDefault: false,
Order: 3,
},
}
// Add sections to the help system
for _, section := range sections {
if err := hs.AddSection(section); err != nil {
log.Fatal(err)
}
}
// Query examples using the predicate system
// 1. Find all top-level sections
topLevel, err := hs.Find(store.IsTopLevel())
if err != nil {
log.Fatal(err)
}
fmt.Printf("Top-level sections: %d\n", len(topLevel))
// 2. Find examples related to "basics"
basicExamples, err := hs.Find(store.And(
store.IsExample(),
store.HasTopic("basics"),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Basic examples: %d\n", len(basicExamples))
// 3. Complex query: Find sections that are either examples OR tutorials AND relate to "basics"
complexQuery, err := hs.Find(store.And(
store.Or(store.IsExample(), store.IsTutorial()),
store.HasTopic("basics"),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Examples or tutorials about basics: %d\n", len(complexQuery))
// 4. Text search (uses LIKE fallback when FTS5 is not available)
searchResults, err := hs.Find(store.TextSearch("hello"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Search results for 'hello': %d\n", len(searchResults))
// 5. Get sections shown by default, ordered by order field
defaultSections, err := hs.Find(store.And(
store.ShownByDefault(),
store.OrderByOrder(),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Default sections: %d\n", len(defaultSections))
// 6. Convenience methods
stats, err := hs.GetStats()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Total sections: %d\n", stats["total"])
}
Output: Top-level sections: 1 Basic examples: 1 Examples or tutorials about basics: 1 Search results for 'hello': 1 Default sections: 2 Total sections: 3
Example (AdvancedQuery) ¶
Example_advancedQuery demonstrates more complex querying capabilities
package main
import (
"fmt"
"log"
"github.com/go-go-golems/glazed/pkg/help/model"
"github.com/go-go-golems/glazed/pkg/help/store"
)
func main() {
hs, err := store.NewInMemoryHelpSystem()
if err != nil {
log.Fatal(err)
}
defer func() {
_ = hs.Close()
}()
// Add test data
sections := []*model.Section{
{
Slug: "cmd-help",
Title: "Command Help",
SectionType: model.SectionExample,
Topics: []string{"commands"},
Commands: []string{"help", "man"},
Flags: []string{"--verbose", "--help"},
Order: 1,
},
{
Slug: "config-tutorial",
Title: "Configuration Tutorial",
SectionType: model.SectionTutorial,
Topics: []string{"configuration", "setup"},
Commands: []string{"config"},
Flags: []string{"--config"},
Order: 2,
},
}
for _, section := range sections {
if err := hs.AddSection(section); err != nil {
log.Fatal(err)
}
}
// Complex query: Find sections that have the "help" command OR the "--help" flag
results, err := hs.Find(store.Or(
store.HasCommand("help"),
store.HasFlag("--help"),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Sections with 'help' command or '--help' flag: %d\n", len(results))
// Query with negation: Find all sections that are NOT tutorials
nonTutorials, err := hs.Find(store.Not(store.IsTutorial()))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Non-tutorial sections: %d\n", len(nonTutorials))
// Pagination example: Get first 1 result, then skip 1 and get next 1
firstResult, err := hs.Find(store.And(
store.OrderByOrder(),
store.Limit(1),
))
if err != nil {
log.Fatal(err)
}
secondResult, err := hs.Find(store.And(
store.OrderByOrder(),
store.Limit(1),
store.Offset(1),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("First result: %s\n", firstResult[0].Slug)
fmt.Printf("Second result: %s\n", secondResult[0].Slug)
}
Output: Sections with 'help' command or '--help' flag: 1 Non-tutorial sections: 1 First result: cmd-help Second result: config-tutorial
Example (LegacyCompatibility) ¶
Example_legacyCompatibility shows how to use the system as a drop-in replacement
package main
import (
"fmt"
"log"
"github.com/go-go-golems/glazed/pkg/help/model"
"github.com/go-go-golems/glazed/pkg/help/store"
)
func main() {
// Create help system (compatible with existing interface)
hs, err := store.NewInMemoryHelpSystem()
if err != nil {
log.Fatal(err)
}
defer func() {
_ = hs.Close()
}()
// Add a section using the legacy-compatible method
section := &model.Section{
Slug: "example-section",
Title: "Example Section",
SectionType: model.SectionExample,
Topics: []string{"examples"},
}
if err := hs.AddSection(section); err != nil {
log.Fatal(err)
}
// Use legacy-compatible methods
retrieved, err := hs.GetSectionWithSlug("example-section")
if err != nil {
log.Fatal(err)
}
count, err := hs.Count()
if err != nil {
log.Fatal(err)
}
// Get examples for a topic (legacy-compatible)
examples, err := hs.GetExamplesForTopic("examples")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Retrieved section: %s\n", retrieved.Title)
fmt.Printf("Total sections: %d\n", count)
fmt.Printf("Examples for 'examples' topic: %d\n", len(examples))
}
Output: Retrieved section: Example Section Total sections: 1 Examples for 'examples' topic: 1
Index ¶
- type HelpSystem
- func (hs *HelpSystem) AddSection(section *model.Section) error
- func (hs *HelpSystem) Clear() error
- func (hs *HelpSystem) Close() error
- func (hs *HelpSystem) Count() (int64, error)
- func (hs *HelpSystem) Find(predicate Predicate) ([]*model.Section, error)
- func (hs *HelpSystem) GetApplicationsForTopic(topic string) ([]*model.Section, error)
- func (hs *HelpSystem) GetDefaultExamplesForTopic(topic string) ([]*model.Section, error)
- func (hs *HelpSystem) GetDefaultTutorialsForTopic(topic string) ([]*model.Section, error)
- func (hs *HelpSystem) GetExamplesForTopic(topic string) ([]*model.Section, error)
- func (hs *HelpSystem) GetSectionWithSlug(slug string) (*model.Section, error)
- func (hs *HelpSystem) GetSections() ([]*model.Section, error)
- func (hs *HelpSystem) GetSectionsByType(sectionType model.SectionType) ([]*model.Section, error)
- func (hs *HelpSystem) GetSectionsForCommand(command string) ([]*model.Section, error)
- func (hs *HelpSystem) GetSectionsForFlag(flag string) ([]*model.Section, error)
- func (hs *HelpSystem) GetStats() (map[string]int64, error)
- func (hs *HelpSystem) GetTopLevelSections() ([]*model.Section, error)
- func (hs *HelpSystem) GetTutorialsForTopic(topic string) ([]*model.Section, error)
- func (hs *HelpSystem) LoadSectionsFromFS(filesystem fs.FS, dir string) error
- func (hs *HelpSystem) Loader() *Loader
- func (hs *HelpSystem) SearchSections(term string) ([]*model.Section, error)
- func (hs *HelpSystem) Store() *Store
- func (hs *HelpSystem) SyncFromFS(filesystem fs.FS, dir string) error
- type Loader
- func (l *Loader) BatchUpsert(ctx context.Context, sections []*model.Section) error
- func (l *Loader) GetSectionStats(ctx context.Context) (map[string]int64, error)
- func (l *Loader) LoadFromFS(ctx context.Context, filesystem fs.FS, rootDir string) error
- func (l *Loader) LoadFromMarkdown(markdownBytes []byte) (*model.Section, error)
- func (l *Loader) LoadSections(ctx context.Context, markdownFiles map[string][]byte) error
- func (l *Loader) SyncFromFS(ctx context.Context, filesystem fs.FS, rootDir string) error
- type Predicate
- func And(predicates ...Predicate) Predicate
- func ContentContains(term string) Predicate
- func DefaultExamplesForTopic(topic string) Predicate
- func DefaultTutorialsForTopic(topic string) Predicate
- func ExamplesForTopic(topic string) Predicate
- func HasCommand(command string) Predicate
- func HasFlag(flag string) Predicate
- func HasTopic(topic string) Predicate
- func IsApplication() Predicate
- func IsExample() Predicate
- func IsGeneralTopic() Predicate
- func IsTemplate() Predicate
- func IsTopLevel() Predicate
- func IsTutorial() Predicate
- func IsType(sectionType model.SectionType) Predicate
- func Limit(limit int) Predicate
- func Not(predicate Predicate) Predicate
- func NotShownByDefault() Predicate
- func Offset(offset int) Predicate
- func Or(predicates ...Predicate) Predicate
- func OrderByCreatedAt() Predicate
- func OrderByOrder() Predicate
- func OrderByTitle() Predicate
- func ShownByDefault() Predicate
- func SlugEquals(slug string) Predicate
- func SlugIn(slugs []string) Predicate
- func TextSearch(term string) Predicate
- func TitleContains(term string) Predicate
- func TopLevelDefaults() Predicate
- func TutorialsForTopic(topic string) Predicate
- type QueryCompiler
- func (qc *QueryCompiler) AddJoin(join string)
- func (qc *QueryCompiler) AddWhere(condition string, args ...interface{})
- func (qc *QueryCompiler) BuildQuery() (string, []interface{})
- func (qc *QueryCompiler) SetLimit(limit int)
- func (qc *QueryCompiler) SetOffset(offset int)
- func (qc *QueryCompiler) SetOrderBy(orderBy string)
- type Store
- func (s *Store) Clear(ctx context.Context) error
- func (s *Store) Close() error
- func (s *Store) Count(ctx context.Context) (int64, error)
- func (s *Store) Delete(ctx context.Context, id int64) error
- func (s *Store) Find(ctx context.Context, predicate Predicate) ([]*model.Section, error)
- func (s *Store) GetByID(ctx context.Context, id int64) (*model.Section, error)
- func (s *Store) GetBySlug(ctx context.Context, slug string) (*model.Section, error)
- func (s *Store) Insert(ctx context.Context, section *model.Section) error
- func (s *Store) List(ctx context.Context, orderBy string) ([]*model.Section, error)
- func (s *Store) Update(ctx context.Context, section *model.Section) error
- func (s *Store) Upsert(ctx context.Context, section *model.Section) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HelpSystem ¶
type HelpSystem struct {
// contains filtered or unexported fields
}
HelpSystem provides a compatibility shim with the existing help system interface
func NewHelpSystem ¶
func NewHelpSystem(dbPath string) (*HelpSystem, error)
NewHelpSystem creates a new HelpSystem with SQLite backing
func NewInMemoryHelpSystem ¶
func NewInMemoryHelpSystem() (*HelpSystem, error)
NewInMemoryHelpSystem creates a new in-memory HelpSystem
func (*HelpSystem) AddSection ¶
func (hs *HelpSystem) AddSection(section *model.Section) error
AddSection adds a single section to the help system
func (*HelpSystem) Clear ¶
func (hs *HelpSystem) Clear() error
Clear removes all sections from the help system
func (*HelpSystem) Close ¶
func (hs *HelpSystem) Close() error
Close closes the underlying database connection
func (*HelpSystem) Count ¶
func (hs *HelpSystem) Count() (int64, error)
Count returns the total number of sections
func (*HelpSystem) Find ¶
func (hs *HelpSystem) Find(predicate Predicate) ([]*model.Section, error)
Find executes a predicate-based query
func (*HelpSystem) GetApplicationsForTopic ¶
func (hs *HelpSystem) GetApplicationsForTopic(topic string) ([]*model.Section, error)
GetApplicationsForTopic returns applications for a specific topic
func (*HelpSystem) GetDefaultExamplesForTopic ¶
func (hs *HelpSystem) GetDefaultExamplesForTopic(topic string) ([]*model.Section, error)
GetDefaultExamplesForTopic returns default examples for a specific topic
func (*HelpSystem) GetDefaultTutorialsForTopic ¶
func (hs *HelpSystem) GetDefaultTutorialsForTopic(topic string) ([]*model.Section, error)
GetDefaultTutorialsForTopic returns default tutorials for a specific topic
func (*HelpSystem) GetExamplesForTopic ¶
func (hs *HelpSystem) GetExamplesForTopic(topic string) ([]*model.Section, error)
GetExamplesForTopic returns examples for a specific topic
func (*HelpSystem) GetSectionWithSlug ¶
func (hs *HelpSystem) GetSectionWithSlug(slug string) (*model.Section, error)
GetSectionWithSlug retrieves a section by its slug
func (*HelpSystem) GetSections ¶
func (hs *HelpSystem) GetSections() ([]*model.Section, error)
GetSections returns all sections (for compatibility with existing interface)
func (*HelpSystem) GetSectionsByType ¶
func (hs *HelpSystem) GetSectionsByType(sectionType model.SectionType) ([]*model.Section, error)
GetSectionsByType returns sections of a specific type
func (*HelpSystem) GetSectionsForCommand ¶
func (hs *HelpSystem) GetSectionsForCommand(command string) ([]*model.Section, error)
GetSectionsForCommand returns sections related to a specific command
func (*HelpSystem) GetSectionsForFlag ¶
func (hs *HelpSystem) GetSectionsForFlag(flag string) ([]*model.Section, error)
GetSectionsForFlag returns sections related to a specific flag
func (*HelpSystem) GetStats ¶
func (hs *HelpSystem) GetStats() (map[string]int64, error)
GetStats returns statistics about the help system
func (*HelpSystem) GetTopLevelSections ¶
func (hs *HelpSystem) GetTopLevelSections() ([]*model.Section, error)
GetTopLevelSections returns all top-level sections
func (*HelpSystem) GetTutorialsForTopic ¶
func (hs *HelpSystem) GetTutorialsForTopic(topic string) ([]*model.Section, error)
GetTutorialsForTopic returns tutorials for a specific topic
func (*HelpSystem) LoadSectionsFromFS ¶
func (hs *HelpSystem) LoadSectionsFromFS(filesystem fs.FS, dir string) error
LoadSectionsFromFS loads sections from a filesystem, compatible with existing interface
func (*HelpSystem) Loader ¶
func (hs *HelpSystem) Loader() *Loader
Loader returns the underlying loader (for advanced usage)
func (*HelpSystem) SearchSections ¶
func (hs *HelpSystem) SearchSections(term string) ([]*model.Section, error)
SearchSections performs a text search across sections
func (*HelpSystem) Store ¶
func (hs *HelpSystem) Store() *Store
Store returns the underlying store (for advanced usage)
func (*HelpSystem) SyncFromFS ¶
func (hs *HelpSystem) SyncFromFS(filesystem fs.FS, dir string) error
SyncFromFS synchronizes the help system with markdown files from a filesystem
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader handles loading and syncing markdown files into the SQLite store
func (*Loader) BatchUpsert ¶
BatchUpsert performs bulk upsert of sections
func (*Loader) GetSectionStats ¶
GetSectionStats returns statistics about the loaded sections
func (*Loader) LoadFromFS ¶
LoadFromFS loads all markdown files from a filesystem into the store
func (*Loader) LoadFromMarkdown ¶
LoadFromMarkdown parses a markdown file and returns a Section
func (*Loader) LoadSections ¶
LoadSections loads multiple sections from markdown bytes
type Predicate ¶
type Predicate func(*QueryCompiler)
Predicate represents a query predicate that can be compiled to SQL
func ContentContains ¶
ContentContains filters sections where content contains the term
func DefaultExamplesForTopic ¶
DefaultExamplesForTopic filters for default examples related to a specific topic
func DefaultTutorialsForTopic ¶
DefaultTutorialsForTopic filters for default tutorials related to a specific topic
func ExamplesForTopic ¶
ExamplesForTopic filters for examples related to a specific topic
func HasCommand ¶
HasCommand filters sections that have a specific command
func IsGeneralTopic ¶
func IsGeneralTopic() Predicate
IsGeneralTopic filters for general topic sections
func NotShownByDefault ¶
func NotShownByDefault() Predicate
NotShownByDefault filters sections that are not shown by default
func OrderByCreatedAt ¶
func OrderByCreatedAt() Predicate
OrderByCreatedAt sorts sections by creation time
func ShownByDefault ¶
func ShownByDefault() Predicate
ShownByDefault filters sections that are shown by default
func SlugEquals ¶
SlugEquals filters sections by exact slug match
func TextSearch ¶
TextSearch performs a fallback text search using LIKE when FTS5 is not available
func TitleContains ¶
TitleContains filters sections where title contains the term
func TopLevelDefaults ¶
func TopLevelDefaults() Predicate
TopLevelDefaults filters for top-level sections shown by default
func TutorialsForTopic ¶
TutorialsForTopic filters for tutorials related to a specific topic
type QueryCompiler ¶
type QueryCompiler struct {
// contains filtered or unexported fields
}
QueryCompiler builds SQL queries from predicates
func NewQueryCompiler ¶
func NewQueryCompiler() *QueryCompiler
NewQueryCompiler creates a new query compiler
func (*QueryCompiler) AddJoin ¶
func (qc *QueryCompiler) AddJoin(join string)
AddJoin adds a JOIN clause
func (*QueryCompiler) AddWhere ¶
func (qc *QueryCompiler) AddWhere(condition string, args ...interface{})
AddWhere adds a WHERE clause condition
func (*QueryCompiler) BuildQuery ¶
func (qc *QueryCompiler) BuildQuery() (string, []interface{})
BuildQuery builds the complete SQL query
func (*QueryCompiler) SetLimit ¶
func (qc *QueryCompiler) SetLimit(limit int)
SetLimit sets the LIMIT clause
func (*QueryCompiler) SetOffset ¶
func (qc *QueryCompiler) SetOffset(offset int)
SetOffset sets the OFFSET clause
func (*QueryCompiler) SetOrderBy ¶
func (qc *QueryCompiler) SetOrderBy(orderBy string)
SetOrderBy sets the ORDER BY clause
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store represents the SQLite-backed help system storage
func NewInMemory ¶
NewInMemory creates a new in-memory SQLite store