Skip to content

Commit d93818f

Browse files
committed
add repo in sdk and modify commands internal
1 parent 8f4bde7 commit d93818f

File tree

3 files changed

+320
-100
lines changed

3 files changed

+320
-100
lines changed

pkg/sdk/commands.go

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package sdk
22

3-
import "memex/pkg/types"
3+
import (
4+
"fmt"
5+
"memex/pkg/types"
6+
)
47

58
// CommandBuilder helps build commands with a fluent interface
69
type CommandBuilder struct {
@@ -39,48 +42,111 @@ func (b *CommandBuilder) Build() types.Command {
3942
return b.cmd
4043
}
4144

45+
// CommandHandler represents a function that handles a command
46+
type CommandHandler func(repo types.Repository, args []string) error
47+
48+
// CommandSet represents a set of commands with their handlers
49+
type CommandSet struct {
50+
commands map[string]types.Command
51+
handlers map[string]CommandHandler
52+
}
53+
54+
// NewCommandSet creates a new command set
55+
func NewCommandSet() *CommandSet {
56+
return &CommandSet{
57+
commands: make(map[string]types.Command),
58+
handlers: make(map[string]CommandHandler),
59+
}
60+
}
61+
62+
// Add adds a command and its handler to the set
63+
func (cs *CommandSet) Add(cmd types.Command, handler CommandHandler) {
64+
cs.commands[cmd.Name] = cmd
65+
cs.handlers[cmd.Name] = handler
66+
}
67+
68+
// Get returns a command and its handler by name
69+
func (cs *CommandSet) Get(name string) (types.Command, CommandHandler, bool) {
70+
cmd, exists := cs.commands[name]
71+
if !exists {
72+
return types.Command{}, nil, false
73+
}
74+
return cmd, cs.handlers[name], true
75+
}
76+
77+
// List returns all commands in the set
78+
func (cs *CommandSet) List() []types.Command {
79+
cmds := make([]types.Command, 0, len(cs.commands))
80+
for _, cmd := range cs.commands {
81+
cmds = append(cmds, cmd)
82+
}
83+
return cmds
84+
}
85+
86+
// Handle executes a command with the given arguments
87+
func (cs *CommandSet) Handle(repo types.Repository, cmd string, args []string) error {
88+
_, handler, exists := cs.Get(cmd)
89+
if !exists {
90+
return fmt.Errorf("%w: command %s", ErrNotFound, cmd)
91+
}
92+
return handler(repo, args)
93+
}
94+
4295
// Common command patterns
4396

4497
// NewAddCommand creates a standard add command
45-
func NewAddCommand(itemType string) types.Command {
46-
return NewCommand("add").
98+
func NewAddCommand(itemType string, handler CommandHandler) (types.Command, CommandHandler) {
99+
cmd := NewCommand("add").
47100
WithDescription("Add " + itemType).
48101
WithUsage("add <" + itemType + ">").
49102
WithArgs(itemType).
50103
Build()
104+
return cmd, handler
51105
}
52106

53107
// NewListCommand creates a standard list command
54-
func NewListCommand(itemType string) types.Command {
55-
return NewCommand("list").
108+
func NewListCommand(itemType string, handler CommandHandler) (types.Command, CommandHandler) {
109+
cmd := NewCommand("list").
56110
WithDescription("List " + itemType + "s").
57111
WithUsage("list [filter]").
58112
Build()
113+
return cmd, handler
59114
}
60115

61116
// NewRemoveCommand creates a standard remove command
62-
func NewRemoveCommand(itemType string) types.Command {
63-
return NewCommand("remove").
117+
func NewRemoveCommand(itemType string, handler CommandHandler) (types.Command, CommandHandler) {
118+
cmd := NewCommand("remove").
64119
WithDescription("Remove " + itemType).
65120
WithUsage("remove <" + itemType + ">").
66121
WithArgs(itemType).
67122
Build()
123+
return cmd, handler
68124
}
69125

70126
// NewShowCommand creates a standard show command
71-
func NewShowCommand(itemType string) types.Command {
72-
return NewCommand("show").
127+
func NewShowCommand(itemType string, handler CommandHandler) (types.Command, CommandHandler) {
128+
cmd := NewCommand("show").
73129
WithDescription("Show " + itemType + " details").
74130
WithUsage("show <" + itemType + ">").
75131
WithArgs(itemType).
76132
Build()
133+
return cmd, handler
77134
}
78135

79136
// NewUpdateCommand creates a standard update command
80-
func NewUpdateCommand(itemType string) types.Command {
81-
return NewCommand("update").
137+
func NewUpdateCommand(itemType string, handler CommandHandler) (types.Command, CommandHandler) {
138+
cmd := NewCommand("update").
82139
WithDescription("Update " + itemType).
83140
WithUsage("update <" + itemType + "> [options]").
84141
WithArgs(itemType).
85142
Build()
143+
return cmd, handler
144+
}
145+
146+
// ValidateArgs validates command arguments
147+
func ValidateArgs(cmd types.Command, args []string) error {
148+
if len(args) < len(cmd.Args) {
149+
return fmt.Errorf("%w: missing required arguments: %v", ErrInvalidInput, cmd.Args[len(args):])
150+
}
151+
return nil
86152
}

pkg/sdk/repository.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package sdk
2+
3+
import (
4+
"fmt"
5+
6+
"memex/pkg/types"
7+
)
8+
9+
// RepositoryHelper provides common repository operations for modules
10+
type RepositoryHelper struct {
11+
repo types.Repository
12+
}
13+
14+
// NewRepositoryHelper creates a new repository helper
15+
func NewRepositoryHelper(repo types.Repository) *RepositoryHelper {
16+
return &RepositoryHelper{repo: repo}
17+
}
18+
19+
// AddNodeWithMeta adds a node with module metadata
20+
func (h *RepositoryHelper) AddNodeWithMeta(content []byte, nodeType string, moduleID string, meta map[string]interface{}) (string, error) {
21+
if meta == nil {
22+
meta = make(map[string]interface{})
23+
}
24+
meta["module"] = moduleID
25+
return h.repo.AddNode(content, nodeType, meta)
26+
}
27+
28+
// GetModuleNodes returns all nodes created by a module
29+
func (h *RepositoryHelper) GetModuleNodes(moduleID string) ([]*types.Node, error) {
30+
return h.repo.QueryNodesByModule(moduleID)
31+
}
32+
33+
// GetModuleLinks returns all links created by a module
34+
func (h *RepositoryHelper) GetModuleLinks(moduleID string) ([]*types.Link, error) {
35+
return h.repo.QueryLinksByModule(moduleID)
36+
}
37+
38+
// AddModuleLink adds a link with module metadata
39+
func (h *RepositoryHelper) AddModuleLink(source, target, linkType string, moduleID string, meta map[string]interface{}) error {
40+
if meta == nil {
41+
meta = make(map[string]interface{})
42+
}
43+
meta["module"] = moduleID
44+
return h.repo.AddLink(source, target, linkType, meta)
45+
}
46+
47+
// DeleteModuleNodes deletes all nodes created by a module
48+
func (h *RepositoryHelper) DeleteModuleNodes(moduleID string) error {
49+
nodes, err := h.repo.QueryNodesByModule(moduleID)
50+
if err != nil {
51+
return fmt.Errorf("querying module nodes: %w", err)
52+
}
53+
54+
for _, node := range nodes {
55+
if err := h.repo.DeleteNode(node.ID); err != nil {
56+
return fmt.Errorf("deleting node %s: %w", node.ID, err)
57+
}
58+
}
59+
60+
return nil
61+
}
62+
63+
// DeleteModuleLinks deletes all links created by a module
64+
func (h *RepositoryHelper) DeleteModuleLinks(moduleID string) error {
65+
links, err := h.repo.QueryLinksByModule(moduleID)
66+
if err != nil {
67+
return fmt.Errorf("querying module links: %w", err)
68+
}
69+
70+
for _, link := range links {
71+
if err := h.repo.DeleteLink(link.Source, link.Target, link.Type); err != nil {
72+
return fmt.Errorf("deleting link %s -> %s: %w", link.Source, link.Target, err)
73+
}
74+
}
75+
76+
return nil
77+
}
78+
79+
// CleanupModule removes all nodes and links created by a module
80+
func (h *RepositoryHelper) CleanupModule(moduleID string) error {
81+
if err := h.DeleteModuleLinks(moduleID); err != nil {
82+
return fmt.Errorf("deleting module links: %w", err)
83+
}
84+
85+
if err := h.DeleteModuleNodes(moduleID); err != nil {
86+
return fmt.Errorf("deleting module nodes: %w", err)
87+
}
88+
89+
return nil
90+
}

0 commit comments

Comments
 (0)