{"id":19297934,"url":"https://github.com/cidgravity/snakelet","last_synced_at":"2025-10-05T04:27:11.426Z","repository":{"id":64887458,"uuid":"578278298","full_name":"CIDgravity/snakelet","owner":"CIDgravity","description":"Minimal go config lib based on viper","archived":false,"fork":false,"pushed_at":"2023-02-09T20:33:33.000Z","size":139,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-02-24T01:12:53.329Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","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/CIDgravity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","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}},"created_at":"2022-12-14T17:14:32.000Z","updated_at":"2023-02-01T19:22:06.000Z","dependencies_parsed_at":"2023-02-17T10:30:27.647Z","dependency_job_id":null,"html_url":"https://github.com/CIDgravity/snakelet","commit_stats":{"total_commits":21,"total_committers":2,"mean_commits":10.5,"dds":0.1428571428571429,"last_synced_commit":"b1a34af2200a61e47e3a4126b1dea15775ea46e1"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/CIDgravity/snakelet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CIDgravity%2Fsnakelet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CIDgravity%2Fsnakelet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CIDgravity%2Fsnakelet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CIDgravity%2Fsnakelet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CIDgravity","download_url":"https://codeload.github.com/CIDgravity/snakelet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CIDgravity%2Fsnakelet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278409913,"owners_count":25982268,"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-05T02:00:06.059Z","response_time":54,"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":[],"created_at":"2024-11-09T23:06:31.851Z","updated_at":"2025-10-05T04:27:11.400Z","avatar_url":"https://github.com/CIDgravity.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# snakelet\n\nUltra minimal go config lib based on [viper](https://github.com/spf13/viper) and [validator](https://github.com/go-playground/validator).\n\nGoals:\n- simplicity =\u003e correctly validate once and return a nice statically typed struct that contains all the config\n- testability =\u003e dependency to config library only in main package. Everywhere else, just pass a subset of the config struct as parameter\n\nHow?:\n- define whichever complex type you want for your config and setup which params are required and which are not\n- call `InitAndLoad` only once in main package and unmarshal to a struct that is statically typed against the config type, reusing nice viper default for precedence (env -\u003e file -\u003e default)\n- crash fast if required fields hasn't been set or if config file cannot be parsed to the config types (ex: random string to int)\n- if no error, forget about `viper` and `snakelet`, only use this unmarshaled struct everywhere\n\nInstead of using the lib directly, the source code itself can be used for inspiration as to how to use `viper` internally to achieve the goals set above.\n\n## Install\n\n```\ngo get github.com/CIDgravity/snakelet\n```\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tsnakelet \"github.com/CIDgravity/snakelet\"\n)\n\ntype DatabaseConfig struct {\n\tUser     string `mapstructure:\"user\" validate:\"required\"`\n\tPassword string `mapstructure:\"password\" validate:\"required\"`\n\tHost     string `mapstructure:\"host\" validate:\"required\"`\n\tPort     int    `mapstructure:\"port\" validate:\"required\"`\n\tName     string `mapstructure:\"name\" validate:\"required\"`\n\tSslMode  string `mapstructure:\"sslMode\"`\n\tMaxConns int    `mapstructure:\"maxConns\"`\n}\n\ntype LogsConfig struct {\n\tLogLevel                string `mapstructure:\"level\"` // error | warn | info - case insensitive\n\tBackendLogsToJsonFormat bool   `mapstructure:\"isJSON\"`\n\tDatabaseSlowThreshold   string `mapstructure:\"databaseSlowThreshold\"` // Value under which a query is considered slow. \"1ms\", \"1s\", etc - anything that's parsable by time.ParseDuration(interval).\n}\n\ntype Config struct {\n\tDatabase DatabaseConfig `mapstructure:\"database\"`\n\tLogs     LogsConfig     `mapstructure:\"log\"`\n\tServer   ServerConfig   `mapstructure:\"server\"`\n}\n\ntype ServerConfig struct {\n\tPort int `mapstructure:\"port\" validate:\"required\"`\n}\n\n// only set default for non-required tag\nfunc GetDefaultConfig() *Config {\n\treturn \u0026Config{\n\t\tDatabase: DatabaseConfig{\n\t\t\tSslMode:  \"disable\",\n\t\t\tMaxConns: 200,\n\t\t},\n\t\tLogs: LogsConfig{\n\t\t\tLogLevel:                \"debug\",\n\t\t\tBackendLogsToJsonFormat: false,\n\t\t\tDatabaseSlowThreshold:   \"1ms\",\n\t\t},\n\t}\n\n}\n\nfunc main() {\n\t// Get config struct and default variables\n\tconf := GetDefaultConfig()\n\n\t// this will mutate the `conf` variable:\n\t_, err := snakelet.InitAndLoad(conf)\n\n\t// Example with params usage:\n\t// err := snakelet.InitAndLoadWithParams(conf, \"/opt/company/project.yaml\", \"company-project\")\n\n\t// Will throw an error because some param are required\n\tif err != nil {\n\t\tfmt.Println(\"Unable to init and load config: %w\", err)\n\t}\n\t// Pass `conf` or a subset of this variable as param throughout the rest of the code so you don't have any dependency on snakelet or viper whatsoever.\n\t// This also contributes to improving testability of your code (you can code with pure funcitons):\n\t// someServerComponent := server.NewServer(conf.Server)\n\t// someDbComponent := database.NewDatabase(conf.Database)\n}\n```\n\nSee the [example directory](./example) for the full working example that you can run.\n\nAlso checkout [the test file](./config_test.go) and [the source code](./config.go).\n\n## License\n\nCopyright (c), 2022, CIDgravity.\n\nAll this work is licensed under permissive open-source license.\n\nExcept written otherwise in particular files, this work is dual-licensed under both [Apache v2](./LICENSE-APACHE) and [MIT](./LICENSE-MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcidgravity%2Fsnakelet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcidgravity%2Fsnakelet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcidgravity%2Fsnakelet/lists"}