-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugins.go
50 lines (43 loc) · 2.15 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
)
// A CheckenvProvider is a function mapping an input string to a map[string]string representing environment
// variables and their values.
// The input string represents a filter specific to the provider in question. Different providers may
// implement filters using different syntaxes. Each provider has to provide information about its filtering
// syntax when it registers itself.
// It can also return an error if there was any kind of issue retrieving the requested environment variables.
type CheckenvProvider func(string) (map[string]string, error)
// InitFromEnvFunction is a function which initializes a CheckenvProvider from the checkenv runtime environment.
// This is useful for providers that describe environment configurations in remote environments like
// via SSH or on cloud configuration stores.
type InitFromEnvFunction func() error
// noop is an InitFromEnvFunction with no effect. It returns nil.
func noop() error {
return nil
}
// CheckenvPlugin represents all the metadata that checkenv requires about a registered plugin:
// 1. A Help string to display to users explaining the plugins filter syntax, how it is initialized, and possibly more.
// 2. An Init function which initializes the plugin when checkenv starts up.
// 3. The Provider function responsible for providing environment configuration from the source that the plugin represents.
type CheckenvPlugin struct {
Help string
Init InitFromEnvFunction
Provider CheckenvProvider
}
// RegisteredPlugins maps plugin names to the actual plugins. checkenv uses this map at runtime to
// understand what environments a user wants to check/compare.
var RegisteredPlugins map[string]CheckenvPlugin = make(map[string]CheckenvPlugin)
// RegisterPlugin is the function that each plugin must call to provide its functionality to checkenv users.
func RegisterPlugin(name string, help string, init InitFromEnvFunction, provider CheckenvProvider) {
if _, ok := RegisteredPlugins[name]; ok {
panic(fmt.Sprintf("A plugin already exists with name: %s", name))
}
plugin := CheckenvPlugin{
Help: help,
Init: init,
Provider: provider,
}
RegisteredPlugins[name] = plugin
}