Documentation ¶
Overview ¶
Example ¶
package main import ( "flag" "fmt" "os" "strings" "github.com/Bofry/config" ) func main() { // set env { os.Clearenv() os.Setenv("ENVIRONMENT", "production") os.Setenv("REDIS_HOST", "127.0.0.3:6379") os.Setenv("REDIS_PASSWORD", "1234") os.Setenv("K8S_REDIS_HOST", "demo-kubernetes:6379") os.Setenv("K8S_REDIS_PASSWORD", "p@ssw0rd") os.Setenv("K8S_REDIS_DB", "6") } // set command line arguments { os.Args = []string{"example", "-redis-db", "32"} flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) } // generate .env { os.WriteFile(".env", []byte( strings.Join([]string{ "REDIS_HOST=127.0.0.1:6379", "REDIS_DB=29", "TAG=demo,test", }, "\n")), 0644) } // generate .VERSION { os.WriteFile(".VERSION", []byte( strings.Join([]string{ "v1.0.2", }, "\n")), 0644) } // generate config.yaml { os.WriteFile("config.yaml", []byte( strings.Join([]string{ "redisDB: 3", "redisPoolSize: 10", "workspace: demo_test", }, "\n")), 0644) } // generate config.staging.yaml { os.WriteFile("config.staging.yaml", []byte( strings.Join([]string{ "redisDB: 9", "redisPoolSize: 10", "workspace: demo_stag", }, "\n")), 0644) } // generate config.production.yaml { os.WriteFile("config.production.yaml", []byte( strings.Join([]string{ "redisDB: 12", "redisPoolSize: 50", "workspace: demo_prod", }, "\n")), 0644) } conf := struct { RedisHost string `env:"REDIS_HOST" yaml:"redisHost" arg:"redis-host;the Redis server address and port"` RedisPassword string `env:"REDIS_PASSWORD" yaml:"redisPassword" arg:"redis-passowrd;the Redis password"` RedisDB int `env:"REDIS_DB" yaml:"redisDB" arg:"redis-db;the Redis database number"` RedisPoolSize int `env:"-" yaml:"redisPoolSize"` Workspace string `env:"-" yaml:"workspace" arg:"workspace;the data workspace"` Tags []string `env:"TAG"` Version string `resource:".VERSION"` }{} config.NewConfigurationService(&conf). LoadDotEnv(). LoadEnvironmentVariables(""). LoadEnvironmentVariables("K8S"). LoadYamlFile("config.yaml"). LoadYamlFile("config.${ENVIRONMENT}.yaml"). LoadCommandArguments(). LoadResource("") fmt.Printf("RedisHost = %q\n", conf.RedisHost) fmt.Printf("RedisPassword = %q\n", conf.RedisPassword) fmt.Printf("RedisDB = %d\n", conf.RedisDB) fmt.Printf("RedisPoolSize = %d\n", conf.RedisPoolSize) fmt.Printf("Workspace = %q\n", conf.Workspace) fmt.Printf("Tags = %q\n", conf.Tags) fmt.Printf("Version = %q\n", conf.Version) }
Output: RedisHost = "demo-kubernetes:6379" RedisPassword = "p@ssw0rd" RedisDB = 32 RedisPoolSize = 50 Workspace = "demo_prod" Tags = ["demo" "test"] Version = "v1.0.2"
Index ¶
- type ArbitraryPrinter
- type CommonPrinter
- type ConfigurationService
- func (service *ConfigurationService) LoadBytes(buffer []byte, unmarshal UnmarshalFunc) *ConfigurationService
- func (service *ConfigurationService) LoadCommandArguments() *ConfigurationService
- func (service *ConfigurationService) LoadDotEnv() *ConfigurationService
- func (service *ConfigurationService) LoadDotEnvFile(filepath string) *ConfigurationService
- func (service *ConfigurationService) LoadEnvironmentVariables(prefix string) *ConfigurationService
- func (service *ConfigurationService) LoadFile(fullpath string, unmarshal UnmarshalFunc) *ConfigurationService
- func (service *ConfigurationService) LoadJsonBytes(buffer []byte) *ConfigurationService
- func (service *ConfigurationService) LoadJsonFile(filepath string) *ConfigurationService
- func (service *ConfigurationService) LoadResource(baseDir string) *ConfigurationService
- func (service *ConfigurationService) LoadYamlBytes(buffer []byte) *ConfigurationService
- func (service *ConfigurationService) LoadYamlFile(filepath string) *ConfigurationService
- func (service *ConfigurationService) Map(mapper structproto.StructMapper) error
- func (service *ConfigurationService) Output()
- func (service *ConfigurationService) OutputWithPrinter(printer Printer)
- type NonePrinter
- type Printable
- type Printer
- type UnmarshalFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArbitraryPrinter ¶ added in v0.2.1
type ArbitraryPrinter struct {
// contains filtered or unexported fields
}
func NewArbitraryPrinter ¶ added in v0.2.1
func NewArbitraryPrinter(writer io.Writer, successor Printer) *ArbitraryPrinter
func (*ArbitraryPrinter) Print ¶ added in v0.2.1
func (p *ArbitraryPrinter) Print(target interface{}) error
type CommonPrinter ¶ added in v0.2.1
type CommonPrinter struct {
// contains filtered or unexported fields
}
func NewCommonPrinter ¶ added in v0.2.1
func NewCommonPrinter(writer io.Writer) *CommonPrinter
func (*CommonPrinter) Print ¶ added in v0.2.1
func (p *CommonPrinter) Print(v interface{}) error
type ConfigurationService ¶
type ConfigurationService struct {
// contains filtered or unexported fields
}
func NewConfigurationService ¶
func NewConfigurationService(target interface{}) *ConfigurationService
func (*ConfigurationService) LoadBytes ¶
func (service *ConfigurationService) LoadBytes(buffer []byte, unmarshal UnmarshalFunc) *ConfigurationService
func (*ConfigurationService) LoadCommandArguments ¶
func (service *ConfigurationService) LoadCommandArguments() *ConfigurationService
func (*ConfigurationService) LoadDotEnv ¶
func (service *ConfigurationService) LoadDotEnv() *ConfigurationService
func (*ConfigurationService) LoadDotEnvFile ¶
func (service *ConfigurationService) LoadDotEnvFile(filepath string) *ConfigurationService
func (*ConfigurationService) LoadEnvironmentVariables ¶
func (service *ConfigurationService) LoadEnvironmentVariables(prefix string) *ConfigurationService
func (*ConfigurationService) LoadFile ¶
func (service *ConfigurationService) LoadFile(fullpath string, unmarshal UnmarshalFunc) *ConfigurationService
Example ¶
// prepare config.yaml { os.WriteFile("config.yaml", []byte( strings.Join([]string{ "redisDB: 3", "redisPoolSize: 10", "workspace: demo_test", }, "\n")), 0644) } conf := struct { RedisHost string `env:"REDIS_HOST" yaml:"redisHost" arg:"redis-host;the Redis server address and port"` RedisPassword string `env:"REDIS_PASSWORD" yaml:"redisPassword" arg:"redis-passowrd;the Redis password"` RedisDB int `env:"REDIS_DB" yaml:"redisDB" arg:"redis-db;the Redis database number"` RedisPoolSize int `env:"-" yaml:"redisPoolSize"` Workspace string `env:"-" yaml:"workspace" arg:"workspace;the data workspace"` Tags []string `env:"TAG"` Version string `resource:".VERSION"` }{} config.NewConfigurationService(&conf). LoadFile("config.yaml", yaml.Unmarshal) fmt.Printf("RedisHost = %q\n", conf.RedisHost) fmt.Printf("RedisPassword = %q\n", conf.RedisPassword) fmt.Printf("RedisDB = %d\n", conf.RedisDB) fmt.Printf("RedisPoolSize = %d\n", conf.RedisPoolSize) fmt.Printf("Workspace = %q\n", conf.Workspace) fmt.Printf("Tags = %q\n", conf.Tags) fmt.Printf("Version = %q\n", conf.Version)
Output: RedisHost = "" RedisPassword = "" RedisDB = 3 RedisPoolSize = 10 Workspace = "demo_test" Tags = [] Version = ""
func (*ConfigurationService) LoadJsonBytes ¶
func (service *ConfigurationService) LoadJsonBytes(buffer []byte) *ConfigurationService
func (*ConfigurationService) LoadJsonFile ¶
func (service *ConfigurationService) LoadJsonFile(filepath string) *ConfigurationService
func (*ConfigurationService) LoadResource ¶
func (service *ConfigurationService) LoadResource(baseDir string) *ConfigurationService
func (*ConfigurationService) LoadYamlBytes ¶
func (service *ConfigurationService) LoadYamlBytes(buffer []byte) *ConfigurationService
func (*ConfigurationService) LoadYamlFile ¶
func (service *ConfigurationService) LoadYamlFile(filepath string) *ConfigurationService
func (*ConfigurationService) Map ¶ added in v0.2.1
func (service *ConfigurationService) Map(mapper structproto.StructMapper) error
func (*ConfigurationService) Output ¶ added in v0.2.1
func (service *ConfigurationService) Output()
func (*ConfigurationService) OutputWithPrinter ¶ added in v0.2.1
func (service *ConfigurationService) OutputWithPrinter(printer Printer)
type NonePrinter ¶ added in v0.2.1
type NonePrinter struct{}
func (NonePrinter) Print ¶ added in v0.2.1
func (NonePrinter) Print(target interface{}) error
type UnmarshalFunc ¶
Source Files ¶
Click to show internal directories.
Click to hide internal directories.