env

package
v0.0.0-...-4abcb88 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SparrowProfileDirsKey    = "sparrow.profile.dirs"    // sparrow profile dirs key
	SparrowProfileIncludeKey = "sparrow.profile.include" // 包含哪些profile
)
View Source
const (
	DefaultPlaceholderPrefix         = "${" // 默认占位符前缀
	DefaultPlaceholderSuffix         = "}"  // 默认占位符后缀
	DefaultPlaceholderValueSeparator = ":"  // 占位符内值分隔符
)
View Source
const (
	DeployInfoEnvironmentPropertySourceName         = "deployInfoEnvironment"
	DefaultApplicationEnvironmentPropertySourceName = "defaultApplicationEnvironment"
	DeployInfoSetKey                                = "deploy.set"
	DeployInfoEnvKey                                = "deploy.env"
)
View Source
const (
	/** 命令行变量 PropertySource GetName */
	CommandLineEnvironmentPropertySourceName = "commandLineEnvironment"
)
View Source
const (
	/** 系统环境变量 PropertySource GetName */
	SystemEnvironmentPropertySourceName = "systemEnvironment"
)

Variables

View Source
var (
	WellKnownSimplePrefixes = map[string]string{
		"}": "{",
		"]": "[",
		")": "(",
	}
)

Functions

func GetCommandLineProperty

func GetCommandLineProperty(key string) (value string, exists bool)

* 获取指定的命令行参数

func ListDirApplicationFiles

func ListDirApplicationFiles(dir string) []*FileUtils.FileInfo

Types

type CommandLinePropertySource

type CommandLinePropertySource struct {
	MapPropertySource
}

** 命令行参数解析,解析格式: 格式: --key=value 实例:

--name=arvin ==> key=name, value = arvin
--name=      ==> key=name, value = ""

func NewCommandLinePropertySource

func NewCommandLinePropertySource(appendCommandLine string) *CommandLinePropertySource

type Environment

type Environment interface {
	PropertyResolver

	/**
	获取激活的配置列表,返回文件的绝对路径
	*/
	GetActiveProfiles() []string

	/**
	获取一个可变的属性来源对象
	*/
	GetPropertySources() *MutablePropertySources

	/**
	合并父环境信息,子环境属性优先生效,只有子环境中不存在的才会在父环境中使用,比如假设父子环境中都有相同的配置key,那么将会使用子环境的优先
	*/
	Merge(parent Environment)

	/**
	订阅变更, keyPattern: 等值、正则匹配,如果为空字符串或者 * 那么表示所有,如果是个合法的正则,那么就按照正则匹配
	*/
	Subscribe(keyPattern string, handler func(event *KeyChangeEvent))

	/**
	绑定配置项到某个模型对象,注意传进来的必须是指针类型, keyPrefix key前缀,会直接和配置struct的属性直接拼接,如果有.的话要注意了
	@param name 名称,唯一
	@param cfgPtr 配置指针
	@param changedListen 是否需要进行监听
	*/
	BindProperties(keyPrefix string, cfgPtr interface{}) (beanPtr interface{}, err error)

	BindPropertiesListen(keyPrefix string, cfgPtr interface{}, changedListen bool) (beanPtr interface{}, err error)

	/**
	获取属性对象
	@param typeTemplate 类型模板,可以提供属性类型的指针类型,也可以直接提供 reflect.Type 类型
	*/
	GetProperties(typeTemplate interface{}) (beanPtr interface{})

	IsDev() bool
	IsTest() bool
	IsFat() bool
	IsProd() bool
	/**
	当前环境
	*/
	GetEnv() deploy.Env
	/**
	部署集合
	*/
	GetSet() string
}

环境,包含运行环境、启动项目用到的参数、配置等等

type KeyChangeEvent

type KeyChangeEvent struct {
	Key        string        // 变更的配置 key
	Ov         string        // 旧值
	Nv         string        // 新值
	ChangeType KeyChangeType // 变更类型
}

配置变更事件

func (*KeyChangeEvent) String

func (e *KeyChangeEvent) String() string

type KeyChangeType

type KeyChangeType string

属性变更类型

const (
	PropertyAdd    KeyChangeType = "ADD"
	PropertyUpdate KeyChangeType = "UPDATE"
	PropertyDel    KeyChangeType = "DEL"
)

type MapPropertySource

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

* 基于 Map 实现的 env/PropertySource 接口

func NewMapPropertySource

func NewMapPropertySource(name string, properties map[string]string) *MapPropertySource

func (*MapPropertySource) Each

func (m *MapPropertySource) Each(consumer func(key string, value string) (stop bool))

func (*MapPropertySource) GetName

func (m *MapPropertySource) GetName() string

func (*MapPropertySource) GetProperty

func (m *MapPropertySource) GetProperty(key string) (value string, exists bool)

func (*MapPropertySource) GetPropertyWithDef

func (m *MapPropertySource) GetPropertyWithDef(key string, def string) string

func (*MapPropertySource) Subscribe

func (m *MapPropertySource) Subscribe(keyPattern string, handler func(event *KeyChangeEvent))

type MutablePropertySources

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

* 可变配置来源

func NewMutablePropertySources

func NewMutablePropertySources(propertySourceList ...PropertySource) *MutablePropertySources

func (*MutablePropertySources) AddAfter

func (s *MutablePropertySources) AddAfter(relativePropertySourceName string, propertySource PropertySource) error

func (*MutablePropertySources) AddBefore

func (s *MutablePropertySources) AddBefore(relativePropertySourceName string, propertySource PropertySource) error

* 添加到指定名称之前, 如果指定名称不存在则返回异常

func (*MutablePropertySources) AddFirst

func (s *MutablePropertySources) AddFirst(propertySource PropertySource)

* 添加到第一个, 最优先生效

func (*MutablePropertySources) AddLast

func (s *MutablePropertySources) AddLast(propertySource PropertySource)

* 加到最后面,最后生效

func (*MutablePropertySources) Contains

func (s *MutablePropertySources) Contains(name string) bool

func (*MutablePropertySources) Each

func (s *MutablePropertySources) Each(consumer func(index int, source PropertySource) (stop bool))

func (*MutablePropertySources) Get

func (s *MutablePropertySources) Get(name string) (source PropertySource, exists bool)

func (*MutablePropertySources) Remove

func (s *MutablePropertySources) Remove(name string)

func (*MutablePropertySources) Replace

func (s *MutablePropertySources) Replace(name string, propertySource PropertySource) error

func (*MutablePropertySources) Size

func (s *MutablePropertySources) Size() int

func (*MutablePropertySources) Subscribe

func (s *MutablePropertySources) Subscribe(listener func(self *MutablePropertySources))

type Option

type Option func(environment *StandardEnvironment)

选项

func AdditionalPropertySources

func AdditionalPropertySources(additionalPropertySources *MutablePropertySources) Option

* 添加额外的配置来源

func AppendCommandLine

func AppendCommandLine(commandLine string) Option

* 追加命令行参数,如果原来有命令参数,再继续追加的话,原来相同key的会被覆盖

func ConfigDirs

func ConfigDirs(dirs ...string) Option

* 配置文件扫描路径,会按照顺序依次搜索配置文件,并且搜索的顺序就是生效的顺序

func DeployInfo

func DeployInfo(info *deploy.Info) Option

* 自定义部署信息,如果这个设置了的话,直接使用这个部署信息,而不是用检测的,一般用于本地测试

func IgnoreUnresolvableNestedPlaceholders

func IgnoreUnresolvableNestedPlaceholders(ignore bool) Option

* 是否忽略无法处理的占位符,如果忽略则不处理,不忽略的话,那么遇到不能解析的占位符直接 panic

func IncludeProfiles

func IncludeProfiles(profiles ...string) Option

* 包含 profiles, 会放在系统计算的之后

func TraceIdGenerator

func TraceIdGenerator(generator logger.TraceIdGenerator) Option

type Options

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

* 配置选项

type PollingPropertySource

type PollingPropertySource struct {
	Name            string         // 名称
	PropertyReader  PropertyReader // 配置读取实现
	PollingInterval int64          // 轮询间隔,单位:秒
	// contains filtered or unexported fields
}

* 基于轮询实现的配置来源

func NewPollingPropertySource

func NewPollingPropertySource(name string, refreshInterval int64, reader PropertyReader) (source *PollingPropertySource, err error)

创建轮询配置源, 参数不正确的话直接抛出panic @param refreshInterval 刷新时间间隔,单位:秒, 小于0表示不进行轮询

func (*PollingPropertySource) Each

func (p *PollingPropertySource) Each(consumer func(key string, value string) (stop bool))

func (*PollingPropertySource) GetName

func (p *PollingPropertySource) GetName() string

func (*PollingPropertySource) GetProperty

func (p *PollingPropertySource) GetProperty(key string) (value string, exists bool)

func (*PollingPropertySource) GetPropertyWithDef

func (p *PollingPropertySource) GetPropertyWithDef(key string, def string) string

func (*PollingPropertySource) Init

func (p *PollingPropertySource) Init()

func (*PollingPropertySource) Reload

func (p *PollingPropertySource) Reload() (err error)

重新加载配置

func (*PollingPropertySource) Subscribe

func (p *PollingPropertySource) Subscribe(keyPattern string, handler func(event *KeyChangeEvent))

type PropertyChangeListener

type PropertyChangeListener struct {
	KeyPattern string                      // 等值、正则匹配,如果为空字符串或者 * 那么表示所有,如果是个合法的正则,那么就按照正则匹配
	Regex      *regexp.Regexp              // 正则表达式
	Handler    func(event *KeyChangeEvent) // 处理器
}

func NewPropertyChangeListener

func NewPropertyChangeListener(keyPattern string, handler func(event *KeyChangeEvent)) *PropertyChangeListener

type PropertyPlaceholderHelper

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

* 占位符处理Helper

func NewPropertyPlaceholderHelper

func NewPropertyPlaceholderHelper(placeholderPrefix string,
	placeholderSuffix string,
	valueSeparator string,
	ignoreUnresolvablePlaceholders bool) *PropertyPlaceholderHelper

func (*PropertyPlaceholderHelper) ReplacePlaceholders

func (h *PropertyPlaceholderHelper) ReplacePlaceholders(value string, placeholderResolver func(key string) string) string

type PropertyReader

type PropertyReader interface {
	/**
	读取所有的配置项, key-value 结构
	*/
	ReadAll() (kvs map[string]string, err error)
}

type PropertyReaderWrapper

type PropertyReaderWrapper struct {
	/**
	读取配置的方法
	*/
	Reader func() (kvs map[string]string, err error)
}

func NewPropertyReader

func NewPropertyReader(reader func() (kvs map[string]string, err error)) *PropertyReaderWrapper

func (*PropertyReaderWrapper) ReadAll

func (p *PropertyReaderWrapper) ReadAll() (kvs map[string]string, err error)

type PropertyResolver

type PropertyResolver interface {

	/**
	 * 判断当前环境中是否配置了 key,注意,给定的 key 不允许为空字符串
	 */
	ContainsProperty(key string) bool

	/**
	获取配置项的字符串值,返回的值中,====不包含占位符====
	@param key 配置 key
	@return value 对应配置项的值
	@return exists 配置项是否存在,即 @ContainsProperty(key string) 的返回值一样意义
	*/
	GetProperty(key string) (value string, exists bool)

	/**
	获取指定配置项的值,如果对应配置项没有配置,那么返回 默认值,====不包含占位符====
	*/
	GetPropertyWithDef(key string, def string) string

	/**
	获取配置项,如果配置项没有配置的话,那么会直接 panic(通常是在项目启动的时候使用,避免项目非正常状态下启动),====不包含占位符====
	*/
	GetRequiredProperty(key string) string

	/**
	处理类似 ${...} 这种占位符, 替换对应的配置项,如果 ${...}中的配置项不存在,则不进行替换,但是也不会抛异常
	*/
	ResolvePlaceholders(text string) string

	/**
	处理类似 ${...} 这种占位符, 替换对应的配置项,如果 ${...}中的配置项不存在,则直接 panic,这是为了防止非正常启动
	*/
	ResolveRequiredPlaceholders(text string) string
}

Interface for resolving properties against any underlying source.

type PropertySource

type PropertySource interface {
	/**
	配置源名称
	*/
	GetName() string

	/**
	获取配置项的字符串值,返回的值中,包含占位符
	@param key 配置 key
	@return value 对应配置项的值
	@return exists 配置项是否存在,即 @ContainsProperty(key string) 的返回值一样意义
	*/
	GetProperty(key string) (value string, exists bool)

	/**
	获取指定配置项的值,如果对应配置项没有配置,那么返回 默认值
	*/
	GetPropertyWithDef(key string, def string) string

	/**
	遍历所有的配置项&值, consumer 处理过程中如果返回 stop=true则停止遍历
	*/
	Each(consumer func(key, value string) (stop bool))

	/**
	订阅变更, keyPattern: 等值、正则匹配,如果为空字符串或者 * 那么表示所有,如果是个合法的正则,那么就按照正则匹配
	*/
	Subscribe(keyPattern string, handler func(event *KeyChangeEvent))
}

func ReadLocalFileAsPropertySource

func ReadLocalFileAsPropertySource(name string, path string) (propertySource PropertySource, err error)

type PropertySources

type PropertySources interface {

	/**
	是否包含指定名称的配置来源
	*/
	Contains(name string) bool

	/**
	获取指定名称的配置来源
	@return source 如果存在则返回
	@return exists 是否存在
	*/
	Get(name string) (source PropertySource, exists bool)

	/**
	遍历配置来源, index 从 0开始,数字越小,优先级越高, consumer 变量过程中,如果 stop 返回true则停止遍历
	*/
	Each(consumer func(index int, source PropertySource) (stop bool))
}

type PropertySourcesPropertyResolver

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

* 配置来源属性解析器, 实现接口:env/PropertyResolver

func NewPropertySourcesPropertyResolver

func NewPropertySourcesPropertyResolver(propertySources PropertySources, ignoreUnresolvableNestedPlaceholders bool) *PropertySourcesPropertyResolver

* 创建属性解析器 @param ignoreUnresolvableNestedPlaceholders 是否忽略无法处理的占位符,如果忽略则不处理,不忽略的话,那么遇到不能解析的占位符直接 panic

func (*PropertySourcesPropertyResolver) ContainsProperty

func (p *PropertySourcesPropertyResolver) ContainsProperty(key string) bool

func (*PropertySourcesPropertyResolver) GetProperty

func (p *PropertySourcesPropertyResolver) GetProperty(key string) (value string, exists bool)

func (*PropertySourcesPropertyResolver) GetPropertyWithDef

func (p *PropertySourcesPropertyResolver) GetPropertyWithDef(key string, def string) string

func (*PropertySourcesPropertyResolver) GetRequiredProperty

func (p *PropertySourcesPropertyResolver) GetRequiredProperty(key string) string

func (*PropertySourcesPropertyResolver) ResolvePlaceholders

func (p *PropertySourcesPropertyResolver) ResolvePlaceholders(text string) string

func (*PropertySourcesPropertyResolver) ResolveRequiredPlaceholders

func (p *PropertySourcesPropertyResolver) ResolveRequiredPlaceholders(text string) string

type StandardEnvironment

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

* 标准环境实现, 实现接口 Environment

func New

func New(options ...Option) *StandardEnvironment

* 新建环境

func (*StandardEnvironment) BindProperties

func (s *StandardEnvironment) BindProperties(keyPrefix string, cfgPtr interface{}) (beanPtr interface{}, err error)

func (*StandardEnvironment) BindPropertiesListen

func (s *StandardEnvironment) BindPropertiesListen(keyPrefix string, cfgPtr interface{}, changedListen bool) (beanPtr interface{}, err error)

func (*StandardEnvironment) ContainsProperty

func (s *StandardEnvironment) ContainsProperty(key string) bool

func (*StandardEnvironment) GetActiveProfiles

func (s *StandardEnvironment) GetActiveProfiles() []string

func (*StandardEnvironment) GetEnv

func (s *StandardEnvironment) GetEnv() deploy.Env

func (*StandardEnvironment) GetProperties

func (s *StandardEnvironment) GetProperties(typeTemplate interface{}) (beanPtr interface{})

* 获取属性对象 @param typeTemplate 类型模板,可以提供属性类型的指针类型,也可以直接提供 reflect.Type 类型

func (*StandardEnvironment) GetProperty

func (s *StandardEnvironment) GetProperty(key string) (value string, exists bool)

func (*StandardEnvironment) GetPropertySources

func (s *StandardEnvironment) GetPropertySources() *MutablePropertySources

func (*StandardEnvironment) GetPropertyWithDef

func (s *StandardEnvironment) GetPropertyWithDef(key string, def string) string

func (*StandardEnvironment) GetRequiredProperty

func (s *StandardEnvironment) GetRequiredProperty(key string) string

func (*StandardEnvironment) GetSet

func (s *StandardEnvironment) GetSet() string

func (*StandardEnvironment) InitPropertyResolver

func (s *StandardEnvironment) InitPropertyResolver()

func (*StandardEnvironment) IsDev

func (s *StandardEnvironment) IsDev() bool

func (*StandardEnvironment) IsFat

func (s *StandardEnvironment) IsFat() bool

func (*StandardEnvironment) IsProd

func (s *StandardEnvironment) IsProd() bool

func (*StandardEnvironment) IsTest

func (s *StandardEnvironment) IsTest() bool

func (*StandardEnvironment) Merge

func (s *StandardEnvironment) Merge(parent Environment)

func (*StandardEnvironment) ResolvePlaceholders

func (s *StandardEnvironment) ResolvePlaceholders(text string) string

func (*StandardEnvironment) ResolveRequiredPlaceholders

func (s *StandardEnvironment) ResolveRequiredPlaceholders(text string) string

func (*StandardEnvironment) Subscribe

func (s *StandardEnvironment) Subscribe(keyPattern string, handler func(event *KeyChangeEvent))

type SystemEnvironmentPropertySource

type SystemEnvironmentPropertySource struct {
	MapPropertySource
}

* 系统环境变量 属性来源

func NewSystemEnvironmentPropertySource

func NewSystemEnvironmentPropertySource() *SystemEnvironmentPropertySource

Jump to

Keyboard shortcuts

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