Skip to content

Commit b732b1c

Browse files
committed
create module manager in /interna/ expose and simplify pkg/
1 parent 9cd54f8 commit b732b1c

File tree

6 files changed

+115
-86
lines changed

6 files changed

+115
-86
lines changed

internal/memex/core/types.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,6 @@ type Repository interface {
6666
GetLinks(nodeID string) ([]*Link, error)
6767
DeleteLink(source, target, linkType string) error
6868

69-
// Module operations
70-
ListModules() []Module
71-
GetModule(id string) (Module, bool)
72-
RegisterModule(module Module) error
73-
74-
// Query operations
75-
QueryNodesByModule(moduleID string) ([]*Node, error)
76-
QueryLinksByModule(moduleID string) ([]*Link, error)
77-
7869
// Close closes the repository
7970
Close() error
8071
}

internal/memex/modules.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package memex
2+
3+
import (
4+
"github.com/systemshift/memex/internal/memex/core"
5+
)
6+
7+
var (
8+
// Global module registry
9+
moduleRegistry = NewRegistry()
10+
)
11+
12+
// RegisterModule registers a module globally
13+
func RegisterModule(module core.Module) error {
14+
return moduleRegistry.RegisterModule(module)
15+
}
16+
17+
// GetModule returns a module by ID
18+
func GetModule(id string) (core.Module, bool) {
19+
return moduleRegistry.GetModule(id)
20+
}
21+
22+
// ListModules returns all registered modules
23+
func ListModules() []core.Module {
24+
return moduleRegistry.ListModules()
25+
}
26+
27+
// HandleModuleCommand executes a module command
28+
func HandleModuleCommand(moduleID string, cmd string, args []string, repo core.Repository) error {
29+
return moduleRegistry.HandleCommand(moduleID, cmd, args, repo)
30+
}

internal/memex/registry.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package memex
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
7+
"github.com/systemshift/memex/internal/memex/core"
8+
)
9+
10+
// Registry manages registered modules
11+
type Registry struct {
12+
modules map[string]core.Module
13+
mu sync.RWMutex
14+
}
15+
16+
// NewRegistry creates a new module registry
17+
func NewRegistry() *Registry {
18+
return &Registry{
19+
modules: make(map[string]core.Module),
20+
}
21+
}
22+
23+
// RegisterModule registers a module
24+
func (r *Registry) RegisterModule(module core.Module) error {
25+
r.mu.Lock()
26+
defer r.mu.Unlock()
27+
28+
if _, exists := r.modules[module.ID()]; exists {
29+
return fmt.Errorf("module already registered: %s", module.ID())
30+
}
31+
32+
r.modules[module.ID()] = module
33+
return nil
34+
}
35+
36+
// GetModule returns a module by ID
37+
func (r *Registry) GetModule(id string) (core.Module, bool) {
38+
r.mu.RLock()
39+
defer r.mu.RUnlock()
40+
41+
module, exists := r.modules[id]
42+
return module, exists
43+
}
44+
45+
// ListModules returns all registered modules
46+
func (r *Registry) ListModules() []core.Module {
47+
r.mu.RLock()
48+
defer r.mu.RUnlock()
49+
50+
result := make([]core.Module, 0, len(r.modules))
51+
for _, mod := range r.modules {
52+
result = append(result, mod)
53+
}
54+
return result
55+
}
56+
57+
// HandleCommand executes a module command
58+
func (r *Registry) HandleCommand(moduleID string, cmd string, args []string, repo core.Repository) error {
59+
module, exists := r.GetModule(moduleID)
60+
if !exists {
61+
return fmt.Errorf("module not found: %s", moduleID)
62+
}
63+
64+
// Initialize module with repository if needed
65+
if err := module.Init(repo); err != nil {
66+
return fmt.Errorf("initializing module: %w", err)
67+
}
68+
69+
return module.HandleCommand(cmd, args)
70+
}

pkg/memex/commands.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package memex
22

33
import (
44
"fmt"
5+
56
"github.com/systemshift/memex/internal/memex"
67
)
78

@@ -114,12 +115,7 @@ func (c *Commands) Module(args ...string) error {
114115

115116
// ModuleHelp shows help for a module
116117
func (c *Commands) ModuleHelp(moduleID string) error {
117-
repo, err := memex.GetRepository()
118-
if err != nil {
119-
return fmt.Errorf("getting repository: %w", err)
120-
}
121-
122-
mod, exists := repo.GetModule(moduleID)
118+
mod, exists := memex.GetModule(moduleID)
123119
if !exists {
124120
return fmt.Errorf("module not found: %s", moduleID)
125121
}

pkg/memex/memex.go

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package memex
33
import (
44
"fmt"
55

6+
internalmemex "github.com/systemshift/memex/internal/memex"
67
"github.com/systemshift/memex/internal/memex/core"
78
"github.com/systemshift/memex/internal/memex/repository"
89
)
@@ -111,57 +112,20 @@ func (m *Memex) GetContent(id string) ([]byte, error) {
111112

112113
// RegisterModule registers a new module
113114
func (m *Memex) RegisterModule(mod core.Module) error {
114-
return m.repo.RegisterModule(mod)
115+
return internalmemex.RegisterModule(mod)
115116
}
116117

117118
// GetModule returns a module by ID
118119
func (m *Memex) GetModule(id string) (core.Module, bool) {
119-
return m.repo.GetModule(id)
120+
return internalmemex.GetModule(id)
120121
}
121122

122123
// ListModules returns all registered modules
123124
func (m *Memex) ListModules() []core.Module {
124-
return m.repo.ListModules()
125+
return internalmemex.ListModules()
125126
}
126127

127-
// QueryNodesByModule returns all nodes created by a module
128-
func (m *Memex) QueryNodesByModule(moduleID string) ([]*Node, error) {
129-
nodes, err := m.repo.QueryNodesByModule(moduleID)
130-
if err != nil {
131-
return nil, err
132-
}
133-
134-
result := make([]*Node, len(nodes))
135-
for i, node := range nodes {
136-
result[i] = &Node{
137-
ID: node.ID,
138-
Type: node.Type,
139-
Content: node.Content,
140-
Meta: node.Meta,
141-
Created: node.Created,
142-
Modified: node.Modified,
143-
}
144-
}
145-
return result, nil
146-
}
147-
148-
// QueryLinksByModule returns all links created by a module
149-
func (m *Memex) QueryLinksByModule(moduleID string) ([]*Link, error) {
150-
links, err := m.repo.QueryLinksByModule(moduleID)
151-
if err != nil {
152-
return nil, err
153-
}
154-
155-
result := make([]*Link, len(links))
156-
for i, link := range links {
157-
result[i] = &Link{
158-
Source: link.Source,
159-
Target: link.Target,
160-
Type: link.Type,
161-
Meta: link.Meta,
162-
Created: link.Created,
163-
Modified: link.Modified,
164-
}
165-
}
166-
return result, nil
128+
// HandleModuleCommand executes a module command
129+
func (m *Memex) HandleModuleCommand(moduleID string, cmd string, args []string) error {
130+
return internalmemex.HandleModuleCommand(moduleID, cmd, args, m.repo)
167131
}

pkg/module/module.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ type (
1616
Repository = core.Repository
1717
)
1818

19-
// Common errors
20-
var (
21-
ErrNotInitialized = core.ErrNotInitialized
22-
)
23-
2419
// Base provides a base implementation of Module interface
2520
type Base struct {
2621
id string
@@ -63,29 +58,12 @@ func (b *Base) Init(repo Repository) error {
6358

6459
// Commands returns the list of available commands
6560
func (b *Base) Commands() []Command {
66-
baseCommands := []Command{
67-
{
68-
Name: "help",
69-
Description: "Show module help",
70-
},
71-
{
72-
Name: "version",
73-
Description: "Show module version",
74-
},
75-
}
76-
return append(baseCommands, b.commands...)
61+
return b.commands
7762
}
7863

7964
// HandleCommand handles a module command
8065
func (b *Base) HandleCommand(cmd string, args []string) error {
81-
switch cmd {
82-
case "help":
83-
return nil // Let the CLI handle help
84-
case "version":
85-
return nil // Let the CLI handle version
86-
default:
87-
return fmt.Errorf("unknown command: %s", cmd)
88-
}
66+
return fmt.Errorf("unknown command: %s", cmd)
8967
}
9068

9169
// AddCommand adds a command to the module
@@ -98,31 +76,31 @@ func (b *Base) AddCommand(cmd Command) {
9876
// AddNode adds a node to the repository
9977
func (b *Base) AddNode(content []byte, nodeType string, meta map[string]interface{}) (string, error) {
10078
if b.repo == nil {
101-
return "", ErrNotInitialized
79+
return "", fmt.Errorf("module not initialized")
10280
}
10381
return b.repo.AddNode(content, nodeType, meta)
10482
}
10583

10684
// GetNode gets a node from the repository
10785
func (b *Base) GetNode(id string) (*Node, error) {
10886
if b.repo == nil {
109-
return nil, ErrNotInitialized
87+
return nil, fmt.Errorf("module not initialized")
11088
}
11189
return b.repo.GetNode(id)
11290
}
11391

11492
// AddLink adds a link between nodes
11593
func (b *Base) AddLink(source, target, linkType string, meta map[string]interface{}) error {
11694
if b.repo == nil {
117-
return ErrNotInitialized
95+
return fmt.Errorf("module not initialized")
11896
}
11997
return b.repo.AddLink(source, target, linkType, meta)
12098
}
12199

122100
// GetLinks gets links for a node
123101
func (b *Base) GetLinks(nodeID string) ([]*Link, error) {
124102
if b.repo == nil {
125-
return nil, ErrNotInitialized
103+
return nil, fmt.Errorf("module not initialized")
126104
}
127105
return b.repo.GetLinks(nodeID)
128106
}

0 commit comments

Comments
 (0)