* dws event * fix consume fail * test: add stream ticket injection probe * feat: add portal ticket stream mode * user event * fix: allow portal ticket normal without app secret * event * user event * eventType filter * refactor(event): 优化IPC端点路径处理和改进相关测试 - 用dwsevent.IPCEndpoint替代原先根据GOOS判断的路径逻辑 - 新增event包实现Unix socket路径长度限制及长路径fallback机制 - 添加endpoint_test.go覆盖路径短长及唯一性的单元测试 - 修改busctl模块使用统一的IPC端点获取方法,避免重复实现 - transport_unix.go新增checkSocketPath函数检查路径长度,防止EINVAL错误 - 在监听和连接Unix socket时加入路径限制检查,提升错误明晰度 - 去除多个文件中无用的runtime导入,简化代码依赖 * opt * event skill * default value * install script event * fix: remove subscribe id event fanout filter * fix(personal): 修正指定发送人消息描述错误 * more im event * filter subId * fix: align personal event schema with stream payload * fix: avoid duplicate app helper name * feat: simplify personal event schemas * feat: simplify event schema output * docs: refine dingtalk event skill references * feat: align personal event consume flags * fix event stop and status visibility * hide app event public entrypoints * hide incomplete personal sender event * remove external event reference comments * chore(event): prepare official release * fix(event): harden personal stream lifecycle --------- Co-authored-by: 玉澜 <yulan.wqy@alibaba-inc.com> Co-authored-by: zhengyubai <zhengyubai618@gmail.com>
122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
// Copyright 2026 Alibaba Group
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package consume
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/event/transport"
|
|
)
|
|
|
|
// Route describes one --route rule. The CLI accepts the wire form
|
|
// `<regex>=dir:<path>`; ParseRoute compiles regex once at startup so the
|
|
// hot path is just a Match.
|
|
//
|
|
// Pattern matches against event.EventType (NOT the whole event JSON).
|
|
// The first matching rule in CLI order wins; unmatched events fall through
|
|
// to the default sink (stdout or --output-dir).
|
|
type Route struct {
|
|
Pattern *regexp.Regexp
|
|
Dir string
|
|
// Raw is the original CLI spec; preserved for status / debug output.
|
|
Raw string
|
|
}
|
|
|
|
// ParseRoute parses one `<regex>=dir:<path>` spec. Returns a typed error
|
|
// for bad inputs so the CLI can render a clear "did you mean" message.
|
|
//
|
|
// Wire grammar:
|
|
//
|
|
// spec = regex "=dir:" path
|
|
// regex = any chars except literal "=" (use \= to escape) (v1: no escape)
|
|
// path = any string (no validation here; sink validates at write time)
|
|
//
|
|
// Examples:
|
|
//
|
|
// "^im\\.message=dir:./im/"
|
|
// "^approval\\.=dir:./approval/"
|
|
func ParseRoute(spec string) (Route, error) {
|
|
if spec == "" {
|
|
return Route{}, fmt.Errorf("consume: empty route spec")
|
|
}
|
|
// v1 grammar is intentionally rigid: split on the first "=dir:".
|
|
// Earlier proposals supported other sink kinds (=file: / =mcp:), but
|
|
// the cobra layer rejects those — keep parsing tight here too.
|
|
const sep = "=dir:"
|
|
idx := strings.Index(spec, sep)
|
|
if idx <= 0 || idx == len(spec)-len(sep) {
|
|
return Route{}, fmt.Errorf("consume: route spec must be '<regex>=dir:<path>', got %q", spec)
|
|
}
|
|
pattern := spec[:idx]
|
|
path := spec[idx+len(sep):]
|
|
re, err := regexp.Compile(pattern)
|
|
if err != nil {
|
|
return Route{}, fmt.Errorf("consume: route regex %q: %w", pattern, err)
|
|
}
|
|
if path == "" {
|
|
return Route{}, fmt.Errorf("consume: route path is empty in %q", spec)
|
|
}
|
|
return Route{Pattern: re, Dir: path, Raw: spec}, nil
|
|
}
|
|
|
|
// ParseRoutes parses many specs in CLI order. On any parse failure returns
|
|
// the partial parse so far and the error — the caller decides whether to
|
|
// continue. (The cobra layer treats any parse error as fatal validation.)
|
|
func ParseRoutes(specs []string) ([]Route, error) {
|
|
out := make([]Route, 0, len(specs))
|
|
for _, s := range specs {
|
|
r, err := ParseRoute(s)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Router decides which sink an event goes to. Match returns the directory
|
|
// of the first matching Route, or empty string when no rule matches (fall
|
|
// through to default sink).
|
|
type Router struct {
|
|
rules []Route
|
|
}
|
|
|
|
// NewRouter constructs a router from pre-parsed rules.
|
|
func NewRouter(rules []Route) *Router { return &Router{rules: rules} }
|
|
|
|
// Match returns the destination directory for the event's type, or "" if
|
|
// no rule matched. Iterates rules in CLI order (first match wins).
|
|
func (r *Router) Match(ev transport.Event) string {
|
|
if r == nil {
|
|
return ""
|
|
}
|
|
for _, rule := range r.rules {
|
|
if rule.Pattern.MatchString(ev.EventType) {
|
|
return rule.Dir
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Rules returns the parsed routes for status / debug output. Caller MUST
|
|
// NOT mutate the returned slice.
|
|
func (r *Router) Rules() []Route {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
return r.rules
|
|
}
|