-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
109 lines (95 loc) · 3.79 KB
/
Copy pathregistry.go
File metadata and controls
109 lines (95 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package pgxspecial
import (
"context"
"fmt"
"strings"
"github.com/balaji01-4d/pgxspecial/database"
)
// SpecialHandler defines the signature for a special command handler.
//
// A SpecialHandler is invoked with the parsed command arguments and an optional
// verbose flag. The provided db is used to execute queries as needed.
//
// The returned pgx.Rows, if non-nil, is passed back to the caller for consumption.
// Any error returned indicates command execution failure.
type SpecialHandler func(ctx context.Context, db database.Queryer, args string, verbose bool) (SpecialCommandResult, error)
// commandRegistry stores all registered special commands, indexed by command
// name and aliases. Command lookup is performed against this registry during
// special command execution.
var commandRegistry = map[string]SpecialCommand{}
// RegisterCommand registers a special command and its aliases in the command registry.
//
// The command name and aliases are normalized based on the CaseSensitive flag.
// If CaseSensitive is false, command keys are stored in lowercase, making lookup
// case-insensitive.
//
// If multiple aliases are provided, each alias is registered to reference the
// same command definition.
//
// RegisterCommand does not perform validation for duplicate command names or
// aliases; later registrations will overwrite existing entries with the same key.
func RegisterCommand(cmdRegistry SpecialCommandRegistry) {
normalize := func(s string) string {
if cmdRegistry.CaseSensitive {
return s
}
return strings.ToLower(s)
}
cmd := SpecialCommand{
Cmd: cmdRegistry.Cmd,
Description: cmdRegistry.Description,
Syntax: cmdRegistry.Syntax,
CaseSensitive: cmdRegistry.CaseSensitive,
Handler: cmdRegistry.Handler,
}
commandRegistry[normalize(cmdRegistry.Cmd)] = cmd
for _, alias := range cmdRegistry.Alias {
commandRegistry[normalize(alias)] = cmd
}
}
// ExecuteSpecialCommand parses and executes a special command using the command registry.
// Syntax: \command[+] [args]
//
// \command - actual special command
//
// \command[+] - actual special command with verbose mode
//
// A special command is identified by a leading backslash (`\`). If the input does not
// start with a backslash, ExecuteSpecialCommand returns (nil, false, nil) to indicate
// that the input should be treated as a normal query.
//
// The first whitespace-delimited token is treated as the command name. If the command
// name ends with a plus sign (`+`), verbose mode is enabled and the suffix is removed
// before command lookup. The remaining input is passed to the command handler as
// arguments.
//
// The provided Queryer is used by the command handler to execute any required queries.
// Return values:
// - SpecialCommandResult: the result returned by the command handler, if any
// - bool: true if the input was recognized as a special command, even if execution failed
// - error: non-nil if the command is unknown or execution fails
//
// An error is returned if the command is not found in the registry or if the command
// handler returns an error.
func ExecuteSpecialCommand(ctx context.Context, queryer database.Queryer, specialCommand string) (SpecialCommandResult, bool, error) {
if !strings.HasPrefix(specialCommand, "\\") {
return nil, false, nil
}
checkVerbose := func(cmd string) (string, bool) {
suff := "+"
return strings.TrimSuffix(cmd, suff), strings.HasSuffix(cmd, suff)
}
fields := strings.Fields(specialCommand)
cmd := fields[0]
args := strings.TrimSpace(strings.TrimPrefix(specialCommand, cmd))
cmd, verbose := checkVerbose(cmd)
command, ok := commandRegistry[cmd]
if !ok {
return nil, true, fmt.Errorf("Unknown Command: %s", cmd)
}
res, err := command.Handler(ctx, queryer, args, verbose)
if err != nil {
return nil, true, err
}
return res, true, nil
}