diff --git a/internal/plugin/manager.go b/internal/plugin/manager.go index 096a9c5..7281de7 100644 --- a/internal/plugin/manager.go +++ b/internal/plugin/manager.go @@ -96,6 +96,7 @@ func NewManager(opts ManagerOptions) (*Manager, error) { supervisors: make(map[string]*SidecarSupervisor), l10nCache: make(map[string]map[string]string), } + reg.ruleSettings = m.GetSettings reg.OnChange(func() { m.mu.Lock() clear(m.l10nCache) diff --git a/internal/plugin/registry.go b/internal/plugin/registry.go index dcdcaec..b37587e 100644 --- a/internal/plugin/registry.go +++ b/internal/plugin/registry.go @@ -98,6 +98,7 @@ type Registry struct { onChange []func() logger func(source, line string) localeFn func() i18n.Locale + ruleSettings func(pluginID, vmName string, maskSecrets bool) map[string]any // Extension point tables. themes map[string]ThemeContribution @@ -626,7 +627,7 @@ func (r *Registry) registerManifestContributions(sc *Scope, m Manifest) { // Register L1 declarative rule hooks on the hook bus. if len(m.Contributes.RuleHooks) > 0 { rules := append([]RuleHook(nil), m.Contributes.RuleHooks...) - r.registerRuleHooks(sc, pid, rules) + r.registerRuleHooks(sc, pid, rules, m.Settings) } // Register declarative services declared in external Manifest.Provides. for _, pr := range m.Provides { diff --git a/internal/plugin/rule_settings_test.go b/internal/plugin/rule_settings_test.go new file mode 100644 index 0000000..4a34062 --- /dev/null +++ b/internal/plugin/rule_settings_test.go @@ -0,0 +1,34 @@ +package plugin + +import ( + "reflect" + "testing" +) + +func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) { + reg := NewRegistry(nil) + reg.ruleSettings = func(pluginID, vmName string, maskSecrets bool) map[string]any { + if pluginID != "local.capture-shortcut" || vmName != "test-vm" || !maskSecrets { + t.Fatalf("unexpected settings lookup: %q %q %v", pluginID, vmName, maskSecrets) + } + return map[string]any{"shortcut": "Ctrl+F12", "autoRelease": false, "token": "private"} + } + intent := StartIntent{PluginID: "local.capture-shortcut"} + schema := &SettingsSchema{Scope: "vm", Properties: map[string]SettingProperty{ + "shortcut": {}, "autoRelease": {}, "token": {Secret: true}, + }} + rule := RuleHook{Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{ + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "shortcut", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "autoRelease", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_TOKEN": "token", + "PATH": "shortcut", + }}}} + reg.applyRuleEnvSettings(&intent, rule, schema, "test-vm") + want := map[string]string{ + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "Ctrl+F12", + "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "false", + } + if !reflect.DeepEqual(intent.Env, want) { + t.Fatalf("unexpected environment: %#v", intent.Env) + } +} diff --git a/internal/plugin/rules.go b/internal/plugin/rules.go index 032a4f4..73b589c 100644 --- a/internal/plugin/rules.go +++ b/internal/plugin/rules.go @@ -16,13 +16,14 @@ type RuleCondition struct { // RuleAction is a constrained action emitted when all conditions match (§5.4). type RuleAction struct { - Kind string `json:"kind"` // "append_args" | "set_env" | "warn" | "post_start" - Key string `json:"key,omitempty"` // fragment key for append_args - Tokens []string `json:"tokens,omitempty"` // QEMU arg tokens - Env map[string]string `json:"env,omitempty"` // env overrides - Message LocalizedText `json:"message,omitempty"` // localized warning text (accepts string or {"zh-CN":..., "en-US":...}) - Action string `json:"action,omitempty"` // post_start kind - Params map[string]any `json:"params,omitempty"` + Kind string `json:"kind"` // "append_args" | "set_env" | "warn" | "post_start" + Key string `json:"key,omitempty"` // fragment key for append_args + Tokens []string `json:"tokens,omitempty"` // QEMU arg tokens + Env map[string]string `json:"env,omitempty"` // env overrides + EnvFromSettings map[string]string `json:"envFromSettings,omitempty"` // environment key to plugin setting + Message LocalizedText `json:"message,omitempty"` // localized warning text (accepts string or {"zh-CN":..., "en-US":...}) + Action string `json:"action,omitempty"` // post_start kind + Params map[string]any `json:"params,omitempty"` } // RuleHook is an L1 declarative hook rule contributed via plugin.json (§5.4). @@ -92,7 +93,7 @@ func factContains(actual, needle any) bool { } } -func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHook) { +func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHook, schema *SettingsSchema) { for _, rule := range rules { rl := rule switch rl.Event { @@ -110,7 +111,9 @@ func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHoo if !EvaluateRuleConditions(rl.Conditions, facts) { return nil, nil } - return buildRuleStartIntent(pluginID, rl, string(r.currentLocale())), nil + intent := buildRuleStartIntent(pluginID, rl, string(r.currentLocale())) + r.applyRuleEnvSettings(&intent, rl, schema, p.VMName) + return intent, nil }, }) case EventHostPreflight: @@ -142,6 +145,39 @@ func (r *Registry) registerRuleHooks(sc *Scope, pluginID string, rules []RuleHoo } } +func (r *Registry) applyRuleEnvSettings(intent *StartIntent, rule RuleHook, schema *SettingsSchema, vmName string) { + if schema == nil || r.ruleSettings == nil { + return + } + for _, action := range rule.Actions { + if action.Kind != "set_env" { + continue + } + for envKey, settingKey := range action.EnvFromSettings { + prop, ok := schema.Properties[settingKey] + if !ok || prop.Secret || !ValidateEnvKey(intent.PluginID, envKey) { + continue + } + scope := prop.Scope + if scope == "" { + scope = schema.Scope + } + settingsScope := "" + if scope == "vm" { + settingsScope = vmName + } + value := r.ruleSettings(intent.PluginID, settingsScope, true)[settingKey] + switch value := value.(type) { + case string, bool: + if intent.Env == nil { + intent.Env = make(map[string]string) + } + intent.Env[envKey] = fmt.Sprint(value) + } + } + } +} + func buildStartFacts(p BeforeStartPayload) map[string]any { facts := make(map[string]any, len(p.HostFacts)+8) for k, v := range p.HostFacts { diff --git a/internal/plugin/types.go b/internal/plugin/types.go index 04ba606..a002fe0 100644 --- a/internal/plugin/types.go +++ b/internal/plugin/types.go @@ -15,7 +15,7 @@ import ( const ( // PluginAPIVersion is the host plugin API contract version (SemVer). - PluginAPIVersion = "1.1.0" + PluginAPIVersion = "1.2.0" // UIAPIVersion is the frontend UI contract version (SemVer); the contract // itself lives in frontend/src/styles/ui-contract.ts (§5.1, §6.3). UIAPIVersion = "2.0.0"