registry

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 7 Imported by: 1

README

Plugin Registry for Golang

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Install and manage plugins for Golang application.

Prerequisites

  • Go >= 1.15

Install

go get github.com/nhatthm/plugin-registry

Usage

plugin-registry helps to install plugins to a container at your choice. There are 4 tasks to manage them:

  • Install
  • Uninstall
  • Enable
  • Disable

plugin-registry is backed by spf13/afero so feel free to use it with your favorite backend file system by using WithFs(fs afero.Fs) option. For example

package mypackage

import (
	registry "github.com/nhatthm/plugin-registry"
	_ "github.com/nhatthm/plugin-registry-github" // Add github installer.
	"github.com/spf13/afero"
)

func createRegistry() (registry.Registry, error) {
	return registry.NewRegistry("~/plugins", registry.WithFs(afero.NewMemMapFs()))
}

By default, plugin-registry will record the installed plugin in config.yaml file in the given container, you can change it to a new place of your choice by using WithConfigFile(path string) option, for example:

package mypackage

import (
	registry "github.com/nhatthm/plugin-registry"
	_ "github.com/nhatthm/plugin-registry-github" // Add github installer.
)

func createRegistry() (registry.Registry, error) {
	return registry.NewRegistry("/usr/local/bin/plugins", registry.WithConfigFile("~/plugins/config.yaml"))
}

If you want to manage the plugins differently, you can write your own Configurator and use WithConfigurator() option to set it, for example:

package mypackage

import (
	registry "github.com/nhatthm/plugin-registry"
	_ "github.com/nhatthm/plugin-registry-github" // Add github installer.
	"github.com/nhatthm/plugin-registry/config"
)

var _ config.Configurator = (*MyConfigurator)(nil)

type MyConfigurator struct{}

func createConfigurator() *MyConfigurator {
	var c MyConfigurator

	// init c.

	return &c
}

func createRegistry() (registry.Registry, error) {
	return registry.NewRegistry("/usr/local/bin/plugins", registry.WithConfigurator(createConfigurator()))
}

Installer

There is no installer provided by this library, you need to install and import it in your project.

Known 3rd party installers:

Examples

package mypackage

import (
	"context"

	registry "github.com/nhatthm/plugin-registry"
	_ "github.com/nhatthm/plugin-registry-github" // Add github installer.
)

var defaultRegistry = mustCreateRegistry()

func mustCreateRegistry() registry.Registry {
	r, err := createRegistry()
	if err != nil {
		panic(err)
	}

	return r
}

func createRegistry() (registry.Registry, error) {
	return registry.NewRegistry("~/plugins")
}

func installPlugin(source string) error {
	return defaultRegistry.Install(context.Background(), source)
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package registry provides plugins functionalities for cobra cli application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FsRegistry

type FsRegistry struct {
	// contains filtered or unexported fields
}

FsRegistry is a file system plugin registry.

func NewRegistry

func NewRegistry(path string, options ...Option) (*FsRegistry, error)

NewRegistry initiates a new plugin registry.

func (*FsRegistry) Config

func (r *FsRegistry) Config() (config.Configuration, error)

Config returns the configuration of the registry.

func (*FsRegistry) Disable

func (r *FsRegistry) Disable(name string) error

Disable disables a plugin by name.

func (*FsRegistry) Enable

func (r *FsRegistry) Enable(name string) error

Enable enabled a plugin by name.

func (*FsRegistry) GetPlugin added in v0.1.4

func (r *FsRegistry) GetPlugin(name string) (*plugin.Plugin, error)

GetPlugin gets plugin by name.

func (*FsRegistry) Install

func (r *FsRegistry) Install(ctx context.Context, src string) error

Install installs plugin from a url.

func (*FsRegistry) Uninstall

func (r *FsRegistry) Uninstall(name string) error

Uninstall uninstalls a plugin.

type Option

type Option func(r *FsRegistry)

Option configures Registry.

func WithConfigFile

func WithConfigFile(configFile string) Option

WithConfigFile sets config file location.

func WithConfigurator

func WithConfigurator(c config.Configurator) Option

WithConfigurator sets configurator.

func WithFs

func WithFs(fs afero.Fs) Option

WithFs sets filesystem.

type Registry

type Registry interface {
	Config() (config.Configuration, error)
	Enable(name string) error
	Disable(name string) error
	Install(ctx context.Context, src string) error
	Uninstall(name string) error
}

Registry is a plugin registry.

Directories

Path Synopsis
Package config provides functionalities for configuring the plugins.
Package config provides functionalities for configuring the plugins.
Package context provides context-services.
Package context provides context-services.
Package installer provides contracts to define a new installer.
Package installer provides contracts to define a new installer.
mock
configurator
Package configurator provide New functionalities for configurator.
Package configurator provide New functionalities for configurator.
installer
Package installer provides mock for installer.
Package installer provides mock for installer.
registry
Package registry provides mock for Registry.
Package registry provides mock for Registry.
Package plugin provides functionalities for reading plugin configuration.
Package plugin provides functionalities for reading plugin configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL