{"id":20388567,"url":"https://github.com/efureev/envi","last_synced_at":"2025-03-04T23:42:35.977Z","repository":{"id":46092372,"uuid":"488574526","full_name":"efureev/envi","owner":"efureev","description":"Package which manage  dotenv (`.env`)","archived":false,"fork":false,"pushed_at":"2022-07-18T06:54:50.000Z","size":55,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-15T09:02:33.878Z","etag":null,"topics":["dotenv","env","package"],"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/efureev.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":null,"security":null,"support":null}},"created_at":"2022-05-04T12:12:31.000Z","updated_at":"2022-05-04T18:17:54.000Z","dependencies_parsed_at":"2022-07-19T02:47:23.516Z","dependency_job_id":null,"html_url":"https://github.com/efureev/envi","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Fenvi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Fenvi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Fenvi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Fenvi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/efureev","download_url":"https://codeload.github.com/efureev/envi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241940541,"owners_count":20045878,"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":["dotenv","env","package"],"created_at":"2024-11-15T03:11:32.192Z","updated_at":"2025-03-04T23:42:35.960Z","avatar_url":"https://github.com/efureev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Envi is a package to manage `.env` files\n\n[![Go package](https://github.com/efureev/envi/actions/workflows/go.yml/badge.svg)](https://github.com/efureev/envi/actions/workflows/go.yml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/efureev/envi)](https://goreportcard.com/report/github.com/efureev/envi)\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/efureev/envi)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/efureev/envi)\n![GitHub](https://img.shields.io/github/license/efureev/envi)\n\n## Installation\n\nAs a library\n\n```shell\ngo get github.com/efureev/envi\n```\n\n## Description\n\n`Envi` allows you to:\n\n- parse, load and save `.env`-files\n- load multiple `.env`-files in a row\n- fully manage your data in `Env`-structure like as:\n    - division by blocks and rows\n    - commented items\n    - set item's comments (blocks and rows)\n    - add or remove items (blocks and rows)\n    - sorting items\n\n## Usage\n\nFor example, `.env` file to parse:\n\n```dotenv\n###   ---[ Application section ]---   ###\n# Application name\nAPP_NAME=\"App name\"\nAPP_DEBUG=false\n\n# Default dev.host\n# APP_URL=http://dev.example.com\nAPP_URL=https://example.com\n\n###   ---[ NGINX cache section ]---   ###\n# Nginx cache path\nCACHE_NGINX_PATH=./storage/cache\n# Enable caching a page\nCACHE_NGINX_ENABLED=false\n\nTEST=false\n\n#APP_TRACE_LOAD=true\nDEBUGBAR_ENABLED=false\n\n#HYPE=false\n```\n\nHere we see:\n\n- 3 `Block`s:\n    - `APP`: Has Comment. Contains rows:\n        - `APP_NAME`: Has Comment\n        - `APP_DEBUG`\n        - `APP_URL`: Has `shadow`: `http://dev.example.com`\n        - `APP_TRACE_LOAD`: Commented row\n    - `CACHE`: Has Comment. Contains rows:\n        - `CACHE_NGINX_PATH`: Has a Comment\n        - `CACHE_NGINX_ENABLED`: Has a Comment\n    - `DEBUGBAR`:\n        - `DEBUGBAR_ENABLED`\n- 2 `Row`s:\n    - `HYPE`: Commented row\n    - `TEST`: Uncommented row\n\n### Blocks\n\nThe Block is defined by first occurrence `_` in row. You may set up minimum rows count to form a `Block`.\nBy default, it's `1`.\n\nBlock may have a comment.\n\n```go\nblock := envi.NewBlock(`app`).SetComment(`Application section`)\n```\n\nYou may change indent after `Block`:\n\n```go\nenvi.SetIndent(2)\n```\n\nYou may change a template of a block comment:\n\n```go\nenvi.SetCommentTemplate(`# \u003c-- `, ` --\u003e`)\n// or\nenvi.SetCommentTemplateByDefault()\n```\n\nYou may set row count to form a block from them:\n\n```go\nenvi.GroupRowsGreaterThen(1)\n```\n\n### Rows\n\nA `Row` may have a comment.\n\n```go\nrow := envi.NewRow(`app-section`, 'section 345').SetComment(`Section for the unit '345'`)\n```\n\nWill be:\n\n```dotenv\n# Section for the unit '345'\nAPP_SECTION=\"section 345\"\n```\n\nA `Row` may be commented.\n\n```go\nrow.Commented()\n```\n\nWill be:\n\n```dotenv\n# Section for the unit '345'\n# APP_SECTION=\"section 345\"\n```\n\nA `Row` may be a part of `Block` or not.\n\n```go\nenv := envi.Env{}\nblock := envi.NewBlock(`app`).AddRow(`session`, `test`)\nenv.Add(block, NewRow(`app-hash`, `sha-256`))\n\nenv.Save(`.env.local`)\n```\n\nWill be:\n\n```dotenv\nSection for the unit '345'\nAPP_SESSION=\"sha-256\"\n```\n\n### How to load `.env` files\n\n```go\nenv, err := envi.Load(`stubs/.env`) // for single file\n\n// to override multi files\nenv, err := envi.Load(`stubs/.env`, `stubs/.env.development`, `stubs/.env.development.local`)\n\n/// ...\n\nenv.Save(`.env.finish`)\n```\n\n### How to marshal `Env`\n\nConvert data to slice of `row`s\n\n```go\nlines := env.MarshalToSlice()\n```\n\nConvert data to string\n\n```go\ntext, err := env.Marshal()\n```\n\nMarshaling settings:\n\n```go\n// Marshal a data without commented rows\nenvi.SetMarshalingWithoutCommentedRows()\n\n// Marshal a data without comments\nenvi.SetMarshalingWithoutComments()\n\n// Marshal a data without `shadows`*\nenvi.SetMarshalingWithoutShadows()\n```\n\n*`shadow` - it's a commented example row. For instance:\n\n```dotenv\n# for docker\n# REDIS_HOST=redis\n# not for docker: 127.0.0.1\n#REDIS_HOST=10.212.12.2\nREDIS_HOST=127.0.0.1\n```\n\n`REDIS_HOST=redis` \u0026 `REDIS_HOST=10.212.12.2` - are shadows for row `REDIS_HOST=127.0.0.1`.\n\n### How to manipulate data after parsing `.env` files\n\n```go\nenv, err := envi.Load(`stubs/.env`)\n\n// Total count rows (including in blocks)\nenv.Count()\n\n// To receive a row by key\nrow := env.Get(`APP_URL`)\n\n// To receive a block by prefix\nblock := env.GetBlock(`app`)\n\n// To remove a row from data-structure\nenv.RemoveRow(`app-url`)\n\n// To remove a Block from data-structure\nenv.RemoveGroup(`app`)\n\n// To add a row or a block\nenv.Add(NewRow(`key`, `val`))\nenv.Add(NewBlock(`prefix`))\n\n// To merge\nenv, err := envi.Load(`stubs/.env`)\nenv2, err := envi.Load(`stubs/.env.local`)\nenv.Merge(env2)\n\n// To merge items\nenv.MergeItems(block1, row1, row3, block2)\n\n// To set system environment\noverride := true // override existing envs\nenv.SetEnv(override)\n```\n\nIn blocks\n\n```go\n\n// To create a new Block\nblock := envi.NewBlock(`APP`)\n\n// To Receive a Block from `Envi` \nblock := env.GetBlock(`APP`)\n\n// Rows count in the block\nblock.Count()\n\n// To receive a row from a block by key without block's prefix. Not: `app-hash`!\nr = block.GetRow(`hash`)\n\n// To receive a row from a block by key with block's prefix. Allow: `app-hash`\nr = block.GetPrefixedRow(`hash`)\n\n// To add rows into a block\nblock.AddRows(rows2, rows1, envi.NewRow(`section`, `1`), ...)\n\n// To add fully prefixed rows\nblock.AddPrefixedRows(envi.NewRow(`APP-section`, `1`), ...)\n\n// To set block's comment\nblock.SetComment(`New Comment`)\n\n// To merge with other block\nblock.MergeBlock(block2)\n\n// To merge with row\nblock.MergeRow(row)\n\n// To find out if this row exists\nblock.HasRow(row)\n\n// To remove a row from the Block by key\nblock.RemoveRow(`section`)\n\n// To remove a row from the Block by fully key\nblock.RemovePrefixedRow(`app-section`)\n```\n\nIn rows\n\n```go\n\n// To create a new row\nrow := envi.NewRow(`app-section`, `two`)\n\n// To set a comment\nrow.SetComment(`new section`)\n\n// To comment row\nrow.Commented()\n\n// To merge with other row\nrow.Merge()\n\n// To get full key\nrow.GetFullKey()\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fefureev%2Fenvi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fefureev%2Fenvi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fefureev%2Fenvi/lists"}