discover

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

README

pkg/discover

Package: github.com/quadroops/goplugin/pkg/discover

We need a ways to discover our registered and available plugins.

  1. We are using single file configuration. And using toml as main configuration's format
  2. This configuration's file should be located at user's home dir, such as: /home/my/.goplugin/config.toml
  3. User able to customize this configuration filepath using environment variable: GOPLUGIN_DIR

Package's functionalities:

Explore

  • Used to discover "quadroops goplugins's path".
  • Check if envvar GOPLUGIN_DIR is exist, if exist then read the config's file from the path, if config file not exist then throw a panic error.
  • If envvar not exist, then by default try to seek user's home dir like /home/my/.goplugin and read the config path from there, if still not exist then throw a panic error.
  • If all process success, then should be return a config filepath (string)

Parser

  • Used to parse configuration from config file and return a PluginConfig

Toml Configuration Values


[meta]
version = "1.0.0"
author = "hiraq|hiraq.dev@gmail.com"
contributors = [
    "a|a@test.com",
    "b|b@test.com
]

# global configurations
[settings]
debug = true 

# Used as main plugin registries
#
# a plugin should provide basic five informations about their self
#
# - Author
# - Md5Sum.  To make sure that we should not be able to exec a plugin which will harm us
# - Exec path
# - Exec start time. Used to wait a plugin to start processes until they are ready to consume by caller
# - Communication type (grpc/rest)
#
# Each of registered plugin MUST have a unique's name
[plugins]

    [plugins.name_1]
    author = "author_1|author_1@gmail.com"
    md5 = "d194b7bad208c2ddfa0ef597fd4abcc5"
    exec = "/path/to/exec"
    exec_args = ["--port", "8080"]
    exec_file = "/path/to/exec"
    exec_time = 5
    comm_type = "grpc"
    comm_port = "8080"
    
    [plugins.name_2]
    author = "author_2|author_2@gmail.com"
    md5 = "d194b7bad208c2ddfa0ef597fd4abcc5"
    exec = "/path/to/exec"
    exec_args = ["--port", "8080"]
    exec_file = "/path/to/exec"
    exec_time = 10
    comm_type = "rest"
    comm_port = "8081"
    
    [plugins.name_3]
    author = "author_3|author_3@gmail.com"
    md5 = "d194b7bad208c2ddfa0ef597fd4abcc5"
    exec = "/path/to/exec"
    exec_args = ["--port", "8080"]
    exec_file = "/path/to/exec"
    exec_time = 20
    comm_type = "nano"
    comm_port = "8082"

# Used as service registries
# A service is an application that consume / using plugins
# They are not allowed to access a plugin that not registered to service's plugin registries
[hosts]

    [hosts.host_1]
    plugins = ["name_1", "name_2"]
    
    [hosts.host_2]
    plugins = ["name_3"]

Usages

import (
	"github.com/quadroops/goplugin/pkg/discover"
	"github.com/quadroops/goplugin/pkg/discover/driver"
)

d := discover.NewConfigChecker(driver.NewOSChecker(), driver.NewDefaultChecker())
conf, err := d.Explore()
if err != nil {
    // error handling 
}

parser := discover.NewConfigParser(driver.NewTomlParser(), driver.NewFileReader())

// will return *discover.PluginConfig if success no error
config, err := parser.Load(conf)
if err != nil {
    // error handling
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	// Check should return a string of config file path
	// and just throw a panic if current file not exist
	Check() string
}

Checker is basic interface and should be implemented by any objects that want to check config file path

type ConfigChecker

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

ConfigChecker used as main struct to explore config file path

func NewConfigChecker

func NewConfigChecker(checkers ...Checker) *ConfigChecker

NewConfigChecker used to create new instance of ConfigChecker

func (*ConfigChecker) Explore

func (cc *ConfigChecker) Explore() (string, error)

Explore used to explore configuration's file from os or default home dir

type ConfigParser

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

ConfigParser used to parse configuration values from given filepath

func NewConfigParser

func NewConfigParser(parser Parser, reader SourceReader) *ConfigParser

NewConfigParser used to create new instance of ConfigParser

func (*ConfigParser) Load

func (cp *ConfigParser) Load(confpath string) (*PluginConfig, error)

Load used to read given config's file, get the content bytes and parse the data

type Host

type Host struct {
	Name   string
	Config PluginConfig
}

Host used as main host's configurations

type Parser

type Parser interface {
	Parse(content []byte) (*PluginConfig, error)
}

Parser is an interface to abstract main function to parse toml's file

type PluginConfig

type PluginConfig struct {
	Meta     PluginMeta            `toml:"meta"`
	Settings PluginSettings        `toml:"settings"`
	Plugins  map[string]PluginInfo `toml:"plugins"`
	Hosts    map[string]PluginHost `toml:"hosts"`
}

PluginConfig used to store all plugin configuration values

type PluginHost

type PluginHost struct {
	Plugins []string `toml:"plugins"`
}

PluginHost used to save all registered service's plugins

type PluginInfo

type PluginInfo struct {
	Author       string   `toml:"author"`
	MD5          string   `toml:"md5"`
	Exec         string   `toml:"exec"`
	ExecArgs     []string `toml:"exec_args"`
	ExecFile     string   `toml:"exec_file"`
	ExecTime     int      `toml:"exec_time"`
	ProtocolType string   `toml:"comm_type"`
}

PluginInfo used to save all plugin's basic informations

type PluginInit

type PluginInit struct {
	SvcName        string
	ConfigFilePath string
}

PluginInit used to save initialize states

type PluginMeta

type PluginMeta struct {
	Version      string   `toml:"version"`
	Author       string   `toml:"author"`
	Contributors []string `toml:"contributors"`
}

PluginMeta used to save all [meta] informations

type PluginSettings

type PluginSettings struct {
	Debug bool `toml:"debug"`
}

PluginSettings used to save all global [settings] informations

type SourceReader

type SourceReader interface {
	Read(sourceAddr string) ([]byte, error)
}

SourceReader is an interface to abstract config reader activity

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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