mirror of
https://github.com/larksuite/cli.git
synced 2026-09-14 18:42:53 +08:00
2b4c6349a1
Replace every command-facing error path in the event domain — the consume/schema command layer, the +subscribe shortcut, EventKey definitions, and the consume orchestration — with typed errs.* envelopes, so consumers get stable type, subtype, param, hint, and missing_scopes metadata for classification and recovery instead of free-form message text. - Input validation (--jq, --param, --output-dir, --filter, --route, unknown EventKey, EventKey params) reports validation / invalid_argument with the offending flag in param and an actionable hint. - Scope preflight reports authorization / missing_scope with the machine-readable missing_scopes list; console-subscription and single-bus preconditions report failed_precondition with recovery hints. - The consume API boundary passes already-typed errors through and classifies transport, non-JSON HTTP, and unparsable responses; the vc note-detail retry now matches the not-found code on typed errors (it silently never fired against the legacy envelope shape). - Previously-bare failures exited 1 with a plain-text "Error:" line and now exit with their category code (validation 2, auth 3, network 4, internal 5) alongside the typed stderr envelope. - forbidigo and errscontract guards now cover the event paths so regressions fail lint; AGENTS.md and the lark-event skill document the typed contract for agent consumers. Validation: make unit-test (race) green; event unit and e2e suites assert category/subtype/param/hint and cause preservation against the real binary; errscontract and golangci lint clean.
76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package event
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/larksuite/cli/internal/validate"
|
|
)
|
|
|
|
// Route holds a compiled regex pattern and its target output directory.
|
|
type Route struct {
|
|
pattern *regexp.Regexp
|
|
dir string
|
|
}
|
|
|
|
// EventRouter dispatches events to output directories by regex matching on event_type.
|
|
type EventRouter struct {
|
|
routes []Route
|
|
}
|
|
|
|
// ParseRoutes parses route flag values into an EventRouter.
|
|
// Format: "regex=dir:./path/to/dir"
|
|
// Returns nil, nil when input is empty.
|
|
func ParseRoutes(specs []string) (*EventRouter, error) {
|
|
if len(specs) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
routes := make([]Route, 0, len(specs))
|
|
for _, spec := range specs {
|
|
parts := strings.SplitN(spec, "=", 2)
|
|
if len(parts) != 2 {
|
|
return nil, eventValidationParamError("--route", "invalid --route %q: expected format regex=dir:./path", spec)
|
|
}
|
|
pattern := parts[0]
|
|
target := parts[1]
|
|
|
|
re, err := regexp.Compile(pattern)
|
|
if err != nil {
|
|
return nil, eventValidationParamErrorWithCause(err, "--route", "invalid regex in --route %q", spec)
|
|
}
|
|
|
|
if !strings.HasPrefix(target, "dir:") {
|
|
return nil, eventValidationParamError("--route", "invalid --route target %q: must start with \"dir:\" prefix (format: regex=dir:./path)", target)
|
|
}
|
|
dir := strings.TrimPrefix(target, "dir:")
|
|
if dir == "" {
|
|
return nil, eventValidationParamError("--route", "invalid --route %q: directory path is empty", spec)
|
|
}
|
|
|
|
safeDir, err := validate.SafeOutputPath(dir)
|
|
if err != nil {
|
|
return nil, eventValidationParamErrorWithCause(err, "--route", "invalid --route %q", spec)
|
|
}
|
|
|
|
routes = append(routes, Route{pattern: re, dir: safeDir})
|
|
}
|
|
|
|
return &EventRouter{routes: routes}, nil
|
|
}
|
|
|
|
// Match returns all target directories for the given event type.
|
|
// Returns nil if no routes match (caller should fall through to default output).
|
|
func (r *EventRouter) Match(eventType string) []string {
|
|
var dirs []string
|
|
for _, route := range r.routes {
|
|
if route.pattern.MatchString(eventType) {
|
|
dirs = append(dirs, route.dir)
|
|
}
|
|
}
|
|
return dirs
|
|
}
|