Documentation ¶
Overview ¶
Package sprbox is an agnostic config parser (supporting YAML, TOML, JSON and Environment vars) and a toolbox factory with automatic configuration based on your build environment.
Index ¶
- Constants
- Variables
- func CompiledPath(path string) string
- func EnvSubDir(path string) string
- func GetInfo(w http.ResponseWriter, r *http.Request)
- func LoadConfig(config interface{}, files ...string) (err error)
- func LoadToolBox(toolBox interface{}, configPath string) (err error)
- func PrintInfo()
- func SetColoredLogs(colored bool)
- func SetDebug(enabled bool)
- func SetFileSearchCaseSensitive(caseSensitive bool)
- func Unmarshal(data []byte, config interface{}) (err error)
- type Environment
- type Repository
Constants ¶
const EnvVarKey = "BUILD_ENV"
EnvVarKey is the environment variable that determine the build environment in sprbox.
Variables ¶
var ( // BUILDENV define the current environment. // Can be defined by code or, since it is an exported string, // can be interpolated with -ldflags at build/run time: // go build -ldflags "-X github.com/oblq/goms/env.TAG=develop" -v -o ./api_bin ./api // // If TAG is empty then the environment variable 'BUILD_ENV' will be used. // // If also the 'BUILD_ENV' environment variable is empty, // if you have setup the VCS var, then the git.BranchName will be used. // Git-Flow automatic environment selection based on branch name is also supported. // Here the default environment RegEx, you can customize them as you want: // - Production exps: Exps{"production", "master"} // - Staging exps: Exps{"staging", "release/*", "hotfix/*"} // - Testing exps: Exps{"testing", "test"} // - Development exps: Exps{"development", "develop", "dev", "feature/*"} // - Local exps: Exps{"local"} BUILDENV = "" // VCS is the project version control system. // By default it uses the working directory. VCS = NewRepository("./") )
var ( Production = &Environment{id: "production", exps: []string{"production", "master"}, RunCompiled: true} Staging = &Environment{id: "staging", exps: []string{"staging", "release/*", "hotfix/*"}, RunCompiled: true} Testing = &Environment{id: "testing", exps: []string{"testing", "test"}, RunCompiled: false} Development = &Environment{id: "development", exps: []string{"development", "develop", "dev", "feature/*"}, RunCompiled: false} Local = &Environment{id: "local", exps: []string{"local"}, RunCompiled: false} )
Default environment's configuration
Functions ¶
func CompiledPath ¶
CompiledPath returns the path base if RunCompiled == true for the environment in use so that static files can stay side by side with the executable while it is possible to have a different location when the program is launched with `go run`. This allow to manage multiple packages in one project during development, for instance using a config path in the parent dir, side by side with the packages, while having the same config folder side by side with the executable where needed.
Can be used in:
sprbox.LoadToolBox(&myToolBox, sprbox.CompiledPath("../config"))
Example:
sprbox.Development.RunCompiled = false sprbox.BUILDENV = sprbox.Development.ID() sprbox.CompiledPath("../static_files/config") // -> "../static_files/config" sprbox.Development.RunCompiled = true sprbox.BUILDENV = sprbox.Development.ID() sprbox.CompiledPath("../static_files/config") // -> "config"
By default only Production and Staging environments have RunCompiled = true.
func GetInfo ¶
func GetInfo(w http.ResponseWriter, r *http.Request)
GetInfo print info in console from an http request.
func LoadConfig ¶
LoadConfig will unmarshal all the matched config files to the config interface.
Build-environment specific files will override generic files. The latest files will override the earliest.
Will also parse struct flags.
func LoadToolBox ¶
LoadToolBox initialize and (eventually) configure the provided struct pointer looking for the config files in the provided configPath.
func SetFileSearchCaseSensitive ¶
func SetFileSearchCaseSensitive(caseSensitive bool)
SetFileSearchCaseSensitive toggle case sensitive cinfig files search.
Types ¶
type Environment ¶
type Environment struct { // RunCompiled true means that the program run from // a precompiled binary for that environment. // CompiledPath() returns the path base if RunCompiled == true // so that static files can stay side by side with the executable // while it is possible to have a different location when the // program is launched with `go run`. // // By default only Production and Staging environments have RunCompiled = true. RunCompiled bool // contains filtered or unexported fields }
Environment struct.
func Env ¶
func Env() *Environment
Env returns the current selected environment by matching the privateTAG variable against the environments RegEx.
func (*Environment) AppendExp ¶
func (e *Environment) AppendExp(exp string)
AppendExp add a regular expression to match that environment.
func (*Environment) ID ¶
func (e *Environment) ID() string
ID returns the environment id, which are also a valid tag for the current environment.
func (*Environment) MatchTag ¶
func (e *Environment) MatchTag(tag string) bool
MatchTag check if the passed tag match that environment, a tag may be the branch name or the machine hostname or whatever you want.
func (*Environment) PrintInfo ¶
func (e *Environment) PrintInfo()
PrintInfo print some environment info in console.
func (*Environment) SetExps ¶
func (e *Environment) SetExps(exps []string)
SetExps set regular expressions to match that environment.
type Repository ¶
Repository represent a git repository
func NewRepository ¶
func NewRepository(path string) *Repository
NewRepository return a new Repository instance for the given path
func (*Repository) PrintInfo ¶
func (r *Repository) PrintInfo()
PrintInfo print git data in console.
func (*Repository) UpdateInfo ¶
func (r *Repository) UpdateInfo()
UpdateInfo grab git info and set 'Error' var eventually.