Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions plugin/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(&reg.hooksConfig.Logger, LoggerName)
Expand Down Expand Up @@ -212,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")
Expand All @@ -231,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")
}
}

Expand Down