zdpgo_env

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: MIT Imports: 14 Imported by: 1

README

zdpgo_env

Golang读取环境变量的库

版本历史

  • v0.1.1 2022/04/26 新增:写入环境变量到新的配置文件
  • v0.1.2 2022/09/22 优化:部分代码优化
  • v0.1.3 2022/09/22 新增:读取环境变量并转换为整数

使用示例

写入环境变量
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_env"
)

func main() {
	e := zdpgo_env.New()
	envMap := map[string]string{
		"a": "bbb",
		"b": "ccc",
		"c": "ddd",
	}

	// 写入环境变量
	err := e.WriteNew(".env1", envMap)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 获取环境变量
	fmt.Println(e.Get("a"))
	fmt.Println(e.Get("b"))
	fmt.Println(e.Get("c"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAStructPtr is returned if you pass something that is not a pointer to a
	// Struct to Parse.
	ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct")
)

nolint: gochecknoglobals

Functions

func Exec added in v0.1.1

func Exec(filenames []string, cmd string, cmdArgs []string) error

Exec 加载环境变量文件列表,然后执行CMD命令 @param filenames 环境变量文件列表 @param cmd 命令 @param cmd cmdArgs 参数

func Load

func Load(filenames ...string) (err error)

Load 加载.env文件中的环境变量

func Marshal added in v0.1.1

func Marshal(envMap map[string]string) (string, error)

Marshal outputs the given environment as a dotenv-formatted environment file. Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.

func Overload added in v0.1.1

func Overload(filenames ...string) (err error)

Overload will read your env file(s) and load them into ENV for this process.

Call this function as close as possible to the start of your program (ideally in main)

If you call Overload without any args it will default to loading .env in the current path

You can otherwise tell it which files to load (there can be more than one) like

godotenv.Overload("fileone", "filetwo")

It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars.

func Parse added in v0.1.1

func Parse(r io.Reader) (envMap map[string]string, err error)

Parse 解析输入流,转换为map对象

func ParseConfig added in v0.1.1

func ParseConfig(v interface{}, opts ...Options) error

ParseConfig 从环境变量读取env配置写入到env标签对应的内容

func ParseWithFuncs added in v0.1.1

func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...Options) error

ParseWithFuncs is the same as `Parse` except it also allows the user to pass in custom parsers.

func Read added in v0.1.1

func Read(filenames ...string) (envMap map[string]string, err error)

Read 读取所有文件中的环境变量 @param filenames 环境变量文件列表 @return envMap 环境变量字典 @return err 错误信息

func Unmarshal added in v0.1.1

func Unmarshal(str string) (envMap map[string]string, err error)

Unmarshal 读取env字符串数据并解析,返回一个map字典

func Write added in v0.1.1

func Write(envMap map[string]string, filename string) error

Write 写入环境变量 @param envMap 环境变量字典 @param filename 环境变量存储文件

Types

type Config

type Config struct {
	Debug          bool   `json:"debug" yaml:"debug" env:"debug"`
	ConfigFilePath string `json:"config_file_path" yaml:"config_file_path" env:"config_file_path"`
}

func GetDefaultConfig added in v0.1.1

func GetDefaultConfig() (*Config, error)

GetDefaultConfig 获取默认配置

func UpdateDefaultConfig added in v0.1.1

func UpdateDefaultConfig(config Config) (cfg *Config)

UpdateDefaultConfig 更新默认配置

type Env

type Env struct {
	Config       *Config                      // 核心配置
	FileEnvMap   map[string]map[string]string // 文件环境变量
	SystemEnvMap map[string]string            // 系统环境变量
	AllEnvMap    map[string]string            // 所有环境变量
}

Env 操作环境变量的核心对象

func New

func New() (e *Env)

New 创建环境变量实例

func NewWithConfig added in v0.1.1

func NewWithConfig(config Config) (e *Env)

NewWithConfig 使用配置创建环境变量实例

func (*Env) Add added in v0.1.1

func (e *Env) Add(key, value string) (err error)

Add 添加环境变量

func (*Env) AddMany added in v0.1.1

func (e *Env) AddMany(keyValues ...string) (err error)

AddMany 同时添加多个环境变量

func (*Env) Find added in v0.1.1

func (e *Env) Find(key string) (value string, exists bool)

Find 查找环境变量

func (*Env) FindAll added in v0.1.1

func (e *Env) FindAll() (result map[string]string)

FindAll 读取所有的环境变量

func (*Env) Get

func (e *Env) Get(key string) string

Get 根据键获取环境变量中的值

func (*Env) GetDefault

func (e *Env) GetDefault(key, defaultValue string) string

GetDefault 获取环境变量信息,找不到则使用默认值

func (*Env) GetInt added in v0.1.3

func (e *Env) GetInt(key string) int

Get 根据键获取环境变量中的值并转换为int类型

func (*Env) Load

func (e *Env) Load(filenames ...string) (err error)

Load 加载.env文件中的环境变量

func (*Env) Parse added in v0.1.1

func (e *Env) Parse(v interface{}, opts ...Options) (err error)

Parse 解析带env标签的结构体,并将其值添加到环境变量中 @param v 结构体对象 @param opts 参数选项 @return opts 参数选项

func (*Env) ParseConfig added in v0.1.1

func (e *Env) ParseConfig(v interface{}, opts ...Options) error

ParseConfig 从环境变量读取env配置写入到env标签对应的内容

func (*Env) Read

func (e *Env) Read(filename string) (err error)

Read 读取环境变量配置文件 @param filename 环境变量文件名称 @return err 错误信息

func (*Env) ReadDefault added in v0.1.1

func (e *Env) ReadDefault() (err error)

ReadDefault 读取默认的环境变量配置文件

func (*Env) ReadFiles added in v0.1.1

func (e *Env) ReadFiles(filenames ...string) (err error)

ReadFiles 加载配置文件列表中的所有环境变量 @param filenames 环境变量文件列表

func (*Env) Remove added in v0.1.1

func (e *Env) Remove(key string) (err error)

Remove 移除环境变量

func (*Env) RemoveMany added in v0.1.1

func (e *Env) RemoveMany(keys ...string) (err error)

RemoveMany 同时移除多个环境变量

func (*Env) Save added in v0.1.1

func (e *Env) Save(filename string) (err error)

Save 保存环境变量

func (*Env) Write added in v0.1.1

func (e *Env) Write(filename string) (err error)

Write 保存环境变量

func (*Env) WriteNew added in v0.1.1

func (e *Env) WriteNew(filename string, envMap map[string]string) (err error)

WriteNew 将环境变量map写入到新的配置文件中

type OnSetFn added in v0.1.1

type OnSetFn func(tag string, value interface{}, isDefault bool)

OnSetFn is a hook that can be run when a value is set.

type Options added in v0.1.1

type Options struct {
	// Environment keys and values that will be accessible for the service.
	Environment map[string]string

	// TagName specifies another tagname to use rather than the default env.
	TagName string

	// RequiredIfNoDef automatically sets all env as required if they do not declare 'envDefault'
	RequiredIfNoDef bool

	// OnSet allows to run a function when a value is set
	OnSet OnSetFn

	// Prefix define a prefix for each key
	Prefix string
	// contains filtered or unexported fields
}

Options for the parser.

type ParserFunc added in v0.1.1

type ParserFunc func(v string) (interface{}, error)

ParserFunc defines the signature of a function that can be used within `CustomParsers`.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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