{"id":18810348,"url":"https://github.com/absaoss/env-binder","last_synced_at":"2025-04-13T20:30:57.511Z","repository":{"id":84232981,"uuid":"419694944","full_name":"AbsaOSS/env-binder","owner":"AbsaOSS","description":"Binding environment variables to GO structures","archived":false,"fork":false,"pushed_at":"2023-02-19T17:17:43.000Z","size":39,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-06-21T12:00:04.220Z","etag":null,"topics":["12-factor","binding","environment-variables","go","golang-library","microservices","parsing","structures","twelve-factor"],"latest_commit_sha":null,"homepage":"","language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AbsaOSS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null}},"created_at":"2021-10-21T11:27:36.000Z","updated_at":"2024-05-31T16:44:29.000Z","dependencies_parsed_at":"2023-09-29T12:47:06.925Z","dependency_job_id":null,"html_url":"https://github.com/AbsaOSS/env-binder","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"e8c12af19b672c73fdebfd16a16e9d7e5d259675"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbsaOSS%2Fenv-binder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbsaOSS%2Fenv-binder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbsaOSS%2Fenv-binder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbsaOSS%2Fenv-binder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AbsaOSS","download_url":"https://codeload.github.com/AbsaOSS/env-binder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223603209,"owners_count":17172060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["12-factor","binding","environment-variables","go","golang-library","microservices","parsing","structures","twelve-factor"],"created_at":"2024-11-07T23:19:54.854Z","updated_at":"2024-11-07T23:19:55.578Z","avatar_url":"https://github.com/AbsaOSS.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ENV binder\n[![License](http://img.shields.io/:license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)\n[![Go Reference](https://pkg.go.dev/badge/github.com/AbsaOSS/env-binder.svg)](https://pkg.go.dev/github.com/AbsaOSS/env-binder?branch=master)\n![Build Status](https://github.com/AbsaOSS/env-binder/actions/workflows/build.yaml/badge.svg?branch=master)\n![Linter](https://github.com/AbsaOSS/env-binder/actions/workflows/lint.yaml/badge.svg?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/AbsaOSS/env-binder)](https://goreportcard.com/report/github.com/AbsaOSS/env-binder?branch=master)\n\n\n\u003e _this module is deprecated: use the module https://github.com/AbsaOSS/go-k8s-operator-binder , which contains significant extensions but is fully backwards compatible._ \n\n\nThe ENV-BINDER package is used to easily bind environment variables to GO structures. ENV-BINDER is designed to\nbe usable in the widest possible range of scenarios.Among other things, it supports variable\nprefixes and bindings to unexported arrays. Take a look at the following usage example:\n```golang\nimport \"github.com/AbsaOSS/env-binder/env\"\n\ntype Endpoint struct {\n\tURL string `env:\"ENDPOINT_URL, require=true\"`\n}\n\ntype Config struct {\n\n\t// reading string value from NAME\n\tName string `env:\"NAME\"`\n\n\t// reuse an already bound env variable NAME\n\tDescription string `env:\"NAME\"`\n\n\t// reuse an already bound variable NAME, but replace only when name \n\t// was not set before\n\tAlternativeName string `env:\"NAME, protected=true\"`\n\n\t// reading int with 8080 as default value\n\tDefaultPort uint16 `env:\"PORT, default=8080\"`\n\n\t// reading slice of strings with default values\n\tRegions []string `env:\"REGIONS, default=[us-east-1,us-east-2,us-west-1]\"`\n\n\t// reading slice of strings from env var\n\tSubnets []string `env:\"SUBNETS, default=[10.0.0.0/24,192.168.1.0/24]\"`\n\t\n\t// default=[] ensures that if INTERVALS does not exist, \n\t// []int8{} is set instead of []int8{nil}\n\tInterval []uint8 `env:\"INTERVALS, default=[]\"`\n\n\t// nested structure\n\tCredentials struct {\n\n\t\t// binding required value\n\t\tKeyID string `env:\"ACCESS_KEY_ID, require=true\"`\n\n\t\t// binding to private field\n\t\tsecretKey string `env:\"SECRET_ACCESS_KEY, require=true\"`\n\t}\n\n\t// expected PRIMARY_ prefix in nested environment variables\n\tEndpoint1 Endpoint `env:\"PRIMARY\"`\n\n\t// expected FAILOVER_ prefix in nested environment variables\n\tEndpoint2 Endpoint `env:\"FAILOVER\"`\n\n\t// the field does not have a bind tag set, \n\t// so it will be ignored during bind\n\tArgs []string\n}\n\n\nfunc main() {\n\tdefer clean()\n\tos.Setenv(\"PRIMARY_ENDPOINT_URL\", \"https://ep1.cloud.example.com\")\n\tos.Setenv(\"FAILOVER_ENDPOINT_URL\", \"https://ep2.cloud.example.com\")\n\tos.Setenv(\"ACCESS_KEY_ID\", \"AKIAIOSFODNN7EXAMPLE\")\n\tos.Setenv(\"SECRET_ACCESS_KEY\", \"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY\")\n\tos.Setenv(\"NAME\", \"Hello from 12-factor\")\n\tos.Setenv(\"PORT\", \"9000\")\n\tos.Setenv(\"SUBNETS\", \"10.0.0.0/24,10.0.1.0/24, 10.1.0.0/24,  10.1.1.0/24\")\n\n\tc := \u0026Config{}\n\tc.AlternativeName = \"protected name\"\n\tc.Description = \"hello from ENV-BINDER\"\n\tif err := env.Bind(c); err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tfmt.Println(JSONize(c))\n}\n\n```\nfunction main generates the following output:\n```json\n{\n  \"Name\": \"Hello from 12-factor\",\n  \"Description\": \"Hello from 12-factor\",\n  \"AlternativeName\": \"protected name\",\n  \"DefaultPort\": 9000,\n  \"Regions\": [\n    \"us-east-1\",\n    \"us-east-2\",\n    \"us-west-1\"\n  ],\n  \"Subnets\": [\n    \"10.0.0.0/24\",\n    \"10.0.1.0/24\",\n    \"10.1.0.0/24\",\n    \"10.1.1.0/24\"\n  ],\n  \"Interval\": [],\n  \"Credentials\": {\n    \"KeyID\": \"AKIAIOSFODNN7EXAMPLE\"\n  },\n  \"Endpoint1\": {\n    \"URL\": \"https://ep1.cloud.example.com\"\n  },\n  \"Endpoint2\": {\n    \"URL\": \"https://ep2.cloud.example.com\"\n  },\n  \"Args\": null\n}\n```\n\n## supported types\nENV-BINDER supports all types listed in the following table.  In addition, it should be noted that in the case\nof slices, ENV-BINDER creates an instance of an empty slice if the value of the environment variable is\ndeclared and its value is empty string. In this case ENV-BINDER returns an empty slice instead of the vulnerable nil.\n\n| primitive types | slices |\n|---|---|\n| `int`,`int8`,`int16`,`int32`,`int64` | `[]int`,`[]int8`,`[]int16`,`[]int32`,`[]int64` |\n| `float32`,`float64` | `[]float32`,`[]float64` |\n| `uint`,`uint8`,`uint16`,`uint32`,`uint64` | `[]uint`,`[]uint8`,`[]uint16`,`[]uint32`,`[]uint64` |\n| `bool` | `[]bool` |\n| `string` | `[]string` |\n\n## supported keywords\nBesides the fact that ENV-BINDER works with private fields and can add prefixes to variable names, it\noperates with several keywords. The structure in the introductory section works with all types\nof these keywords.\n\n- `default` - the value specified in the default tag is used in case env variable does not exist. e.g:\n  `env: \"SUBNET\", default=10.0.1.0/24` or `env: \"ENV_SUBNETS\", default=[]` which will set an empty slice instead\n  of a vulnerable nil value in case `ENV_SUBNETS` does not exist.\n\n- `require` - if `require=true` then env variable must exist otherwise Bind function returns error\n\n- `protected` - if `protected=true` then, in case the field in the structure already has a set value , the\n  Bind function will not set it. Otherwise, bind will be applied to it.\n\nYou can combine individual tags freely: `env: \"ENV_SWITCHER\", default=[true, false, true], protected=true`\nis a perfectly valid configuration\n\n## API\nIf the Bind function is not enough for you, you can use any of the static functions of our API:\n```go\nimport \"github.com/AbsaOSS/env-binder/env\"\n\n// GetEnvAsStringOrFallback returns the env variable for the given key\n// and falls back to the given defaultValue if not set\nGetEnvAsStringOrFallback(key, defaultValue string) string\n\n// GetEnvAsArrayOfStringsOrFallback returns the env variable for the given key\n// and falls back to the given defaultValue if not set\n// GetEnvAsArrayOfStringsOrFallback trims all whitespaces from input \n// i.e. \"us, fr, au\" -\u003e {\"us\",\"fr\",\"au\"}\nGetEnvAsArrayOfStringsOrFallback(key string, defaultValue []string) []string\n\n// GetEnvAsIntOrFallback returns the env variable (parsed as integer) for\n// the given key and falls back to the given defaultValue if not set\nGetEnvAsIntOrFallback(key string, defaultValue int) (int, error)\n\n// GetEnvAsArrayOfIntsOrFallback returns the env variable for the given key\n// and falls back to the given defaultValue if not set\nGetEnvAsArrayOfIntsOrFallback(key string, defaultValue []int) ([]int, error)\n\n\n// GetEnvAsFloat64OrFallback returns the env variable (parsed as float64) for\n// the given key and falls back to the given defaultValue if not set\nGetEnvAsFloat64OrFallback(key string, defaultValue float64) (float64, error)\n\n// GetEnvAsArrayOfFloat64OrFallback returns the env variable for the given key\n// and falls back to the given defaultValue if not set\nGetEnvAsArrayOfFloat64OrFallback(key string, defaultValue []float64) ([]float64, error)\n\n\n// GetEnvAsBoolOrFallback returns the env variable for the given key,\n// parses it as boolean and falls back to the given defaultValue if not set\nGetEnvAsBoolOrFallback(key string, defaultValue bool) (bool, error)\n\n// GetEnvAsArrayOfBoolOrFallback returns the env variable for the given key\n// and falls back to the given defaultValue if not set\nGetEnvAsArrayOfBoolOrFallback(key string, defaultValue []bool) ([]bool, error) \n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabsaoss%2Fenv-binder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabsaoss%2Fenv-binder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabsaoss%2Fenv-binder/lists"}