From a5395a3fa35e398e5824c13e5cc99e2ceda015f8 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 4 Jan 2023 14:26:12 +0100 Subject: [PATCH 1/3] Use koanf getter functions for cleaner code Update comments --- plugin/registry.go | 50 ++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/plugin/registry.go b/plugin/registry.go index 4085e64d..9f1b2875 100644 --- a/plugin/registry.go +++ b/plugin/registry.go @@ -112,40 +112,31 @@ func (reg *RegistryImpl) LoadPlugins(pluginConfig *koanf.Koanf) { }, } - if enabled, ok := pluginConfig.Get(name + ".enabled").(bool); !ok || !enabled { - reg.hooksConfig.Logger.Debug().Str("name", name).Msg("Plugin is disabled or is not set") + // Is the plugin enabled? + plugin.Enabled = pluginConfig.Bool(name + ".enabled") + if !plugin.Enabled { + reg.hooksConfig.Logger.Debug().Str("name", name).Msg("Plugin is disabled") continue - } else { - plugin.Enabled = enabled } - if localPath, ok := pluginConfig.Get( - name + ".localPath").(string); !ok || localPath == "" { + // File path of the plugin on disk. + plugin.LocalPath = pluginConfig.String(name + ".localPath") + if plugin.LocalPath == "" { reg.hooksConfig.Logger.Debug().Str("name", name).Msg( - "Local file of plugin doesn't exist or is not set") + "Local file of the plugin doesn't exist or is not set") continue - } else { - plugin.LocalPath = localPath - } - - if args := pluginConfig.Strings(name + ".args"); len(args) > 0 { - plugin.Args = args - } - - if env := pluginConfig.Strings(name + ".env"); len(env) > 0 { - plugin.Env = append(plugin.Env, env...) } - if checksum, ok := pluginConfig.Get(name + ".checksum").(string); !ok || checksum == "" { + // Checksum of the plugin. + plugin.ID.Checksum = pluginConfig.String(name + ".checksum") + if plugin.ID.Checksum == "" { reg.hooksConfig.Logger.Debug().Str("name", name).Msg( "Checksum of plugin doesn't exist or is not set") continue - } else { - plugin.ID.Checksum = checksum } // Verify the checksum. - // TODO: Load the plugin from a remote location if the checksum doesn't match + // TODO: Load the plugin from a remote location if the checksum didn't match? if sum, err := sha256sum(plugin.LocalPath); err != nil { reg.hooksConfig.Logger.Debug().Err(err).Msg("Failed to calculate checksum") continue @@ -159,9 +150,20 @@ func (reg *RegistryImpl) LoadPlugins(pluginConfig *koanf.Koanf) { continue } - // Plugin priority is determined by the order in which it is listed in the config file - // Built-in plugins are loaded first, followed by user-defined plugins. Built-in plugins - // have a priority of 0 to 999, and user-defined plugins have a priority of 1000 or greater. + // Commandline arguments to pass to the plugin. + if args := pluginConfig.Strings(name + ".args"); len(args) > 0 { + plugin.Args = args + } + + // Custom environment variables to pass to the plugin. + if env := pluginConfig.Strings(name + ".env"); len(env) > 0 { + plugin.Env = append(plugin.Env, env...) + } + + // Plugin priority is determined by the order in which the plugin is listed + // in the config file. Built-in plugins are loaded first, followed by user-defined + // plugins. Built-in plugins have a priority of 0 to 999, and user-defined plugins + // have a priority of 1000 or greater. plugin.Priority = Priority(PluginPriorityStart + uint(priority)) logAdapter := logging.NewHcLogAdapter(®.hooksConfig.Logger, LoggerName) From 80df74e7c1acf614126e007774148d93e188dee7 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 4 Jan 2023 14:27:22 +0100 Subject: [PATCH 2/3] Load requires field from the plugin config --- plugin/registry.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugin/registry.go b/plugin/registry.go index 9f1b2875..915eab81 100644 --- a/plugin/registry.go +++ b/plugin/registry.go @@ -214,6 +214,10 @@ func (reg *RegistryImpl) LoadPlugins(pluginConfig *koanf.Koanf) { plugin.Description = metadata.Fields["description"].GetStringValue() plugin.License = metadata.Fields["license"].GetStringValue() plugin.ProjectURL = metadata.Fields["projectUrl"].GetStringValue() + if err := mapstructure.Decode(metadata.Fields["requires"].GetListValue().AsSlice(), + &plugin.Requires); err != nil { + reg.hooksConfig.Logger.Debug().Err(err).Msg("Failed to decode plugin requirements") + } if err := mapstructure.Decode(metadata.Fields["authors"].GetListValue().AsSlice(), &plugin.Authors); err != nil { reg.hooksConfig.Logger.Debug().Err(err).Msg("Failed to decode plugin authors") From 0c7f409e5d8dc93248c71cfce5caa937857ddd65 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 4 Jan 2023 14:27:39 +0100 Subject: [PATCH 3/3] Add more logs and traces --- plugin/registry.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin/registry.go b/plugin/registry.go index 915eab81..dadba103 100644 --- a/plugin/registry.go +++ b/plugin/registry.go @@ -237,10 +237,13 @@ func (reg *RegistryImpl) LoadPlugins(pluginConfig *koanf.Koanf) { } } + reg.hooksConfig.Logger.Trace().Msgf("Plugin metadata: %+v", plugin) + reg.Add(plugin) + reg.hooksConfig.Logger.Debug().Str("name", plugin.ID.Name).Msg("Plugin metadata loaded") reg.RegisterHooks(plugin.ID) - reg.hooksConfig.Logger.Debug().Str("name", plugin.ID.Name).Msg("Plugin metadata loaded") + reg.hooksConfig.Logger.Debug().Str("name", plugin.ID.Name).Msg("Plugin hooks registered") } }