Architecture and Design

Plug-in System Overview

The oglr project employs a plug-in system to enable extensibility and modularity. This system allows developers to create custom components, called “plug-ins,” that can be dynamically loaded and integrated into the core application.

Plug-in Structure

A plug-in is essentially a Go package that conforms to a specific interface. This interface defines the essential methods that a plug-in must implement.

// A Plug-in is an interface that defines the basic functionality
          // of a plug-in.
          type PlugIn interface {
              // Name returns the name of the plug-in.
              Name() string
          
              // Description returns a short description of the plug-in.
              Description() string
          
              // Version returns the version of the plug-in.
              Version() string
          
              // Init initializes the plug-in.
              // This method is called when the plug-in is loaded.
              Init() error
          }
          

Plug-in Registration

The oglr system utilizes a central registry to manage the registration and discovery of plug-ins. When the application starts, it scans for available plug-in packages and registers them with the registry.

  • The plugin/manager/manager.go file contains the core logic for managing the plug-in registry.
  • The Register function in manager.go handles the process of registering new plug-ins.
// Register registers a new plug-in.
          func Register(name string, plugin PlugIn) error {
              // ...
              return nil
          }
          

Plug-in Loading and Execution

  • The plugin/manager/manager.go file implements the loading and execution of plug-ins.
  • The Load function, within manager.go, loads a specific plug-in by name, instantiates it, and calls its Init method to initialize the plug-in.
// Load loads a plug-in by name.
          func Load(name string) (PlugIn, error) {
              // ...
              return nil, nil
          }
          

Creating New Plug-ins

To create a new plug-in, follow these steps:

  1. Create a new Go package: The package name should be descriptive and unique.
  2. Implement the PlugIn interface: Define the methods required by the interface (Name, Description, Version, Init).
  3. Register the plug-in: In your plug-in package, call the Register function from the manager.go file.
// myplugin/myplugin.go
          package myplugin
          
          import (
              "github.com/stevedunn/oglr/plugin/manager"
          )
          
          // MyPlugin is a sample plug-in.
          type MyPlugin struct{}
          
          func (p *MyPlugin) Name() string {
              return "MyPlugin"
          }
          
          // ... other interface methods
          
          func init() {
              manager.Register("myplugin", &MyPlugin{})
          }
          

Example Plug-in

The oglr project includes a sample plug-in demonstrating the basic structure:

  • The plugin/sample/sample.go file provides an example plug-in implementation.
// sample/sample.go
          package sample
          
          // ... implementation of the PlugIn interface
          

Benefits of the Plug-in System

  • Extensibility: Allows developers to extend the functionality of the core application without modifying its source code.
  • Modularity: Encapsulates functionality into independent, reusable components.
  • Ease of Development: Simplifies the development process by breaking down complex applications into smaller, manageable units.
  • Customization: Enables users to tailor the application to their specific needs.