environment

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

About

This package is a library of allan577 project. This library provides the function of managing the runtime environment for the application. Generally, no additional work is needed to realize the type of runtime environment shared between the various components of the application.

Why?

Each component in an application usually needs to have different behaviors (such as more detailed log output) in different runtime environments. However, it is quite tedious for each component to maintain a set of runtime environment. In some large-scale applications, it is impossible to do so. This library is to solve this problem. Let's manage the runtime environment more conveniently within the application.

Install

go get -u -v gitee.com/allan577/go-lib-environment

Usage

package main
        
import (
    "gitee.com/allan577/go-lib-environment"
)

func main() {
    // Get the current runtime environment.
    environment.Get() 

    // Set the runtime environment value.
    // If the given runtime environment is not supported, ErrInvalidEnv error is returned.
    // If the current runtime environment is locked, ErrLocked error is returned.
    // environment.Development  // "development"
    // environment.Testing      // "testing"
    // environment.Prerelease   // "prerelease"
    // environment.Production   // "production"
    environment.Set(environment.Testing)

    // Register functions can register any custom runtime environment.
    // Note: The Register method must be invoked before calling the Set method.
    environment.Register("foo")
    environment.Registered("foo") // true

    // Lock locks the current runtime environment.
    // After locking, the current runtime environment cannot be changed.
    environment.Lock()
    // SetAndLock sets and locks the current runtime environment.
    // If the runtime environment settings fail, they are not locked.
    environment.SetAndLock(environment.Testing)

    // Listen adds a given runtime environment listener.
    // When the runtime environment changes, all registered listeners will be executed.
    environment.Listen(func(after, before environment.Env) {
        // Do something! 
    })

    // New creates and returns a new instance of the runtime environment manager.
    // The default runtime environment is Development, and all built-in runtime environments
    // have been registered.
    // The runtime environment manager has all the functions and methods of the same name!
    manager := environment.New()

    // NewEmpty creates and returns an empty instance of the runtime environment manager.
    // The manager returned by this function does not register any runtime environment,
    // and the current runtime environment is empty.
    empty := environment.NewEmpty()
}

License

Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidEnv represents that the given runtime environment is not
	// registered or supported.
	ErrInvalidEnv = errors.New("environment: invalid env")

	// ErrLocked indicates that the current runtime environment is locked
	// and cannot be changed.
	ErrLocked = errors.New("environment: locked")
)

Functions

func In

func In(envs []Env) bool

In returns whether the current runtime environment is in the given runtime environment list.

func Is

func Is(env Env) bool

Is returns whether the given runtime environment is equal to the current runtime environment.

func Listen

func Listen(listener Listener)

Listen adds a given runtime environment listener. If the given listener is nil, ignore it.

func Lock

func Lock()

Lock locks the current runtime environment. After locking, the current runtime environment cannot be changed.

func Locked

func Locked() bool

Locked returns whether the current runtime environment is locked.

func Register

func Register(env Env)

Register registers a custom runtime environment. If you want to add a custom environment, this method must be called before the Set() method.

func Registered

func Registered(env Env) bool

Registered determines whether the given runtime environment is already registered.

func Set

func Set(env Env) error

Set sets the current runtime environment. If the given runtime environment is not supported, ErrInvalidEnv error is returned. If the current runtime environment is locked, ErrLocked error is returned.

func SetAndLock

func SetAndLock(env Env) error

SetAndLock sets and locks the current runtime environment. If the runtime environment settings fail, they are not locked.

Types

type Env

type Env string

Env type defines the runtime environment.

const (
	// Development is the development environment, which is also the default
	// runtime environment.
	Development Env = "development"

	// Alpha is a alpha environment, usually used for initial quality acceptance.
	Alpha Env = "alpha"

	// Testing is a test environment, usually used for initial quality acceptance.
	Testing Env = "testing"

	// Prerelease is a pre release environment, usually used for grayscale
	// testing or quality acceptance.
	Prerelease Env = "prerelease"

	// Production is the production environment and the final deployment
	// environment of the application.
	Production Env = "production"
)

func Get

func Get() Env

Get returns the current runtime environment.

func (Env) In

func (e Env) In(envs []Env) bool

In method returns whether the current runtime environment is in the given runtime environment list.

func (Env) Is

func (e Env) Is(env Env) bool

Is method returns whether the given runtime environment is equal to the current runtime environment.

func (Env) String

func (e Env) String() string

String method returns the current runtime environment string.

type Listener

type Listener func(after, before Env)

Listener defines the runtime environment listener. Listeners are used to receive notifications when the runtime environment changes.

func UnListen

func UnListen() Listener

UnListen removes and returns to the recently added listener. If there is no listener to be removed, nil is returned.

func UnListenAll

func UnListenAll() []Listener

UnListenAll removes and returns all added listeners. If there is no listener to be removed, nil is returned.

type Manager

type Manager interface {
	// Get method returns the current runtime environment.
	Get() Env

	// Is returns whether the given runtime environment is equal to the
	// current runtime environment.
	Is(Env) bool

	// In returns whether the current runtime environment is in the given
	// runtime environment list.
	In(envs []Env) bool

	// Register method registers a custom runtime environment.
	// If you want to add a custom environment, this method must be called
	// before the Manager.Set() method.
	Register(Env)

	// Registered determines whether the given runtime environment is already registered.
	Registered(Env) bool

	// Lock method locks the current runtime environment.
	// After locking, the current runtime environment cannot be changed.
	Lock()

	// Locked method returns whether the current runtime environment is locked.
	Locked() bool

	// Set method sets the current runtime environment.
	// If the given runtime environment is not supported, ErrInvalidEnv error is returned.
	// If the current runtime environment is locked, ErrLocked error is returned.
	Set(Env) error

	// SetAndLock method sets and locks the current runtime environment.
	// If the runtime environment settings fail, they are not locked.
	SetAndLock(Env) error

	// Listen method adds a given runtime environment listener.
	// If the given listener is nil, ignore it.
	Listen(Listener)

	// UnListen removes and returns to the recently added listener.
	// If there is no listener to be removed, nil is returned.
	UnListen() Listener

	// UnListenAll removes and returns all added listeners.
	// If there is no listener to be removed, nil is returned.
	UnListenAll() []Listener
}

Manager interface type defines a runtime environment manager. An independent system should share an independent manager instance.

func New

func New() Manager

New creates and returns a new instance of the built-in runtime environment manager. The default runtime environment is Development, and all built-in runtime environments have been registered.

func NewEmpty

func NewEmpty() Manager

NewEmpty creates and returns an empty instance of the runtime environment manager. The manager returned by this function does not register any runtime environment, and the current runtime environment is empty.

Jump to

Keyboard shortcuts

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