{"id":29704290,"url":"https://github.com/iamolegga/goenvsubst","last_synced_at":"2025-10-07T04:33:11.679Z","repository":{"id":302606622,"uuid":"1012959292","full_name":"iamolegga/goenvsubst","owner":"iamolegga","description":"it's like envsubst in linux but for Go data structures","archived":false,"fork":false,"pushed_at":"2025-07-03T08:45:44.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-22T09:34:55.274Z","etag":null,"topics":["config","configuration","environment","environment-variables","envsubst","go","golang"],"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/iamolegga.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-03T06:38:04.000Z","updated_at":"2025-07-04T18:43:06.000Z","dependencies_parsed_at":"2025-07-03T09:42:01.124Z","dependency_job_id":null,"html_url":"https://github.com/iamolegga/goenvsubst","commit_stats":null,"previous_names":["iamolegga/goenvsubst"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/iamolegga/goenvsubst","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fgoenvsubst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fgoenvsubst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fgoenvsubst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fgoenvsubst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamolegga","download_url":"https://codeload.github.com/iamolegga/goenvsubst/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamolegga%2Fgoenvsubst/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278722718,"owners_count":26034460,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["config","configuration","environment","environment-variables","envsubst","go","golang"],"created_at":"2025-07-23T14:09:59.333Z","updated_at":"2025-10-07T04:33:11.628Z","avatar_url":"https://github.com/iamolegga.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# goenvsubst\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/iamolegga/goenvsubst.svg)](https://pkg.go.dev/github.com/iamolegga/goenvsubst) [![Go Report Card](https://goreportcard.com/badge/github.com/iamolegga/goenvsubst)](https://goreportcard.com/report/github.com/iamolegga/goenvsubst) ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/iamolegga/goenvsubst/on-push-main.yml) [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/62d6a131cbdb4c268069b279773deb2a)](https://app.codacy.com/gh/iamolegga/goenvsubst/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_coverage) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/62d6a131cbdb4c268069b279773deb2a)](https://app.codacy.com/gh/iamolegga/goenvsubst/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA Go package for recursively replacing environment variable references in Go data structures with their actual values from the environment.\n\n## Features\n\n- **Comprehensive Type Support**: Works with structs, slices, maps, arrays, and pointers\n- **Nested Structures**: Handles deeply nested and complex data structures\n- **Safe Pointer Handling**: Safely processes nil pointers without panics\n- **In-Place Modification**: Modifies data structures in-place for efficiency\n- **Environment Variable Format**: Uses `$VAR_NAME` format for variable references\n- **Missing Variable Handling**: Replaces undefined or empty variables with empty strings\n- **Zero Dependencies**: Pure Go implementation with no external dependencies\n\n## Installation\n\n```bash\ngo get github.com/iamolegga/goenvsubst\n```\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"os\"\n    \"github.com/iamolegga/goenvsubst\"\n)\n\nfunc main() {\n    // Set environment variable\n    os.Setenv(\"DATABASE_URL\", \"postgres://localhost:5432/mydb\")\n    \n    config := \u0026struct {\n        DatabaseURL string\n        Debug       bool\n    }{\n        DatabaseURL: \"$DATABASE_URL\",\n        Debug:       true,\n    }\n    \n    err := goenvsubst.Do(config)\n    if err != nil {\n        panic(err)\n    }\n    \n    fmt.Printf(\"Database URL: %s\\n\", config.DatabaseURL)\n    // Output: Database URL: postgres://localhost:5432/mydb\n}\n```\n\n## Usage Examples\n\n### Struct Fields\n\n```go\ntype Config struct {\n    Host     string\n    Port     string\n    Username string\n    Password string\n}\n\nconfig := \u0026Config{\n    Host:     \"$DB_HOST\",\n    Port:     \"$DB_PORT\", \n    Username: \"$DB_USER\",\n    Password: \"$DB_PASS\",\n}\n\ngoenvsubst.Do(config)\n```\n\n### Slices and Arrays\n\n```go\n// Slice\nurls := \u0026[]string{\"$API_URL\", \"$BACKUP_URL\", \"https://static.example.com\"}\ngoenvsubst.Do(urls)\n\n// Array\nservers := \u0026[3]string{\"$SERVER1\", \"$SERVER2\", \"$SERVER3\"}\ngoenvsubst.Do(servers)\n```\n\n### Maps\n\n```go\nconfig := \u0026map[string]string{\n    \"database_url\": \"$DATABASE_URL\",\n    \"redis_url\":    \"$REDIS_URL\",\n    \"api_key\":      \"$API_KEY\",\n}\ngoenvsubst.Do(config)\n// Note: Map keys are never modified, only values\n```\n\n### Complex Nested Structures\n\n```go\ntype DatabaseConfig struct {\n    URL      string\n    Username string\n    Password string\n}\n\ntype AppConfig struct {\n    Database DatabaseConfig\n    Services []string\n    Env      map[string]string\n}\n\nconfig := \u0026AppConfig{\n    Database: DatabaseConfig{\n        URL:      \"$DATABASE_URL\",\n        Username: \"$DB_USER\",\n        Password: \"$DB_PASS\",\n    },\n    Services: []string{\"$SERVICE1\", \"$SERVICE2\"},\n    Env: map[string]string{\n        \"LOG_LEVEL\": \"$LOG_LEVEL\",\n        \"DEBUG\":     \"$DEBUG_MODE\",\n    },\n}\n\ngoenvsubst.Do(config)\n```\n\n### Pointers\n\n```go\n// Safe with nil pointers\nvar config *struct{ Value string }\ngoenvsubst.Do(\u0026config) // No error, no operation\n\n// Works with actual pointers\nvalue := \"$MY_VALUE\"\nptr := \u0026value\ngoenvsubst.Do(\u0026ptr)\n```\n\n## Supported Data Types\n\n| Type | Support | Notes |\n|------|---------|-------|\n| `string` | ✅ | Environment variables are substituted |\n| `struct` | ✅ | All string fields are processed recursively |\n| `slice` | ✅ | All elements are processed recursively |\n| `array` | ✅ | All elements are processed recursively |\n| `map` | ✅ | Only values are processed, keys remain unchanged |\n| `pointer` | ✅ | Safely handles nil pointers |\n| `int`, `bool`, etc. | ✅ | Non-string types are ignored (no substitution) |\n| `interface{}` | ❌ | Not currently supported |\n\n## Environment Variable Format\n\nEnvironment variables should be referenced using the `$VAR_NAME` format:\n\n```go\n// Supported formats\n\"$DATABASE_URL\"           // ✅ Full string replacement\n\"$MISSING_VAR\"            // ✅ Undefined vars become empty strings\n\n// Not supported formats\n\"${DATABASE_URL}\"         // ❌ Braces not supported\n\"prefix-$API_KEY-suffix\"  // ❌ Partial substitution not supported\n```\n\n## Error Handling\n\nThe `Do` function returns an error for future extensibility, but currently is designed to be robust:\n\n```go\nerr := goenvsubst.Do(config)\nif err != nil {\n    log.Printf(\"Failed to substitute environment variables: %v\", err)\n    return\n}\n```\n\n## Important Notes\n\n- **In-Place Modification**: The function modifies the input data structure directly\n- **Map Keys**: Only map values are processed, keys are never modified\n- **Missing Variables**: Undefined or empty environment variables are replaced with empty strings\n- **Thread Safety**: Safe for concurrent use (doesn't modify global state)\n- **Nil Pointers**: Handled safely without causing panics\n- **Type Safety**: Only string values are processed for substitution\n\n## Testing\n\nRun the tests:\n\n```bash\ngo test -v\n```\n\nRun example tests:\n\n```bash\ngo test -v -run Example\n```\n\n## Documentation\n\nFor detailed documentation and more examples, visit: [pkg.go.dev Documentation](https://pkg.go.dev/github.com/iamolegga/goenvsubst)\n\n## License\n\nMIT License - see [LICENSE](LICENSE) file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamolegga%2Fgoenvsubst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamolegga%2Fgoenvsubst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamolegga%2Fgoenvsubst/lists"}