{"id":13836805,"url":"https://github.com/jamiealquiza/envy","last_synced_at":"2025-04-06T03:07:27.867Z","repository":{"id":57479986,"uuid":"76893809","full_name":"jamiealquiza/envy","owner":"jamiealquiza","description":"Envy automatically exposes environment variables for all of your Go flags","archived":false,"fork":false,"pushed_at":"2020-08-11T13:37:35.000Z","size":11,"stargazers_count":156,"open_issues_count":4,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T01:13:12.787Z","etag":null,"topics":["cli","env","environment","environment-variables","flags","go-packages","golang","library"],"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/jamiealquiza.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":"2016-12-19T20:12:23.000Z","updated_at":"2025-02-11T21:33:02.000Z","dependencies_parsed_at":"2022-09-26T17:41:34.688Z","dependency_job_id":null,"html_url":"https://github.com/jamiealquiza/envy","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiealquiza%2Fenvy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiealquiza%2Fenvy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiealquiza%2Fenvy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiealquiza%2Fenvy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamiealquiza","download_url":"https://codeload.github.com/jamiealquiza/envy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427006,"owners_count":20937201,"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":["cli","env","environment","environment-variables","flags","go-packages","golang","library"],"created_at":"2024-08-04T15:00:54.818Z","updated_at":"2025-04-06T03:07:27.849Z","avatar_url":"https://github.com/jamiealquiza.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"[![GoDoc](https://godoc.org/github.com/jamiealquiza/envy?status.svg)](https://godoc.org/github.com/jamiealquiza/envy)\n\n\n# envy\n\nAutomatically exposes environment variables for all of your flags. It supports the standard flags package along with limited support for [Cobra](https://github.com/spf13/cobra) commands.\n\nEnvy takes a namespace prefix that will be used for environment variable lookups. Each flag registered in your app will be prefixed, uppercased, and hyphens exchanged for underscores; if a matching environment variable is found, it will set the respective flag value as long as the value is not otherwise explicitly set (see usage for precedence).\n\n### Example: flag\n\nCode:\n```go\npackage main\n\nimport (\n        \"flag\"\n        \"fmt\"\n\n        \"github.com/jamiealquiza/envy\"\n)\n\nfunc main() {\n        var address = flag.String(\"address\", \"127.0.0.1\", \"Some random address\")\n        var port = flag.String(\"port\", \"8131\", \"Some random port\")\n\n        envy.Parse(\"MYAPP\") // Expose environment variables.\n        flag.Parse()\n\n        fmt.Println(*address)\n        fmt.Println(*port)\n}\n```\n\nOutput:\n```sh\n# Prints flag defaults\n% ./example\n127.0.0.1\n8131\n\n# Setting flags via env vars.\n% MYAPP_ADDRESS=\"0.0.0.0\" MYAPP_PORT=\"9080\" ./example\n0.0.0.0\n9080\n```\n\n### Example: Cobra\n\nCode:\n```go\n\n// Where to execute envy depends on the structure\n// of your Cobra implementation. A common pattern\n// is to define a root command and an 'Execute' function\n// that's called from the application main. We can call\n// envy ParseCobra here and configure it to recursively\n// update all child commands. Alternatively, it can be\n// scoped to child commands at some point in their\n// initialization.\n\nvar rootCmd = \u0026cobra.Command{\n Use: \"myapp\",\n}\n\nfunc Execute() {\n  // Configure envy.\n  cfg := CobraConfig{\n    // The env var prefix.\n    Prefix: \"MYAPP\",\n    // Whether to parse persistent flags.\n    Persistent: true,\n    // Whether to recursively update child command FlagSets.\n    Recursive: true,\n  }\n\n  // Apply.\n  envy.ParseCobra(rootCmd, cfg)\n\n  if err := rootCmd.Execute(); err != nil {\n    fmt.Println(err)\n    os.Exit(1)\n  }\n}\n```\n\nOutput:\n```sh\n# Root command flags.\n% myapp\nUsage:\n  myapp [command]\n\nAvailable Commands:\n  help        Help about any command\n  doathing    This is a subcommand\n\nFlags:\n  -h, --help               help for myapp\n      --some-config        A global config [MYAPP_SOME_CONFIG]\n\nUse \"myapp [command] --help\" for more information about a command.\n\n# Child command flags. Notice that the prefix\n# has the subcommand name automatically appended\n# while preserving global/parent env vars.\n% myapp doathing\nUsage:\n  myapp doathing [flags]\n\nFlags:\n  -h, --help               help for myapp\n      --subcmd-config      Another config [MYAPP_DOATHING_SUBCMD_CONFIG]\n\nGlobal Flags:\n      --some-flag          A global flag [MYAPP_SOME_FLAG]\n```\n\n### Usage\n\n**Variable precedence:**\n\nEnvy results in the following order of precedence, each item overwriting the previous:\n`flag default` -\u003e `Envy generated env var` -\u003e `flag set at the CLI`.\n\nResults referencing the stdlib flag example code:\n- `./example` will result in `port` being set to `8131`\n- `MYAPP_PORT=5678 ./example` will result in `port` being set to `5678`\n- `MYAPP_PORT=5678 ./example -port=1234` will result in `port` being set to `1234`\n\n\n**Env vars in help output:**\n\nEnvy can update your app help output so that it includes the environment variable generated/referenced for each flag. This is done by calling `envy.Parse()` before `flag.Parse()`.\n\nThe above example:\n```\nUsage of ./example:\n  -address string\n        Some random address [MYAPP_ADDRESS] (default \"127.0.0.1\")\n  -port string\n        Some random port [MYAPP_PORT] (default \"8131\")\n```\n\n If this isn't desired, simply call `envy.Parse()` after `flag.Parse()`:\n```go\n// ...\n\tflag.Parse()\n        envy.Parse(\"MYAPP\") // looks for MYAPP_ADDRESS \u0026 MYAPP_PORT\n// ...\n```\n\n```\nUsage of ./example:\n  -address string\n        Some random address (default \"127.0.0.1\")\n  -port string\n        Some random port (default \"8131\")\n```\n\n**Satisfying types:**\n\nEnvironment variables should be defined using a type that satisfies the respective type in your Go application's flag. For example:\n- `string` -\u003e `APP_ASTRINGVAR=\"someString\"`\n- `int` -\u003e `APP_ANINTVAR=42`\n- `bool` -\u003e `APP_ABOOLVAR=true`\n\n**Side effects:**\n\nSetting a flag through an Envy generated environment variable will have the same effects on the default `flag.CommandLine` as if the flag were set via the command line. This only affect users that may rely on `flag.CommandLine` methods that make distinctions between set and to-be set flags (such as the `Visit` method).\n\n**Cobra compatibility:**\n\nThe extensive types in Cobra's underlying [pflag](https://github.com/spf13/pflag) have not been tested, hence the \"limited support\" reference.\n\nAlso, keep in mind that Cobra can change in a way that breaks support with envy. Functionality was tested as of 2018-11-19.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiealquiza%2Fenvy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamiealquiza%2Fenvy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiealquiza%2Fenvy/lists"}