{"id":16431836,"url":"https://github.com/coreybutler/go-localenvironment","last_synced_at":"2025-03-23T08:31:40.705Z","repository":{"id":57513122,"uuid":"163483017","full_name":"coreybutler/go-localenvironment","owner":"coreybutler","description":"Apply environment variables if they exist in env.json.","archived":false,"fork":false,"pushed_at":"2024-06-11T07:46:57.000Z","size":27,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T18:57:51.842Z","etag":null,"topics":["environment-manager","environment-variables","environment-vars"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coreybutler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["coreybutler"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2018-12-29T06:26:49.000Z","updated_at":"2024-06-11T07:47:00.000Z","dependencies_parsed_at":"2024-06-20T11:22:47.693Z","dependency_job_id":null,"html_url":"https://github.com/coreybutler/go-localenvironment","commit_stats":{"total_commits":29,"total_committers":2,"mean_commits":14.5,"dds":"0.31034482758620685","last_synced_commit":"0b50bcf61b27bf5cc871d8433cdd83f737c4097c"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fgo-localenvironment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fgo-localenvironment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fgo-localenvironment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coreybutler%2Fgo-localenvironment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coreybutler","download_url":"https://codeload.github.com/coreybutler/go-localenvironment/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245078067,"owners_count":20557274,"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":["environment-manager","environment-variables","environment-vars"],"created_at":"2024-10-11T08:32:56.923Z","updated_at":"2025-03-23T08:31:40.007Z","avatar_url":"https://github.com/coreybutler.png","language":"Go","funding_links":["https://github.com/sponsors/coreybutler"],"categories":[],"sub_categories":[],"readme":"# go-localenvironment\n\n[![Version](https://img.shields.io/github/tag/coreybutler/go-localenvironment.svg)](https://github.com/coreybutler/go-localenvironment)\n[![GoDoc](https://godoc.org/github.com/coreybutler/go-localenvironment?status.svg)](https://godoc.org/github.com/coreybutler/go-localenvironment)\n\nApply environment variables sourced from `env.json`, `.env`, or custom sources in the current working directory. This is a port of the [coreybutler/localenvironment](https://github.com/coreybutler/localenvironment) Node.js module.\n\n**Basic Usage**\n\n```go\nimport \"github.com/coreybutler/go-localenvironment\"\n\nfunc main() {\n  localenvironment.Apply()\n}\n```\n\n## Overview\n\nThis package provides a lightweight approach to environment variable management within an application. It will look for a file called `env.json` or `.env`. If one or both files exist, each key is added as an environment variable, accessible via the [os.Getenv](https://golang.org/pkg/os/#Getenv) method. If the file does not exist, it is silently ignored.\n\n### Example Usage\n\nConsider the following directory contents:\n\n```sh\n\u003e dir\n  - env.json\n  - .env\n  - main.go\n```\n\n**env.json**\n\n```json\n{\n  \"MY_API_KEY\": \"12345\"\n}\n```\n\n**.env**\n\n```sh\nMY_OTHER_API_KEY=ABC67890\n```\n\n**main.go**:\n\n```go\npackage main\n\nimport (\n  \"os\"\n  \"log\"\n  \"github.com/coreybutler/go-localenvironment\"\n)\n\nfunc main() {\n  err := localenvironment.Apply() // Apply the env.json/.env attributes to the environment variables.\n  if err != nil {\n    log.Printf(\"Error: %s\", err)\n  }\n\n  apiKey := os.Getenv(\"MY_API_KEY\")\n  otherApiKey := os.Getenv(\"MY_OTHER_API_KEY\")\n\n  log.Printf(\"My API key is %s. The other is %s.\\n\", apiKey, otherApiKey)\n}\n```\n\nRunning `main.go` will log `My API key is 12345. The other is ABC67890.`.\n\nVariables applied by localenvironment are _ephemerally added_ to the environment. They are not persisted to the user/system environment variable store. If `MY_API_URL` is defined as a user/system variable, it will still be available whether localenvironment applies `env.json`/`.env` variables or not.\n\nIn the case of a conflicting variable, localenvironment will override values at runtime only. For example, if `MY_API_KEY=abcde` is defined as a user environment variable, localenvironment will override the value of `MY_API_KEY` with the value from the `env.json`/`.env` file (i.e. it will be `12345`).\n\n### Variable Flattening/Expansion (JSON only)\n\nNested JSON properties are automatically flattened.\n\nFor example:\n\n```javascript\n{\n  \"a\": {\n    \"b\": {\n      \"c\": \"something\"\n    }\n  }\n}\n```\n\nThe data structure above would be flattened into an environment variable called `A_B_C`, with a value of `something`.\n\n### Custom Sources (other than `env.json`/`.env`)\n\nIt is possible to specify an alternative JSON/KV files using the `ApplyFile` method:\n\n```go\npackage main\n\nimport (\n    \"os\"\n    \"log\"\n    \"github.com/coreybutler/go-localenvironment\"\n)\n\nfunc main() {\n    err := localenvironment.ApplyFile(\"/path/to/config.json\") // Apply your own attributes to the environment variables.\n    if err != nil {\n      log.Printf(\"Error: %s\", err)\n    }\n\n    apiKey := os.Getenv(\"...\")\n}\n```\n\n### Multiple Sources\n\nWhile less common, there are circumstances where it is useful to apply more than one configuration to the environment variables. The `ApplyFiles` method supports this.\n\n\n```go\npackage main\n\nimport (\n    \"os\"\n    \"log\"\n    \"github.com/coreybutler/go-localenvironment\"\n)\n\nfunc main() {\n    err := localenvironment.ApplyFiles(\n      \"/path/to/config.json\",\n      \"/path/to/other.json\"\n    ) // Apply your own attributes to the environment variables.\n\n    if err != nil {\n      log.Printf(\"Error: %s\", err)\n    }\n\n    apiKey := os.Getenv(\"...\")\n}\n```\n\n---\n\n## Why?\n\nThis is a simple approach for adding environment variable management to code. Alternatively (or in addition), it may be desirable to define environment variables in the build process. For this, [QuikGo](https://github.com/quikdev/go) provides a robust option leveraging similar techniques.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreybutler%2Fgo-localenvironment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoreybutler%2Fgo-localenvironment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoreybutler%2Fgo-localenvironment/lists"}